{"id":13880803,"url":"https://github.com/thalesrc/js-utils","last_synced_at":"2025-07-16T17:31:07.455Z","repository":{"id":56243039,"uuid":"132260129","full_name":"thalesrc/js-utils","owner":"thalesrc","description":"Javascript utility functions for web development","archived":false,"fork":false,"pushed_at":"2024-09-03T21:17:01.000Z","size":641,"stargazers_count":25,"open_issues_count":1,"forks_count":5,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-05-20T13:14:50.134Z","etag":null,"topics":["async-map","compact","deepclone","defer","difference","intersection","isfalsy","istruthy","javascript","js-utils","minmax","noop","promise","replace","revert","timeout","typescript","unique-id","utility-library"],"latest_commit_sha":null,"homepage":"https://thalesrc.github.io/js-utils/","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/thalesrc.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":null,"patreon":"alisahin","open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":null}},"created_at":"2018-05-05T15:27:11.000Z","updated_at":"2024-09-25T03:16:39.000Z","dependencies_parsed_at":"2022-08-15T15:20:29.571Z","dependency_job_id":null,"html_url":"https://github.com/thalesrc/js-utils","commit_stats":null,"previous_names":[],"tags_count":80,"template":false,"template_full_name":null,"purl":"pkg:github/thalesrc/js-utils","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thalesrc%2Fjs-utils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thalesrc%2Fjs-utils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thalesrc%2Fjs-utils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thalesrc%2Fjs-utils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thalesrc","download_url":"https://codeload.github.com/thalesrc/js-utils/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thalesrc%2Fjs-utils/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265527545,"owners_count":23782480,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["async-map","compact","deepclone","defer","difference","intersection","isfalsy","istruthy","javascript","js-utils","minmax","noop","promise","replace","revert","timeout","typescript","unique-id","utility-library"],"created_at":"2024-08-06T08:03:30.000Z","updated_at":"2025-07-16T17:31:07.116Z","avatar_url":"https://github.com/thalesrc.png","language":"TypeScript","readme":"# @thalesrc/js-utils\nJavascript utility functions for web development\n\n[![travis](https://travis-ci.org/thalesrc/js-utils.svg)](https://travis-ci.org/thalesrc/js-utils)\n[![codecov](https://codecov.io/gh/thalesrc/js-utils/branch/master/graph/badge.svg)](https://codecov.io/gh/thalesrc/js-utils)\n[![npm](https://img.shields.io/npm/v/@thalesrc/js-utils.svg)](https://www.npmjs.com/package/@thalesrc/js-utils)\n[![npm](https://img.shields.io/npm/dw/@thalesrc/js-utils.svg)](https://www.npmjs.com/package/@thalesrc/js-utils)\n[![TypeScript](https://badges.frapsoft.com/typescript/version/typescript-next.svg?v=101)](https://github.com/ellerbrock/typescript-badges/)\n[![npm](https://img.shields.io/npm/l/@thalesrc/js-utils.svg)](https://github.com/thalesrc/js-utils/blob/master/LICENSE)\n[![Join the chat at https://gitter.im/thalesrc_js-utils/Lobby](https://badges.gitter.im/thalesrc_js-utils/Lobby.svg)](https://gitter.im/thalesrc_js-utils/Lobby?utm_source=badge\u0026utm_medium=badge\u0026utm_campaign=pr-badge\u0026utm_content=badge)\n\n## Motivation\n\nCollecting commonly used utility functions in a package.\n\n## Goals\n\n* Typescript support\n* Tree-shaking\n* No dependencies\n* High performance\n* Documentation\n* Well tested\n* Static/Prototype method support\n\n## Installation\n* npm: `npm install @thalesrc/js-utils --save`\n* yarn: `yarn add @thalesrc/js-utils`\n\n## Documentation\n\nSee: [thalesrc.github.io/js-utils](https://thalesrc.github.io/js-utils)\n\n## Functions\n\n### Array\n\n#### [Async Map](https://thalesrc.github.io/js-utils/modules/_array_async_map_.html)\nMaps an array asynchronously\n\n```typescript\nimport { asyncMap } \"@thalesrc/js-utils/array\";\n\nconst array = [1, 2, 3];\n\nconst result = await asyncMap(array, async value =\u003e {\n return await addOneAfterASecond(value);\n});\n\nconsole.log(result); // [2, 3, 4]\n```\n\n#### [Compact](https://thalesrc.github.io/js-utils/modules/_array_compact_.html)\nFilters falsy values of an array\n\n```typescript\nimport { compact } from \"@thalesrc/js-utils/array\";\n\nconst arr = [undefined, \"\", false, 0, 1, \"1\", null];\nconst compacted = compact(arr); // [1, \"1\"];\n```\n\n#### [Difference](https://thalesrc.github.io/js-utils/modules/_array_difference_.html)\nGets the difference of the two arrays or sets\n\n```typescript\nimport { difference } from \"@thalesrc/js-utils/array\";\n\nconst base = [\"a\", \"b\", \"c\", \"d\", \"a\", \"b\", \"c\", \"d\"];\n\ndifference(base, [\"a\", \"b\"]); // [\"c\", \"d\", \"c\", \"d\"]\ndifference(base, [\"a\", \"b\"], true); // [\"c\", \"d\", \"a\", \"b\", \"c\", \"d\"]\n```\n\n#### [Find By Key](https://thalesrc.github.io/js-utils/modules/_array_find_by_key_.html)\nFinds an object in an array by matching the value set on the key\n\n```typescript\nimport { findByKey } from \"@thalesrc/js-utils/array\";\n\nconst array = [{a: 1}, {a: 2}, {a: 3}];\n\nfindByKey(array, \"a\", 2); // {a: 2}\n```\n\n#### [Intersection](https://thalesrc.github.io/js-utils/modules/_array_intersection_.html)\nGets the intersection of the two arrays or sets\n\n```typescript\nimport { intersection } from \"@thalesrc/js-utils/array\";\n\nconst base = [\"a\", \"b\", \"c\", \"d\", \"a\", \"b\", \"c\", \"d\"];\n\nintersection(base, [\"a\", \"b\", \"x\"]); // [\"a\", \"b\", \"a\", \"b\"]\nintersection(base, [\"a\", \"b\", \"x\"], false); // [\"a\", \"b\"]\n```\n\n#### [Limit](https://thalesrc.github.io/js-utils/modules/_array_limit_.html)\nReturns first `n` children of an array\n\n```typescript\nimport { limit } from \"@thalesrc/js-utils/array\";\n\nconst array = [\"a\", \"b\", \"c\", \"d\", \"e\", \"f\"];\n\nlimit(array, 3); // [\"a\", \"b\", \"c\"]\n```\n\n#### [Remove](https://thalesrc.github.io/js-utils/modules/_array_remove_.html)\nRemoves an item from an array\n\n```typescript\nimport { remove } from \"@thalesrc/js-utils/array\";\n\nconst array = [\"a\", \"b\", \"c\", \"a\", \"b\", \"c\"];\n\nremove(array, \"b\"); // [\"a\", \"c\", \"a\", \"b\", \"c\"]\nremove(array, \"b\", true); // [\"a\", \"c\", \"a\", \"c\"]\n```\n\n#### [Replace](https://thalesrc.github.io/js-utils/modules/_array_replace_.html)\nReplaces an item with passed one of an array\n\n```typescript\nimport { replace } from \"@thalesrc/js-utils/array\";\n\nconst array = [\"a\", \"b\", \"c\", \"a\", \"b\", \"c\"];\n\nreplace(array, \"b\", \"x\"); // [\"a\", \"x\", \"c\", \"a\", \"b\", \"c\"]\nreplace(array, {startingIndex: 3, deleteCount: 1, itemsToReplace: ['x', 'y']}); // [\"a\", \"b\", \"c\", \"x\", \"y\", \"b\", \"c\"];\n\nconst config = new Map();\nconfig.set(\"a\", \"x\")\nconfig.set(\"b\", \"y\");\n\nreplace(array, {itemsToReplace: config}); // [\"x\", \"y\", \"c\", \"a\", \"b\", \"c\"];\nreplace(array, {itemsToReplace: config, multi: true}); // [\"x\", \"y\", \"c\", \"x\", \"y\", \"c\"];\n```\n\n#### [Uniquify](https://thalesrc.github.io/js-utils/modules/_array_uniquify_.html)\nRemoves repeated items from the array\n\n```typescript\nimport { uniquify } \"@thalesrc/js-utils/array\";\n\nconst array = [\"a\", \"b\", \"c\", \"a\", \"b\", \"c\"];\n\nuniquify(array); // [\"a\", \"b\", \"c\"]\n```\n\n#### [Uniquify By Key](https://thalesrc.github.io/js-utils/modules/_array_uniquify_by_key_.html)\nRemoves objects from the array which the value of its specifed key included before by another\n\n```typescript\nimport { uniquifyByKey } \"@thalesrc/js-utils/array\";\n\nconst array = [{a: 1}, {a: 1}, {a: 2}, {a: 3}, {a: 3}, {a: 4}];\n\nuniquifyByKey(array, 'a'); // [{a: 1}, {a: 2}, {a: 3}, {a: 4}]\n```\n\n### Function\n\n#### [Debounce](https://thalesrc.github.io/js-utils/modules/_function_debounce_.html)\nDebounces a function that delays invoking until after configured time have elapsed since the last time the debounced function was invoked\n\n```typescript\nimport { debounce } from \"@thalesrc/js-utils/promise\";\n\nfunction foo() {\n  console.log(\"hello\");\n}\n\nfor (let i = 0; i \u003c 10; i++) {\n  debounce(foo);\n}\n\n// logs \"hello\" only once\n```\n\n#### [Defer](https://thalesrc.github.io/js-utils/modules/_function_defer_.html)\nDelays the execution of the passed function\n\n```typescript\nimport { defer } from \"@thalesrc/js-utils/function\";\n\nconst result = await defer(() =\u003e aFunctionToDeferThatReturnsHello());\n\nconsole.log(result); // 'hello'\n```\n\n#### [Noop](https://thalesrc.github.io/js-utils/modules/_function_noop_.html)\nNoop function\n\n```typescript\nimport { noop } from \"@thalesrc/js-utils/function\";\n\nnoop();\n```\n\n#### [Of](https://thalesrc.github.io/js-utils/modules/_function_of_.html)\nCreates a function which returns the specified value\n\n```typescript\nimport { of } from \"@thalesrc/js-utils/function\";\n\nconst base = [1, 2, 5, {}, \"x\", \"y\"];\n\nbase.map(of('hi')); // [\"hi\", \"hi\", \"hi\", \"hi\", \"hi\", \"hi\"]\n```\n\n### Map\n\n#### [Merge](https://thalesrc.github.io/js-utils/modules/_map_merge_.html)\nMerges two maps\n\n```typescript\nimport { merge } from \"@thalesrc/js-utils/map\";\n\nconst first = new Map();\nfirst.set(\"a\", 1);\n\nconst second = new Map();\nsecond.set(\"b\", 2);\nmerge(first, second); // [{key: \"a\", value: 1}, {key: \"b\", value: 2}]\n```\n\n### Math\n\n#### [Min-Max](https://thalesrc.github.io/js-utils/modules/_math_min_max_.html)\nLimits the value by specified range\n\n```typescript\nimport { minMax } from \"@thalesrc/js-utils/math\";\n\nconst limitedValue = minMax(200, 300, Math.random() * 1000); // Outputs between 200-300\n```\n\n### Object\n\n#### [Clone](https://thalesrc.github.io/js-utils/modules/_object_clone_.html)\nA function to deep clone anything (recursively)\n\n```typescript\nimport { clone } from \"@thalesrc/js-utils/object\";\n\nconst object = {a: 1, b: {c: true, d: [\"x\", \"y\"]}};\n\nconst clonedObject = clone(object);\n// {a: 1, b: {c: true, d: [\"x\", \"y\"]}}\n// object.b.d === clonedObject.b.d // false\n```\n\n#### [Compact](https://thalesrc.github.io/js-utils/modules/_object_compact_.html)\nRemoves `null` and `undefined` values and their keys from an object\n\n```typescript\nimport { compact } from \"@thalesrc/js-utils/object\";\n\nconst a = {\n x: null,\n y: undefined,\n z: 20\n};\n\ncompact(a); // {z: 20}\n```\n\n#### [Deepest](https://thalesrc.github.io/js-utils/modules/_object_deepest_.html)\nGet deepest value in an object chain\n\n```typescript\nimport { deepest } from \"@thalesrc/js-utils/object\";\n\nconst a = {x: null};\nconst b = {x: a};\nconst c = {x: b};\n\ndeepest(c, 'x'); // {x: null} (a)\n```\n\n### Promise\n\n#### [Never](https://thalesrc.github.io/js-utils/modules/_promise_never_.html)\nA promise which never resolves\n\n```typescript\nimport { never, NEVER } from '@thalesrc/js-utils/promise';\n\nfunction foo(promise = never()) {\n  promise.then(val =\u003e {\n    ...\n  });\n}\n\n// or\n\nfunction foo(promise = NEVER) {\n  promise.then(val =\u003e {\n    ...\n  });\n}\n```\n\n#### [Revert](https://thalesrc.github.io/js-utils/modules/_promise_revert_.html)\nExchanges resolve state with rejection of a promise\n\n```typescript\nimport { revert } from \"@thalesrc/js-utils/promise\";\n\nconst errorPromise = Promise.reject(new Error('foo'));\n\nrevert(errorPromise)\n .then(err =\u003e {\n   console.log(\"this will be logged\", err);\n })\n .catch(res =\u003e {\n   console.log(\"this won't be logged\", res);\n });\n```\n\n#### [Timeout](https://thalesrc.github.io/js-utils/modules/_promise_timeout_.html)\nReturns a promise which resolves after specified time\n\n```typescript\nimport { timeout } from \"@thalesrc/js-utils/promise\";\n\ntimeout(1000)\n .then(() =\u003e console.log(\"will be logged after a second\"));\n```\n\n#### [Try Catch](https://thalesrc.github.io/js-utils/modules/_promise_try_catch_.html)\nMerges result and error in the same callback\n\n```typescript\nimport { tryCatch } from \"@thalesrc/js-utils/promise\";\n\nconst promise = anAsyncCall();\n\nconst [error, result] = await tryCatch(promise);\n```\n\n#### [Try One By One](https://thalesrc.github.io/js-utils/modules/_promise_try_one_by_one_.html)\nTries a set of promises one by one with given order. Breaks the call when a promise resolved. Otherwise keeps trying incoming promises until the list is finished.\n\n```typescript\nimport { tryOneByOne } from \"@thalesrc/js-utils/promise\";\n\nasync function fooFunction() {\n  const foo = await tryOneByOne([\n    () =\u003e someCall(),\n    (err) =\u003e anotherCall(),\n    (err) =\u003e fooPromise()\n  ]);\n\n  // do stuff\n}\n```\n\n### String\n\n#### [Limit](https://thalesrc.github.io/js-utils/modules/_string_limit_.html)\nLimits the string to `n` character\n\n```typescript\nimport { limit } from \"@thalesrc/js-utils/string\";\n\nconst str = 'foobarbaz';\n\nlimit(str, 3); // 'foo'\n```\n\n### Etc.\n\n#### [Arrayize](https://thalesrc.github.io/js-utils/modules/_arrayize_.html)\nEncapsulates a non array value with an array that contains it unless the value is already an array\n\n```typescript\nimport { arrayize } from \"@thalesrc/js-utils\";\n\nconst foo = 'foo';\nconst bar = ['bar'];\nconst fooArr = arrayize(foo); // ['foo'];\nconst barArr = arrayize(bar); // ['bar'];\n```\n\n#### [Compact](https://thalesrc.github.io/js-utils/modules/_compact_.html)\nFilters falsy values of the given array\nRemoves `null` and `undefined` values and their keys from an object\n\n```typescript\nimport { compact } from \"@thalesrc/js-utils\";\n\nconst arr = [undefined, \"\", false, 0, 1, \"1\"];\nconst compacted = compact(arr); // [1, \"1\"];\n\nconst object = {\n x: null,\n y: undefined,\n z: 20\n};\n\nconst compacted = compact(object); // {z: 20}\n```\n\n#### [Is Falsy](https://thalesrc.github.io/js-utils/modules/_is_falsy_.html)\nReturns whether the entered value is falsy\n\n```typescript\nimport { isFalsy } from \"@thalesrc/js-utils\";\n\nisFalsy(undefined); // true\nisFalsy(true); // false\n```\n\n#### [Is Truthy](https://thalesrc.github.io/js-utils/modules/_is_truthy_.html)\nReturns whether the entered value is truthy\n\n```typescript\nimport { isTruthy } from \"@thalesrc/js-utils\";\n\nisTruthy(undefined); // false\nisTruthy(true); // true\n```\n\n#### [Limit](https://thalesrc.github.io/js-utils/modules/_limit_.html)\nLimits the string or array to `n` character\n\n```typescript\nimport { limit } from \"@thalesrc/js-utils\";\n\nconst str = 'foobarbaz';\nconst array = [\"a\", \"b\", \"c\", \"d\", \"e\", \"f\"];\n\nlimit(str, 3); // 'foo'\nlimit(array, 3); // [\"a\", \"b\", \"c\"]\n```\n\n#### [Open Promise](https://thalesrc.github.io/js-utils/modules/_open-promise_.html)\nA promise constructor to resolve or reject from outside\n\n```typescript\nimport { OpenPromise } from \"@thalesrc/js-utils\";\n\nconst aPromiseWillBeResolvedLater = new OpenPromise();\n\naPromiseWillBeResolvedLater.then(val =\u003e console.log(val));\n\naPromiseWillBeResolvedLater.resolve({x: 1});\n// logs `{x: 1}`\n```\n\n#### [Smart Map](https://thalesrc.github.io/js-utils/modules/_smart_map_.html)\nLike WeakMap but can also store values using primitive keys\n\nSee: [WeakMap](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap)\n\n```typescript\nimport { SmartMap } from \"@thalesrc/js-utils\";\n\nconst aMap = new SmartMap();\n\naMap.set(\"foo\", \"foo\");\naMap.set(1, \"thales rocks\");\nconsole.log(aMap.size) // 2\n\naMap.set({}, \"thales rocks again\");\nconsole.log(aMap.size) // 2\n\nconst anObject = {};\naMap.set(anObject, \"thales rocks again and again\");\nconsole.log(aMap.size) // 3\nconsole.log(aMap.get(anObject)) // \"thales rocks again and again\"\n```\n\n#### [Unique Id](https://thalesrc.github.io/js-utils/modules/_unique-id_.html)\nStarts a new counter for every unique prefix and if a prefix is given, returns the id by prefixing it, otherwise returns the id as number\n\n```typescript\nimport { uniqueId } from \"@thalesrc/js-utils\";\n\nuniqueId(); // 0\nuniqueId(); // 1\nuniqueId(\"some-str\"); // \"some-str-0\";\nuniqueId(\"some-str\"); // \"some-str-1\";\nuniqueId(); // 3\n```\n\n## Static/Prototype Methods\n\nYou may use any of these methods by adding them to the constructors or prototypes to native objects in main file.\n\nPrototype Example:\n\n```typescript\n// main.ts\nimport \"@thalesrc/js-utils/array/proto/compact\";\n\n// somewhere else\nconst arr = [undefined, \"\", false, 0, 1, \"1\", null];\nconst compacted = arr.compact(); // [1, \"1\"];\n\n```\n\nStatic Example:\n\n```typescript\n// main.ts\nimport \"@thalesrc/js-utils/promise/static/timeout\";\n\n// somewhere else\nPromise.timeout(1000)\n .then(() =\u003e console.log(\"will be logged after a second\"));\n```\n\n## Legacy Typescript Support\n\nInstall legacy build\n\n`npm install @thalesrc/js-utils@legacy --save`\n## License\n\n[MIT](./LICENSE)","funding_links":["https://patreon.com/alisahin"],"categories":["TypeScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthalesrc%2Fjs-utils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthalesrc%2Fjs-utils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthalesrc%2Fjs-utils/lists"}