Ninja Parading Musically

    @nevware21/ts-utils
    TypeScript icon, indicating that this package has built-in type declarations

    0.6.0 • Public • Published

    @nevware21 ts-utils

    Common JavaScript/TypeScript helper functions for better minification

    GitHub Workflow Status (main) codecov npm version downloads downloads

    Description

    This is a collection of general JavaScript functions (written in and for TypeScript) to aid with removing code duplication to assist with minification, the provided functions are expected to only rarely be included in their namespaced environment.

    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

    • Node (12, 14, 16, 18)
    • Browser (Chromium - headless)
    • Web Worker (Chromium - headless)

    All of the polyfill functions are tested against the standard native implementations for node, browser and web-worker to ensure compatibility.

    Documentation and details

    See the documentation generated from source code via typedoc for a full list and details of all of the available types, functions and interfaces.

    See Browser Support for details.

    Type Functions / Helpers / Aliases / Polyfills
    Runtime Environment Checks getDocument(); getGlobal(); getHistory(); getInst(); getNavigator(); getPerformance(); getWindow(); hasDocument(); hasHistory(); hasNavigator(); hasPerformance(); hasWindow(); isNode(); isWebWorker(); hasIdleCallback();
    Type Identity isArray(); isArrayBuffer(); isBlob(); isBoolean(); isDate(); isError(); isFile(); isFormData(); isFunction(); isIterable(); isIterator(); isNullOrUndefined(); isNumber(); isObject(); isPlainObject(); isPrimitive(); isPromise(); isPromiseLike(); isThenable(); isRegExp(); isStrictNullOrUndefined(); isStrictUndefined(); isString(); isTypeof(); isUndefined();
    Value Check hasValue(); isDefined(); isNotTruthy(); isNullOrUndefined(); isStrictNullOrUndefined(); isStrictUndefined(); isTruthy(); isUndefined();
       
    Array arrAppend(); arrForEach(); arrIndexOf(); arrMap(); arrReduce(); getLength(); isArray();
    polyIsArray
    Enum createEnum(); createEnumKeyMap(); createEnumValueMap(); createSimpleMap(); createTypeMap();
    Error createCustomError(); isError(); throwError(); throwRangeError(); throwTypeError(); throwUnsupported();
    Iterator createArrayIterator(); createIterator(); createIterable(); createRangeIterator(); iterForOf(); isIterable(); isIterator(); makeIterable();
    Math mathCeil(); mathFloor(); mathMax(); mathMin(); mathToInt(); mathTrunc();
    Object deepExtend(); isObject(); objAssign(); objCopyProps(); objCreate(); objDeepCopy(); objDeepFreeze(); objDefine(); objDefineAccessors(); objDefineGet(); objDefineProp(); objDefineProps(); objDefineProperties(); objExtend(); objForEachKey(); objFreeze(); objGetOwnPropertyDescriptor(); objHasOwn(); objHasOwnProperty(); objKeys(); objSeal(); objGetPrototypeOf(); objSetPrototypeOf(); objToString();
    polyObjKeys(); polyObjHasOwn()
    String asString(); getLength(); isString(); strEndsWith(); strIndexOf(); strIsNullOrEmpty(); strIsNullOrWhiteSpace(); strLastIndexOf(); strLeft(); strPadEnd(); strPadStart(); strRepeat(); strRight(); strSlice(); strStartsWith(); strSubstr(); strSubstring(); strTrim(); strTrimEnd(); strTrimLeft(); strTrimRight(); strTrimStart();
    polyStrSubstr(); polyStrTrim(); polyStrTrimEnd(); polyStrTrimStart();
    Symbol WellKnownSymbols (const enum);
    getKnownSymbol(); getSymbol(); hasSymbol(); isSymbol(); newSymbol(); symbolFor(); symbolKeyFor();
    polyGetKnownSymbol(); polyNewSymbol(); polySymbolFor(); polySymbolKeyFor();

    Polyfills are used to automatically backfill runtimes that do not support Symbol, not all of the Symbol functionality is provided.
    Timer elapsedTime(); perfNow(); utcNow(); scheduleIdleCallback(); scheduleInterval(); scheduleTimeout(); scheduleTimeoutWith(); hasIdleCallback();
    For runtimes that don't support requestIdleCallback normal setTimeout() is used with the values from setDefaultIdleTimeout() and setDefaultMaxExecutionTime()
    polyUtcNow();
    Conversion asString(); getIntValue();
    Lazy getLazy();

    Unless otherwise stated in the functions documentation polyfills are used to automatically backfill unsupported functions in older ES5 runtimes

    Quickstart

    Install the npm packare: npm install @nevware21/ts-utils --save

    And then just import the helpers and use them.

    import { isArray, arrForEach, objForEachKey, objHasOwnProperty } from "@nevware21/ts-utils";
    
    export function simpleTest(theValue: any): string[] {
        let result: any[] = [];
    
        if (isArray(theValue)) {
            arrForEach(theValue, (value, idx) => {
                if (objHasOwnProperty(theValue, value)) {
                    result.push(idx + ":" + value);
                }
            });
        } else {
            objForEachKey(theValue, (key, value) => {
                if (value) {
                    result.push(key + "=" + value);
                }
            });
        }
    
        return result;
    }

    Or checking if a variable is a string

    import { isString } from "@nevware21/ts-utils";
    
    function checkString(value: any) {
        let ug = 1;
    
        return isString(value);
    }

    Browser Support

    General support is currently set to ES5 supported runtimes higher.

    Chrome Firefox IE Opera Safari
    Latest Latest 9+ Latest Latest

    Note: While some polyfills are provided to "somewhat" support ES3/IE8 this library does not intend to become a fully fledged polyfill library. And the polyfills provided (or contributed) are just the minimum set that have been required over time. And should be less necessary are time moves forward.

    Polyfills

    All of the included polyfills are tested against the current native implementation running in node, browser and worker environments to ensure that they conform to the current specification, these polyfills are only internally used for ES5 compatibility and when running in an environment (mostly IE) that does not support the required function.

    Some additional polyfills are provided for simple backward compatability to enable the utility functions in older environments (such as ES3 / IE8), however, you don't have to use or include these provided polyfils. If you need to use them you will need to import the pre-packaged "polyfill" bundle (bundle/ts-polyfills-utils.min.js) directly by hosting it on your own CDN or all of the non-internal polyfill implementations are exported so you could implement your own version of the polyfill initializer or more simply provide your own alternatives.

    Note: Several functions use the Object.defineProperty and therefore support is limited to runtimes or good polyfills that can correctly implement this functionality. (eg. createIterator; createIterable)

    Contributing

    Read our contributing guide to learn about our development process, how to propose bugfixes and improvements, and how to build and test your changes.

    Install

    npm i @nevware21/ts-utils

    DownloadsWeekly Downloads

    1,425

    Version

    0.6.0

    License

    MIT

    Unpacked Size

    2.46 MB

    Total Files

    296

    Last publish

    Collaborators

    • nev21