Skip to content

Commit

Permalink
Stop using internal functions for diagnostic to help with base minifi…
Browse files Browse the repository at this point in the history
…cation for smaller usages (#239)

- Reduce base minification usage
  • Loading branch information
nev21 authored Feb 6, 2024
1 parent 5711213 commit 3780a66
Show file tree
Hide file tree
Showing 41 changed files with 724 additions and 217 deletions.
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ This is a collection of general JavaScript functions (written in and for TypeScr

Support for standard JavaScript functions (ES5+) that are not support in all environments will be backed by internal polyfill implementations when not available.

### Test Environments
### Test Environments

- Node (16, 18, 20)
- Browser (Chromium - headless)
- Web Worker (Chromium - headless)
Expand All @@ -29,7 +30,7 @@ See [Browser Support](#browser-support) for details.

| Type | Functions / Helpers / Aliases / Polyfills
|----------------------------|---------------------------------------------------
| Runtime Environment Checks | <code>[getDocument](https://nevware21.github.io/ts-utils/typedoc/functions/getDocument.html)(); [getGlobal](https://nevware21.github.io/ts-utils/typedoc/functions/getGlobal.html)(); [getHistory](https://nevware21.github.io/ts-utils/typedoc/functions/getHistory.html)(); [getInst](https://nevware21.github.io/ts-utils/typedoc/functions/getInst.html)(); [getNavigator](https://nevware21.github.io/ts-utils/typedoc/functions/getNavigator.html)(); [getPerformance](https://nevware21.github.io/ts-utils/typedoc/functions/getPerformance.html)(); [getWindow](https://nevware21.github.io/ts-utils/typedoc/functions/getWindow.html)(); [hasDocument](https://nevware21.github.io/ts-utils/typedoc/functions/hasDocument.html)(); [hasHistory](https://nevware21.github.io/ts-utils/typedoc/functions/hasHistory.html)(); [hasNavigator](https://nevware21.github.io/ts-utils/typedoc/functions/hasNavigator.html)(); [hasPerformance](https://nevware21.github.io/ts-utils/typedoc/functions/hasPerformance.html)(); [hasWindow](https://nevware21.github.io/ts-utils/typedoc/functions/hasWindow.html)(); [isNode](https://nevware21.github.io/ts-utils/typedoc/functions/isNode.html)(); [isWebWorker](https://nevware21.github.io/ts-utils/typedoc/functions/isWebWorker.html)(); [hasIdleCallback](https://nevware21.github.io/ts-utils/typedoc/functions/hasIdleCallback.html)(); [lazySafeGetInst](https://nevware21.github.io/ts-utils/typedoc/functions/lazySafeGetInst.html)(); </code>
| Runtime Environment Checks | <code>[getDocument](https://nevware21.github.io/ts-utils/typedoc/functions/getDocument.html)(); [getGlobal](https://nevware21.github.io/ts-utils/typedoc/functions/getGlobal.html)(); [getHistory](https://nevware21.github.io/ts-utils/typedoc/functions/getHistory.html)(); [getInst](https://nevware21.github.io/ts-utils/typedoc/functions/getInst.html)(); [getNavigator](https://nevware21.github.io/ts-utils/typedoc/functions/getNavigator.html)(); [getPerformance](https://nevware21.github.io/ts-utils/typedoc/functions/getPerformance.html)(); [getWindow](https://nevware21.github.io/ts-utils/typedoc/functions/getWindow.html)(); [hasDocument](https://nevware21.github.io/ts-utils/typedoc/functions/hasDocument.html)(); [hasHistory](https://nevware21.github.io/ts-utils/typedoc/functions/hasHistory.html)(); [hasNavigator](https://nevware21.github.io/ts-utils/typedoc/functions/hasNavigator.html)(); [hasPerformance](https://nevware21.github.io/ts-utils/typedoc/functions/hasPerformance.html)(); [hasWindow](https://nevware21.github.io/ts-utils/typedoc/functions/hasWindow.html)(); [isNode](https://nevware21.github.io/ts-utils/typedoc/functions/isNode.html)(); [isWebWorker](https://nevware21.github.io/ts-utils/typedoc/functions/isWebWorker.html)(); [hasIdleCallback](https://nevware21.github.io/ts-utils/typedoc/functions/hasIdleCallback.html)(); [lazySafeGetInst](https://nevware21.github.io/ts-utils/typedoc/functions/lazySafeGetInst.html)(); [safeGetInst](https://nevware21.github.io/ts-utils/typedoc/functions/safeGetInst.html)(); </code>
| Type Identity | <code>[isArray](https://nevware21.github.io/ts-utils/typedoc/functions/isArray.html)(); [isArrayBuffer](https://nevware21.github.io/ts-utils/typedoc/functions/isArrayBuffer.html)(); [isBlob](https://nevware21.github.io/ts-utils/typedoc/functions/isBlob.html)(); [isBoolean](https://nevware21.github.io/ts-utils/typedoc/functions/isBoolean.html)(); [isDate](https://nevware21.github.io/ts-utils/typedoc/functions/isDate.html)(); [isError](https://nevware21.github.io/ts-utils/typedoc/functions/isError.html)(); [isFile](https://nevware21.github.io/ts-utils/typedoc/functions/isFile.html)(); [isFormData](https://nevware21.github.io/ts-utils/typedoc/functions/isFormData.html)(); [isFunction](https://nevware21.github.io/ts-utils/typedoc/functions/isFunction.html)(); [isIterable](https://nevware21.github.io/ts-utils/typedoc/functions/isIterable.html)(); [isIterator](https://nevware21.github.io/ts-utils/typedoc/functions/isIterator.html)(); [isNullOrUndefined](https://nevware21.github.io/ts-utils/typedoc/functions/isNullOrUndefined.html)(); [isNumber](https://nevware21.github.io/ts-utils/typedoc/functions/isNumber.html)(); [isObject](https://nevware21.github.io/ts-utils/typedoc/functions/isObject.html)(); [isPlainObject](https://nevware21.github.io/ts-utils/typedoc/functions/isPlainObject.html)(); [isPrimitive](https://nevware21.github.io/ts-utils/typedoc/functions/isPrimitive.html)(); [isPrimitiveType](https://nevware21.github.io/ts-utils/typedoc/functions/isPrimitiveType.html)(); [isPromise](https://nevware21.github.io/ts-utils/typedoc/functions/isPromise.html)(); [isPromiseLike](https://nevware21.github.io/ts-utils/typedoc/functions/isPromiseLike.html)(); [isThenable](https://nevware21.github.io/ts-utils/typedoc/functions/isThenable.html)(); [isRegExp](https://nevware21.github.io/ts-utils/typedoc/functions/isRegExp.html)(); [isStrictNullOrUndefined](https://nevware21.github.io/ts-utils/typedoc/functions/isStrictNullOrUndefined.html)(); [isStrictUndefined](https://nevware21.github.io/ts-utils/typedoc/functions/isStrictUndefined.html)(); [isString](https://nevware21.github.io/ts-utils/typedoc/functions/isString.html)(); [isTypeof](https://nevware21.github.io/ts-utils/typedoc/functions/isTypeof.html)(); [isUndefined](https://nevware21.github.io/ts-utils/typedoc/functions/isUndefined.html)();</code>
| Value Check | <code>[hasValue](https://nevware21.github.io/ts-utils/typedoc/functions/hasValue.html)(); [isDefined](https://nevware21.github.io/ts-utils/typedoc/functions/isDefined.html)(); [isNotTruthy](https://nevware21.github.io/ts-utils/typedoc/functions/isNotTruthy.html)(); [isNullOrUndefined](https://nevware21.github.io/ts-utils/typedoc/functions/isNullOrUndefined.html)(); [isStrictNullOrUndefined](https://nevware21.github.io/ts-utils/typedoc/functions/isStrictNullOrUndefined.html)(); [isStrictUndefined](https://nevware21.github.io/ts-utils/typedoc/functions/isStrictUndefined.html)(); [isTruthy](https://nevware21.github.io/ts-utils/typedoc/functions/isTruthy.html)(); [isUndefined](https://nevware21.github.io/ts-utils/typedoc/functions/isUndefined.html)();</code>
| Value | <code>[getValueByKey](https://nevware21.github.io/ts-utils/typedoc/functions/getValueByKey.html)(); [setValueByKey](https://nevware21.github.io/ts-utils/typedoc/functions/setValueByKey.html)(); [getValueByIter](https://nevware21.github.io/ts-utils/typedoc/functions/getValueByIter.html)(); [setValueByIter](https://nevware21.github.io/ts-utils/typedoc/functions/setValueByIter.html)(); [encodeAsJson](https://nevware21.github.io/ts-utils/typedoc/functions/encodeAsJson.html)(); [encodeAsHtml](https://nevware21.github.io/ts-utils/typedoc/functions/encodeAsHtml.html)(); [asString](https://nevware21.github.io/ts-utils/typedoc/functions/asString.html)(); [getIntValue](https://nevware21.github.io/ts-utils/typedoc/functions/getIntValue.html)(); [normalizeJsName](https://nevware21.github.io/ts-utils/typedoc/functions/normalizeJsName.html)();</code>
Expand All @@ -46,8 +47,11 @@ See [Browser Support](#browser-support) for details.
| Symbol | <code>[WellKnownSymbols](https://nevware21.github.io/ts-utils/typedoc/enums/WellKnownSymbols.html) (const enum);<br/>[getKnownSymbol](https://nevware21.github.io/ts-utils/typedoc/functions/getKnownSymbol.html)(); [getSymbol](https://nevware21.github.io/ts-utils/typedoc/functions/getSymbol.html)(); [hasSymbol](https://nevware21.github.io/ts-utils/typedoc/functions/hasSymbol.html)(); [isSymbol](https://nevware21.github.io/ts-utils/typedoc/functions/isSymbol.html)(); [newSymbol](https://nevware21.github.io/ts-utils/typedoc/functions/newSymbol.html)(); [symbolFor](https://nevware21.github.io/ts-utils/typedoc/functions/symbolFor.html)(); [symbolKeyFor](https://nevware21.github.io/ts-utils/typedoc/functions/symbolKeyFor.html)();<br/>[polyGetKnownSymbol](https://nevware21.github.io/ts-utils/typedoc/functions/polyGetKnownSymbol.html)(); [polyNewSymbol](https://nevware21.github.io/ts-utils/typedoc/functions/polyNewSymbol.html)(); [polySymbolFor](https://nevware21.github.io/ts-utils/typedoc/functions/polySymbolFor.html)(); [polySymbolKeyFor](https://nevware21.github.io/ts-utils/typedoc/functions/polySymbolKeyFor.html)();</code><br/>Polyfills are used to automatically backfill runtimes that do not support `Symbol`, not all of the Symbol functionality is provided.
| Timer | <code>[elapsedTime](https://nevware21.github.io/ts-utils/typedoc/functions/elapsedTime.html)(); [perfNow](https://nevware21.github.io/ts-utils/typedoc/functions/perfNow.html)(); [utcNow](https://nevware21.github.io/ts-utils/typedoc/functions/utcNow.html)(); [scheduleIdleCallback](https://nevware21.github.io/ts-utils/typedoc/functions/scheduleIdleCallback.html)(); [scheduleInterval](https://nevware21.github.io/ts-utils/typedoc/functions/scheduleInterval.html)(); [scheduleTimeout](https://nevware21.github.io/ts-utils/typedoc/functions/scheduleTimeout.html)(); [scheduleTimeoutWith](https://nevware21.github.io/ts-utils/typedoc/functions/scheduleTimeoutWith.html)(); [hasIdleCallback](https://nevware21.github.io/ts-utils/typedoc/functions/hasIdleCallback.html)(); </code><br/>For runtimes that don't support `requestIdleCallback` normal setTimeout() is used with the values from [`setDefaultIdleTimeout`](https://nevware21.github.io/ts-utils/typedoc/functions/setDefaultIdleTimeout.html)() and [`setDefaultMaxExecutionTime`](https://nevware21.github.io/ts-utils/typedoc/functions/setDefaultMaxExecutionTime.html)(); <br /><code>[polyUtcNow](https://nevware21.github.io/ts-utils/typedoc/functions/polyUtcNow.html)();</code>
| Conversion | <code>[encodeAsJson](https://nevware21.github.io/ts-utils/typedoc/functions/encodeAsJson.html)(); [encodeAsHtml](https://nevware21.github.io/ts-utils/typedoc/functions/encodeAsHtml.html)(); [asString](https://nevware21.github.io/ts-utils/typedoc/functions/asString.html)(); [getIntValue](https://nevware21.github.io/ts-utils/typedoc/functions/getIntValue.html)(); [normalizeJsName](https://nevware21.github.io/ts-utils/typedoc/functions/normalizeJsName.html)(); [strLetterCase](https://nevware21.github.io/ts-utils/typedoc/functions/strLetterCase.html)(); [strCamelCase](https://nevware21.github.io/ts-utils/typedoc/functions/strCamelCase.html)(); [strKebabCase](https://nevware21.github.io/ts-utils/typedoc/functions/strKebabCase.html)(); [strSnakeCase](https://nevware21.github.io/ts-utils/typedoc/functions/strSnakeCase.html)(); [strUpper](https://nevware21.github.io/ts-utils/typedoc/functions/strUpper.html)(); [strLower](https://nevware21.github.io/ts-utils/typedoc/functions/strLower.html)(); </code>
| Cache | <code>[createCachedValue](https://nevware21.github.io/ts-utils/typedoc/functions/createCachedValue.html)(); [createDeferredCachedValue](https://nevware21.github.io/ts-utils/typedoc/functions/createDeferredCachedValue.html)();
| Lazy | <code>[getLazy](https://nevware21.github.io/ts-utils/typedoc/functions/getLazy.html)(); [getWritableLazy](https://nevware21.github.io/ts-utils/typedoc/functions/getWritableLazy.html)(); [lazySafeGetInst](https://nevware21.github.io/ts-utils/typedoc/functions/lazySafeGetInst.html)(); [safeGetLazy](https://nevware21.github.io/ts-utils/typedoc/functions/safeGetLazy.html)(); </code>
| Safe | <code>[safeGetLazy](https://nevware21.github.io/ts-utils/typedoc/functions/safeGetLazy.html)(); [safeGet](https://nevware21.github.io/ts-utils/typedoc/functions/safeGet.html)(); [lazySafeGetInst](https://nevware21.github.io/ts-utils/typedoc/functions/lazySafeGetInst.html)(); [safeGetLazy](https://nevware21.github.io/ts-utils/typedoc/functions/safeGetLazy.html)(); </code>
| Safe | <code>[safe](https://nevware21.github.io/ts-utils/typedoc/functions/safe.html)(); [safeGetLazy](https://nevware21.github.io/ts-utils/typedoc/functions/safeGetLazy.html)(); [safeGet](https://nevware21.github.io/ts-utils/typedoc/functions/safeGet.html)(); [lazySafeGetInst](https://nevware21.github.io/ts-utils/typedoc/functions/lazySafeGetInst.html)(); [safeGetLazy](https://nevware21.github.io/ts-utils/typedoc/functions/safeGetLazy.html)(); [safeGetInst](https://nevware21.github.io/ts-utils/typedoc/functions/safeGetInst.html)(); </code>
| Diagnostic | <code>[dumpObj](https://nevware21.github.io/ts-utils/typedoc/functions/dumpObj.html)(); </code>


> Unless otherwise stated in the functions documentation polyfills are used to automatically backfill unsupported functions in older ES5 runtimes
Expand All @@ -64,6 +68,10 @@ ie. It may or may not be ES6 depending on the runtime landscape and requests rec

When we release v2.x the supported browser matrix will also shift as required to match the defined language level supported at that time.

## TypeScript Support

This library is built using TypeScript v4.9.5 and uses some keywords that where added in v2.8, so while it is recommended that you use at least v4.9.5, but the definitions will require at least v2.8 and therefore this will be the current minimum support version. If there are issues with any versions of TypeScript please open an issue and we will review whether its possible to work around any limitations with any specific features.

## Quickstart

Install the npm packare: `npm install @nevware21/ts-utils --save`
Expand Down
4 changes: 2 additions & 2 deletions lib/src/funcs/fnProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
*/

import { arrForEach } from "../array/forEach";
import { arrSlice } from "../array/slice";
import { isArray, isFunction } from "../helpers/base";
import { ArrProto, CALL, SLICE } from "../internal/constants";
import { fnApply } from "./fnApply";
import { fnBind } from "./fnBind";
import { ProxyFunctionDef, TypeFuncNames } from "./types";
Expand Down Expand Up @@ -66,7 +66,7 @@ export function createFnDeferredProxy<H, F extends (...args:any) => any>(hostFn:

return function() {
// Capture the original arguments passed to the method
var theArgs = arrSlice(arguments);
var theArgs = ArrProto[SLICE][CALL](arguments);
let theHost = hostFn();

return fnApply(theHost[funcName] as (...args: any) => any, theHost, theArgs);
Expand Down
10 changes: 4 additions & 6 deletions lib/src/funcs/readArgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import { iterForOf } from "../iterator/forOf";
import { objHasOwn } from "../object/has_own";
import { getKnownSymbol, hasSymbol } from "../symbol/symbol";
import { WellKnownSymbols } from "../symbol/well_known"
import { ILazyValue, getLazy } from "../helpers/lazy"
import { ICachedValue, createCachedValue } from "../helpers/cache";

let _iterSymbol: ILazyValue<symbol>;
let _iterSymbol: ICachedValue<symbol>;

/**
* Read the arguments from the provided array, iterator / or generator function
Expand Down Expand Up @@ -85,14 +85,12 @@ let _iterSymbol: ILazyValue<symbol>;
*/
/*#__NO_SIDE_EFFECTS__*/
export function readArgs<T = any>(theArgs: ArrayLike<T> | Iterable<T>, start?: number, end?: number): T[] {
if (!_iterSymbol) {
_iterSymbol = getLazy(() => hasSymbol() && getKnownSymbol(WellKnownSymbols.iterator));
}


if (!objHasOwn(theArgs, LENGTH)) {
// Does not contain a length property so lets check if it's an iterable
// IArgument is both ArrayLike and an iterable, so prefering to treat it as
// an array for performance
!_iterSymbol && (_iterSymbol = createCachedValue(hasSymbol() && getKnownSymbol(WellKnownSymbols.iterator)));
let iterFn = _iterSymbol.v && theArgs[_iterSymbol.v];
if (iterFn) {
let values: T[] = [];
Expand Down
6 changes: 3 additions & 3 deletions lib/src/helpers/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Licensed under the MIT license.
*/

import { ArrCls, BOOLEAN, CALL, FUNCTION, NULL_VALUE, NUMBER, OBJECT, ObjProto, STRING, UNDEFINED, UNDEF_VALUE } from "../internal/constants";
import { ArrCls, BOOLEAN, FUNCTION, NULL_VALUE, NUMBER, OBJECT, ObjProto, STRING, UNDEFINED, UNDEF_VALUE } from "../internal/constants";
import { safeGet } from "./safe_get";

const PRIMITIVE_TYPES = [ STRING, NUMBER, BOOLEAN, UNDEFINED, "symbol", "bigint" ];
Expand Down Expand Up @@ -67,7 +67,7 @@ export function _createObjIs<T>(theName: string): (value: any) => value is T {
*/
/*#__NO_SIDE_EFFECTS__*/
export function objToString(value: any): string {
return ObjProto.toString[CALL](value);
return ObjProto.toString.call(value);
}

/**
Expand Down Expand Up @@ -564,7 +564,7 @@ export function isPromise<T>(value: any): value is Promise<T> {
*/
/*#__NO_SIDE_EFFECTS__*/
export function isNotTruthy(value: any) {
return !value || !safeGet(() => (value && (0 + value)), value);
return !value || !isTruthy(value);
}

/**
Expand Down
89 changes: 89 additions & 0 deletions lib/src/helpers/cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* @nevware21/ts-utils
* https://github.com/nevware21/ts-utils
*
* Copyright (c) 2024 Nevware21
* Licensed under the MIT license.
*/

import { objDefineProp, objDefineProperties } from "../object/define";

/**
* A generic interface for holding a cached value
* @since 0.10.5
* @group Helpers
* @group Cache
* @typeparam T - The type of the value to be cached
* @example
* ```ts
* let cachedValue: ICachedValue<string> = {
* v: "some value"
* };
* ```
*/
export interface ICachedValue<T> {
/**
* Returns the current cached value
*/
v: T
}

/**
* Create and return a readonly {@link ICachedValue} instance that is populated with the provided value.
* This is useful when you want to cache a previously fetched value and return it without having to re-fetch
* it again.
* This is a lightweight version of {@link getLazy} which does not support any expiration or invalidation,
* it also will not honor the {@link setBypassLazyCache} setting and will always return the provided value.
* @since 0.10.5
* @group Helpers
* @group Cache
* @typeparam T - The type of the value to be cached
* @param value
* @returns A new {@link ICachedValue} instance which wraps the provided value
* @example
* ```ts
* let cachedValue = createCachedValue("some value");
* // cachedValue.v === "some value"
*
* JSON.stringify(cachedValue) === '{"v":"some value"}';
* ```
*/
/*#__NO_SIDE_EFFECTS__*/
export function createCachedValue<T>(value: T): ICachedValue<T> {
return objDefineProp({
toJSON: () => value
} as any, "v", { value }) as ICachedValue<T>;
}

/**
* Create and return a readonly {@link ICachedValue} instance which will cache and return the value returned
* by the callback function. The callback function will only be called once, multiple access of the value
* will not cause re-execution of the callback as the result from the first call is cached internally.
* This is a lightweight version of {@link getLazy} which does not support any expiration or invalidation,
* it also will not honor the {@link setBypassLazyCache} setting and will always return the provided value.
* @since 0.10.5
* @group Helpers
* @group Cache
* @typeparam T - The type of the value to be cached
* @param cb - The callback function to fetch the value to be lazily evaluated and cached
* @returns
*/
/*#__NO_SIDE_EFFECTS__*/
export function createDeferredCachedValue<T>(cb: () => T): ICachedValue<T> {
return objDefineProp({} as ICachedValue<T>, "v", {
get: function () {
let result = cb();
objDefineProp(this, "v", {
value: result,
enumerable: true
});

return result;
},
enumerable: true,
configurable: true
});
}



Loading

0 comments on commit 3780a66

Please sign in to comment.