@telperion/js-utils 3.7.0 → 3.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.build-manifest.json +966 -0
- package/README.md +24 -0
- package/array/arrayize.cjs +5 -3
- package/array/arrayize.cjs.map +1 -0
- package/array/arrayize.d.cts +19 -0
- package/array/arrayize.d.ts +3 -1
- package/array/arrayize.js +2 -1
- package/array/arrayize.js.map +1 -1
- package/array/async-map.cjs +5 -3
- package/array/async-map.cjs.map +1 -0
- package/array/async-map.d.cts +36 -0
- package/array/async-map.d.ts +3 -1
- package/array/async-map.js +2 -1
- package/array/async-map.js.map +1 -1
- package/array/compact.cjs +8 -5
- package/array/compact.cjs.map +1 -0
- package/array/compact.d.cts +30 -0
- package/array/compact.d.ts +3 -1
- package/array/compact.js +4 -2
- package/array/compact.js.map +1 -1
- package/array/difference.cjs +5 -3
- package/array/difference.cjs.map +1 -0
- package/array/difference.d.cts +48 -0
- package/array/difference.d.ts +6 -3
- package/array/difference.js +2 -1
- package/array/difference.js.map +1 -1
- package/array/find-by-key.cjs +5 -3
- package/array/find-by-key.cjs.map +1 -0
- package/array/find-by-key.d.cts +32 -0
- package/array/find-by-key.d.ts +3 -1
- package/array/find-by-key.js +2 -1
- package/array/find-by-key.js.map +1 -1
- package/array/gather.cjs +5 -3
- package/array/gather.cjs.map +1 -0
- package/array/gather.d.cts +50 -0
- package/array/gather.d.ts +3 -1
- package/array/gather.js +2 -1
- package/array/gather.js.map +1 -1
- package/array/index.cjs +34 -15
- package/array/index.cjs.map +1 -0
- package/array/index.d.cts +14 -0
- package/array/index.d.ts +14 -12
- package/array/index.js +14 -13
- package/array/index.js.map +1 -1
- package/array/intersection.cjs +5 -3
- package/array/intersection.cjs.map +1 -0
- package/array/intersection.d.cts +48 -0
- package/array/intersection.d.ts +6 -3
- package/array/intersection.js +2 -1
- package/array/intersection.js.map +1 -1
- package/array/limit.cjs +5 -3
- package/array/limit.cjs.map +1 -0
- package/array/limit.d.cts +31 -0
- package/array/limit.d.ts +3 -1
- package/array/limit.js +2 -1
- package/array/limit.js.map +1 -1
- package/array/proto/async-map.cjs +6 -4
- package/array/proto/async-map.cjs.map +1 -0
- package/array/proto/async-map.d.cts +24 -0
- package/array/proto/async-map.d.ts +0 -1
- package/array/proto/async-map.js +2 -2
- package/array/proto/async-map.js.map +1 -1
- package/array/proto/compact.cjs +6 -4
- package/array/proto/compact.cjs.map +1 -0
- package/array/proto/compact.d.cts +14 -0
- package/array/proto/compact.d.ts +0 -1
- package/array/proto/compact.js +2 -2
- package/array/proto/compact.js.map +1 -1
- package/array/proto/difference.cjs +6 -4
- package/array/proto/difference.cjs.map +1 -0
- package/array/proto/difference.d.cts +28 -0
- package/array/proto/difference.d.ts +2 -1
- package/array/proto/difference.js +2 -2
- package/array/proto/difference.js.map +1 -1
- package/array/proto/find-by-key.cjs +6 -4
- package/array/proto/find-by-key.cjs.map +1 -0
- package/array/proto/find-by-key.d.cts +24 -0
- package/array/proto/find-by-key.d.ts +0 -1
- package/array/proto/find-by-key.js +2 -2
- package/array/proto/find-by-key.js.map +1 -1
- package/array/proto/gather.cjs +6 -4
- package/array/proto/gather.cjs.map +1 -0
- package/array/proto/gather.d.cts +31 -0
- package/array/proto/gather.d.ts +0 -1
- package/array/proto/gather.js +2 -2
- package/array/proto/gather.js.map +1 -1
- package/array/proto/intersection.cjs +6 -4
- package/array/proto/intersection.cjs.map +1 -0
- package/array/proto/intersection.d.cts +28 -0
- package/array/proto/intersection.d.ts +2 -1
- package/array/proto/intersection.js +2 -2
- package/array/proto/intersection.js.map +1 -1
- package/array/proto/limit.cjs +6 -4
- package/array/proto/limit.cjs.map +1 -0
- package/array/proto/limit.d.cts +23 -0
- package/array/proto/limit.d.ts +0 -1
- package/array/proto/limit.js +2 -2
- package/array/proto/limit.js.map +1 -1
- package/array/proto/remove.cjs +6 -4
- package/array/proto/remove.cjs.map +1 -0
- package/array/proto/remove.d.cts +32 -0
- package/array/proto/remove.d.ts +3 -3
- package/array/proto/remove.js +2 -2
- package/array/proto/remove.js.map +1 -1
- package/array/proto/replace.cjs +6 -4
- package/array/proto/replace.cjs.map +1 -0
- package/array/proto/replace.d.cts +73 -0
- package/array/proto/replace.d.ts +2 -1
- package/array/proto/replace.js +2 -2
- package/array/proto/replace.js.map +1 -1
- package/array/proto/slice-after.cjs +8 -0
- package/array/proto/slice-after.cjs.map +1 -0
- package/array/proto/slice-after.d.cts +33 -0
- package/array/proto/slice-after.d.ts +33 -0
- package/array/proto/slice-after.js +6 -0
- package/array/proto/slice-after.js.map +1 -0
- package/array/proto/slice-before.cjs +8 -0
- package/array/proto/slice-before.cjs.map +1 -0
- package/array/proto/slice-before.d.cts +33 -0
- package/array/proto/slice-before.d.ts +33 -0
- package/array/proto/slice-before.js +6 -0
- package/array/proto/slice-before.js.map +1 -0
- package/array/proto/uniquify-by-key.cjs +6 -4
- package/array/proto/uniquify-by-key.cjs.map +1 -0
- package/array/proto/uniquify-by-key.d.cts +23 -0
- package/array/proto/uniquify-by-key.d.ts +0 -1
- package/array/proto/uniquify-by-key.js +2 -2
- package/array/proto/uniquify-by-key.js.map +1 -1
- package/array/proto/uniquify.cjs +6 -4
- package/array/proto/uniquify.cjs.map +1 -0
- package/array/proto/uniquify.d.cts +22 -0
- package/array/proto/uniquify.d.ts +0 -1
- package/array/proto/uniquify.js +2 -2
- package/array/proto/uniquify.js.map +1 -1
- package/array/remove.cjs +5 -3
- package/array/remove.cjs.map +1 -0
- package/array/remove.d.cts +35 -0
- package/array/remove.d.ts +3 -1
- package/array/remove.js +2 -1
- package/array/remove.js.map +1 -1
- package/array/replace.cjs +5 -3
- package/array/replace.cjs.map +1 -0
- package/array/replace.d.cts +142 -0
- package/array/replace.d.ts +8 -5
- package/array/replace.js +2 -1
- package/array/replace.js.map +1 -1
- package/array/slice-after.cjs +44 -0
- package/array/slice-after.cjs.map +1 -0
- package/array/slice-after.d.cts +35 -0
- package/array/slice-after.d.ts +35 -0
- package/array/slice-after.js +42 -0
- package/array/slice-after.js.map +1 -0
- package/array/slice-before.cjs +44 -0
- package/array/slice-before.cjs.map +1 -0
- package/array/slice-before.d.cts +35 -0
- package/array/slice-before.d.ts +35 -0
- package/array/slice-before.js +42 -0
- package/array/slice-before.js.map +1 -0
- package/array/static/arrayize.cjs +6 -4
- package/array/static/arrayize.cjs.map +1 -0
- package/array/static/arrayize.d.cts +7 -0
- package/array/static/arrayize.d.ts +2 -1
- package/array/static/arrayize.js +2 -2
- package/array/static/arrayize.js.map +1 -1
- package/array/uniquify-by-key.cjs +5 -3
- package/array/uniquify-by-key.cjs.map +1 -0
- package/array/uniquify-by-key.d.cts +31 -0
- package/array/uniquify-by-key.d.ts +3 -1
- package/array/uniquify-by-key.js +2 -1
- package/array/uniquify-by-key.js.map +1 -1
- package/array/uniquify.cjs +5 -3
- package/array/uniquify.cjs.map +1 -0
- package/array/uniquify.d.cts +29 -0
- package/array/uniquify.d.ts +3 -1
- package/array/uniquify.js +2 -1
- package/array/uniquify.js.map +1 -1
- package/boolean/static/is-falsy.cjs +6 -4
- package/boolean/static/is-falsy.cjs.map +1 -0
- package/boolean/static/is-falsy.d.cts +24 -0
- package/boolean/static/is-falsy.d.ts +2 -1
- package/boolean/static/is-falsy.js +2 -2
- package/boolean/static/is-falsy.js.map +1 -1
- package/boolean/static/is-truthy.cjs +6 -4
- package/boolean/static/is-truthy.cjs.map +1 -0
- package/boolean/static/is-truthy.d.cts +24 -0
- package/boolean/static/is-truthy.d.ts +2 -1
- package/boolean/static/is-truthy.js +2 -2
- package/boolean/static/is-truthy.js.map +1 -1
- package/class/index.cjs +8 -4
- package/class/index.cjs.map +1 -0
- package/class/index.d.cts +1 -0
- package/class/index.d.ts +1 -1
- package/class/index.js +1 -2
- package/class/index.js.map +1 -1
- package/class/mixin.cjs +6 -4
- package/class/mixin.cjs.map +1 -0
- package/class/mixin.d.cts +51 -0
- package/class/mixin.d.ts +3 -2
- package/class/mixin.js +3 -2
- package/class/mixin.js.map +1 -1
- package/compact.cjs +10 -7
- package/compact.cjs.map +1 -0
- package/compact.d.cts +4 -0
- package/compact.d.ts +4 -2
- package/compact.js +7 -5
- package/compact.js.map +1 -1
- package/fesm/js-utils.mjs +1817 -0
- package/fesm/js-utils.mjs.map +1 -0
- package/function/call.cjs +5 -3
- package/function/call.cjs.map +1 -0
- package/function/call.d.cts +3 -0
- package/function/call.d.ts +3 -1
- package/function/call.js +2 -1
- package/function/call.js.map +1 -1
- package/function/debounce.cjs +19 -16
- package/function/debounce.cjs.map +1 -0
- package/function/debounce.d.cts +47 -0
- package/function/debounce.d.ts +7 -4
- package/function/debounce.js +8 -6
- package/function/debounce.js.map +1 -1
- package/function/defer.cjs +8 -5
- package/function/defer.cjs.map +1 -0
- package/function/defer.d.cts +28 -0
- package/function/defer.d.ts +3 -1
- package/function/defer.js +4 -2
- package/function/defer.js.map +1 -1
- package/function/index.cjs +18 -8
- package/function/index.cjs.map +1 -0
- package/function/index.d.cts +5 -0
- package/function/index.d.ts +5 -5
- package/function/index.js +5 -6
- package/function/index.js.map +1 -1
- package/function/noop.cjs +5 -3
- package/function/noop.cjs.map +1 -0
- package/function/noop.d.cts +22 -0
- package/function/noop.d.ts +3 -1
- package/function/noop.js +2 -1
- package/function/noop.js.map +1 -1
- package/function/of.cjs +8 -5
- package/function/of.cjs.map +1 -0
- package/function/of.d.cts +33 -0
- package/function/of.d.ts +3 -1
- package/function/of.js +4 -2
- package/function/of.js.map +1 -1
- package/function/proto/debounce.cjs +7 -5
- package/function/proto/debounce.cjs.map +1 -0
- package/function/proto/debounce.d.cts +29 -0
- package/function/proto/debounce.d.ts +0 -1
- package/function/proto/debounce.js +2 -2
- package/function/proto/debounce.js.map +1 -1
- package/function/static/call.cjs +6 -4
- package/function/static/call.cjs.map +1 -0
- package/function/static/call.d.cts +7 -0
- package/function/static/call.d.ts +2 -1
- package/function/static/call.js +2 -2
- package/function/static/call.js.map +1 -1
- package/function/static/defer.cjs +6 -4
- package/function/static/defer.cjs.map +1 -0
- package/function/static/defer.d.cts +24 -0
- package/function/static/defer.d.ts +2 -1
- package/function/static/defer.js +2 -2
- package/function/static/defer.js.map +1 -1
- package/function/static/noop.cjs +6 -4
- package/function/static/noop.cjs.map +1 -0
- package/function/static/noop.d.cts +19 -0
- package/function/static/noop.d.ts +2 -1
- package/function/static/noop.js +2 -2
- package/function/static/noop.js.map +1 -1
- package/function/static/of.cjs +6 -4
- package/function/static/of.cjs.map +1 -0
- package/function/static/of.d.cts +27 -0
- package/function/static/of.d.ts +2 -1
- package/function/static/of.js +2 -2
- package/function/static/of.js.map +1 -1
- package/index.cjs +95 -34
- package/index.cjs.map +1 -0
- package/index.d.cts +44 -0
- package/index.d.ts +44 -15
- package/index.js +43 -17
- package/index.js.map +1 -1
- package/is-falsy.cjs +5 -3
- package/is-falsy.cjs.map +1 -0
- package/is-falsy.d.cts +30 -0
- package/is-falsy.d.ts +3 -1
- package/is-falsy.js +2 -1
- package/is-falsy.js.map +1 -1
- package/is-truthy.cjs +5 -3
- package/is-truthy.cjs.map +1 -0
- package/is-truthy.d.cts +30 -0
- package/is-truthy.d.ts +3 -1
- package/is-truthy.js +2 -1
- package/is-truthy.js.map +1 -1
- package/limit.cjs +10 -7
- package/limit.cjs.map +1 -0
- package/limit.d.cts +4 -0
- package/limit.d.ts +4 -2
- package/limit.js +7 -5
- package/limit.js.map +1 -1
- package/map/index.cjs +8 -4
- package/map/index.cjs.map +1 -0
- package/map/index.d.cts +1 -0
- package/map/index.d.ts +1 -1
- package/map/index.js +1 -2
- package/map/index.js.map +1 -1
- package/map/merge.cjs +5 -3
- package/map/merge.cjs.map +1 -0
- package/map/merge.d.cts +26 -0
- package/map/merge.d.ts +3 -1
- package/map/merge.js +2 -1
- package/map/merge.js.map +1 -1
- package/math/index.cjs +8 -4
- package/math/index.cjs.map +1 -0
- package/math/index.d.cts +1 -0
- package/math/index.d.ts +1 -1
- package/math/index.js +1 -2
- package/math/index.js.map +1 -1
- package/math/min-max.cjs +5 -3
- package/math/min-max.cjs.map +1 -0
- package/math/min-max.d.cts +26 -0
- package/math/min-max.d.ts +3 -1
- package/math/min-max.js +2 -1
- package/math/min-max.js.map +1 -1
- package/math/static/min-max.cjs +6 -4
- package/math/static/min-max.cjs.map +1 -0
- package/math/static/min-max.d.cts +21 -0
- package/math/static/min-max.d.ts +2 -1
- package/math/static/min-max.js +2 -2
- package/math/static/min-max.js.map +1 -1
- package/object/clone.cjs +14 -11
- package/object/clone.cjs.map +1 -0
- package/object/clone.d.cts +164 -0
- package/object/clone.d.ts +9 -6
- package/object/clone.js +6 -4
- package/object/clone.js.map +1 -1
- package/object/compact.cjs +5 -3
- package/object/compact.cjs.map +1 -0
- package/object/compact.d.cts +39 -0
- package/object/compact.d.ts +3 -1
- package/object/compact.js +2 -1
- package/object/compact.js.map +1 -1
- package/object/deep-freeze.cjs +5 -3
- package/object/deep-freeze.cjs.map +1 -0
- package/object/deep-freeze.d.cts +3 -0
- package/object/deep-freeze.d.ts +3 -1
- package/object/deep-freeze.js +2 -1
- package/object/deep-freeze.js.map +1 -1
- package/object/deepest.cjs +5 -3
- package/object/deepest.cjs.map +1 -0
- package/object/deepest.d.cts +50 -0
- package/object/deepest.d.ts +5 -2
- package/object/deepest.js +2 -1
- package/object/deepest.js.map +1 -1
- package/object/index.cjs +16 -8
- package/object/index.cjs.map +1 -0
- package/object/index.d.cts +6 -0
- package/object/index.d.ts +6 -5
- package/object/index.js +5 -6
- package/object/index.js.map +1 -1
- package/object/map.cjs +5 -3
- package/object/map.cjs.map +1 -0
- package/object/map.d.cts +34 -0
- package/object/map.d.ts +3 -1
- package/object/map.js +2 -1
- package/object/map.js.map +1 -1
- package/object/static/clone.cjs +6 -4
- package/object/static/clone.cjs.map +1 -0
- package/object/static/clone.d.cts +44 -0
- package/object/static/clone.d.ts +2 -1
- package/object/static/clone.js +2 -2
- package/object/static/clone.js.map +1 -1
- package/object/static/compact.cjs +6 -4
- package/object/static/compact.cjs.map +1 -0
- package/object/static/compact.d.cts +27 -0
- package/object/static/compact.d.ts +2 -1
- package/object/static/compact.js +2 -2
- package/object/static/compact.js.map +1 -1
- package/object/static/deep-freeze.cjs +6 -4
- package/object/static/deep-freeze.cjs.map +1 -0
- package/object/static/deep-freeze.d.cts +7 -0
- package/object/static/deep-freeze.d.ts +2 -1
- package/object/static/deep-freeze.js +2 -2
- package/object/static/deep-freeze.js.map +1 -1
- package/object/static/deepest.cjs +6 -4
- package/object/static/deepest.cjs.map +1 -0
- package/object/static/deepest.d.cts +34 -0
- package/object/static/deepest.d.ts +3 -1
- package/object/static/deepest.js +2 -2
- package/object/static/deepest.js.map +1 -1
- package/object/static/map.cjs +6 -4
- package/object/static/map.cjs.map +1 -0
- package/object/static/map.d.cts +27 -0
- package/object/static/map.d.ts +2 -1
- package/object/static/map.js +2 -2
- package/object/static/map.js.map +1 -1
- package/open-promise.cjs +13 -11
- package/open-promise.cjs.map +1 -0
- package/open-promise.d.cts +82 -0
- package/open-promise.d.ts +5 -3
- package/open-promise.js +4 -2
- package/open-promise.js.map +1 -1
- package/package.json +817 -412
- package/promise/chain.cjs +5 -3
- package/promise/chain.cjs.map +1 -0
- package/promise/chain.d.cts +9 -0
- package/promise/chain.d.ts +3 -1
- package/promise/chain.js +2 -1
- package/promise/chain.js.map +1 -1
- package/promise/index.cjs +21 -10
- package/promise/index.cjs.map +1 -0
- package/promise/index.d.cts +7 -0
- package/promise/index.d.ts +7 -7
- package/promise/index.js +7 -8
- package/promise/index.js.map +1 -1
- package/promise/never.cjs +10 -7
- package/promise/never.cjs.map +1 -0
- package/promise/never.d.cts +36 -0
- package/promise/never.d.ts +4 -2
- package/promise/never.js +5 -3
- package/promise/never.js.map +1 -1
- package/promise/promisify.cjs +5 -3
- package/promise/promisify.cjs.map +1 -0
- package/promise/promisify.d.cts +9 -0
- package/promise/promisify.d.ts +3 -1
- package/promise/promisify.js +2 -1
- package/promise/promisify.js.map +1 -1
- package/promise/proto/revert.cjs +6 -4
- package/promise/proto/revert.cjs.map +1 -0
- package/promise/proto/revert.d.cts +28 -0
- package/promise/proto/revert.d.ts +0 -1
- package/promise/proto/revert.js +2 -2
- package/promise/proto/revert.js.map +1 -1
- package/promise/proto/try-catch.cjs +6 -4
- package/promise/proto/try-catch.cjs.map +1 -0
- package/promise/proto/try-catch.d.cts +29 -0
- package/promise/proto/try-catch.d.ts +0 -1
- package/promise/proto/try-catch.js +2 -2
- package/promise/proto/try-catch.js.map +1 -1
- package/promise/revert.cjs +5 -3
- package/promise/revert.cjs.map +1 -0
- package/promise/revert.d.cts +40 -0
- package/promise/revert.d.ts +3 -1
- package/promise/revert.js +2 -1
- package/promise/revert.js.map +1 -1
- package/promise/static/chain.cjs +6 -4
- package/promise/static/chain.cjs.map +1 -0
- package/promise/static/chain.d.cts +11 -0
- package/promise/static/chain.d.ts +2 -1
- package/promise/static/chain.js +2 -2
- package/promise/static/chain.js.map +1 -1
- package/promise/static/never.cjs +7 -5
- package/promise/static/never.cjs.map +1 -0
- package/promise/static/never.d.cts +40 -0
- package/promise/static/never.d.ts +2 -1
- package/promise/static/never.js +2 -2
- package/promise/static/never.js.map +1 -1
- package/promise/static/timeout.cjs +6 -4
- package/promise/static/timeout.cjs.map +1 -0
- package/promise/static/timeout.d.cts +22 -0
- package/promise/static/timeout.d.ts +2 -1
- package/promise/static/timeout.js +2 -2
- package/promise/static/timeout.js.map +1 -1
- package/promise/static/try-one-by-one.cjs +6 -4
- package/promise/static/try-one-by-one.cjs.map +1 -0
- package/promise/static/try-one-by-one.d.cts +28 -0
- package/promise/static/try-one-by-one.d.ts +2 -1
- package/promise/static/try-one-by-one.js +2 -2
- package/promise/static/try-one-by-one.js.map +1 -1
- package/promise/timeout.cjs +13 -11
- package/promise/timeout.cjs.map +1 -0
- package/promise/timeout.d.cts +112 -0
- package/promise/timeout.d.ts +5 -2
- package/promise/timeout.js +4 -3
- package/promise/timeout.js.map +1 -1
- package/promise/try-catch.cjs +5 -3
- package/promise/try-catch.cjs.map +1 -0
- package/promise/try-catch.d.cts +47 -0
- package/promise/try-catch.d.ts +4 -2
- package/promise/try-catch.js +2 -1
- package/promise/try-catch.js.map +1 -1
- package/promise/try-one-by-one.cjs +5 -3
- package/promise/try-one-by-one.cjs.map +1 -0
- package/promise/try-one-by-one.d.cts +44 -0
- package/promise/try-one-by-one.d.ts +3 -1
- package/promise/try-one-by-one.js +2 -1
- package/promise/try-one-by-one.js.map +1 -1
- package/set/proto/difference.cjs +6 -4
- package/set/proto/difference.cjs.map +1 -0
- package/set/proto/difference.d.cts +27 -0
- package/set/proto/difference.d.ts +2 -1
- package/set/proto/difference.js +2 -2
- package/set/proto/difference.js.map +1 -1
- package/set/proto/intersection.cjs +6 -4
- package/set/proto/intersection.cjs.map +1 -0
- package/set/proto/intersection.d.cts +27 -0
- package/set/proto/intersection.d.ts +2 -1
- package/set/proto/intersection.js +2 -2
- package/set/proto/intersection.js.map +1 -1
- package/smart-map.cjs +4 -3
- package/smart-map.cjs.map +1 -0
- package/smart-map.d.cts +82 -0
- package/smart-map.d.ts +3 -2
- package/smart-map.js +2 -1
- package/smart-map.js.map +1 -1
- package/string/index.cjs +8 -4
- package/string/index.cjs.map +1 -0
- package/string/index.d.cts +1 -0
- package/string/index.d.ts +1 -1
- package/string/index.js +1 -2
- package/string/index.js.map +1 -1
- package/string/limit.cjs +8 -5
- package/string/limit.cjs.map +1 -0
- package/string/limit.d.cts +31 -0
- package/string/limit.d.ts +3 -1
- package/string/limit.js +5 -3
- package/string/limit.js.map +1 -1
- package/string/proto/limit.cjs +6 -4
- package/string/proto/limit.cjs.map +1 -0
- package/string/proto/limit.d.cts +30 -0
- package/string/proto/limit.d.ts +3 -3
- package/string/proto/limit.js +2 -2
- package/string/proto/limit.js.map +1 -1
- package/time/days.cjs +8 -5
- package/time/days.cjs.map +1 -0
- package/time/days.d.cts +17 -0
- package/time/days.d.ts +3 -1
- package/time/days.js +4 -2
- package/time/days.js.map +1 -1
- package/time/hours.cjs +8 -5
- package/time/hours.cjs.map +1 -0
- package/time/hours.d.cts +17 -0
- package/time/hours.d.ts +3 -1
- package/time/hours.js +4 -2
- package/time/hours.js.map +1 -1
- package/time/index.cjs +20 -10
- package/time/index.cjs.map +1 -0
- package/time/index.d.cts +7 -0
- package/time/index.d.ts +7 -7
- package/time/index.js +7 -8
- package/time/index.js.map +1 -1
- package/time/minutes.cjs +8 -5
- package/time/minutes.cjs.map +1 -0
- package/time/minutes.d.cts +17 -0
- package/time/minutes.d.ts +3 -1
- package/time/minutes.js +4 -2
- package/time/minutes.js.map +1 -1
- package/time/months.cjs +8 -5
- package/time/months.cjs.map +1 -0
- package/time/months.d.cts +17 -0
- package/time/months.d.ts +3 -1
- package/time/months.js +4 -2
- package/time/months.js.map +1 -1
- package/time/seconds.cjs +5 -3
- package/time/seconds.cjs.map +1 -0
- package/time/seconds.d.cts +17 -0
- package/time/seconds.d.ts +3 -1
- package/time/seconds.js +2 -1
- package/time/seconds.js.map +1 -1
- package/time/weeks.cjs +8 -5
- package/time/weeks.cjs.map +1 -0
- package/time/weeks.d.cts +17 -0
- package/time/weeks.d.ts +3 -1
- package/time/weeks.js +4 -2
- package/time/weeks.js.map +1 -1
- package/time/years.cjs +8 -5
- package/time/years.cjs.map +1 -0
- package/time/years.d.cts +17 -0
- package/time/years.d.ts +3 -1
- package/time/years.js +4 -2
- package/time/years.js.map +1 -1
- package/unique-id.cjs +5 -3
- package/unique-id.cjs.map +1 -0
- package/unique-id.d.cts +23 -0
- package/unique-id.d.ts +4 -2
- package/unique-id.js +2 -1
- package/unique-id.js.map +1 -1
|
@@ -0,0 +1,1817 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Encapsulates a non array value with an array that contains it unless the value is already an array
|
|
3
|
+
*
|
|
4
|
+
* * * *
|
|
5
|
+
* Example usage:
|
|
6
|
+
* ```typescript
|
|
7
|
+
* import { arrayize } from "@telperion/js-utils";
|
|
8
|
+
*
|
|
9
|
+
* const foo = 'foo';
|
|
10
|
+
* const bar = ['bar'];
|
|
11
|
+
* const fooArr = arrayize(foo); // ['foo'];
|
|
12
|
+
* const barArr = arrayize(bar); // ['bar'];
|
|
13
|
+
* ```
|
|
14
|
+
* * * *
|
|
15
|
+
* @param value Array or single value to capsulate
|
|
16
|
+
*/
|
|
17
|
+
function arrayize(value) {
|
|
18
|
+
return value instanceof Array ? value : [value];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* #### Async Map
|
|
23
|
+
*
|
|
24
|
+
* Maps an array asynchronously
|
|
25
|
+
*
|
|
26
|
+
* * * *
|
|
27
|
+
* Example:
|
|
28
|
+
* ```typescript
|
|
29
|
+
* import { asyncMap } "@telperion/js-utils/array";
|
|
30
|
+
*
|
|
31
|
+
* const array = [1, 2, 3];
|
|
32
|
+
*
|
|
33
|
+
* asyncMap(array, async value => {
|
|
34
|
+
* return await addOneAfterASecond(value);
|
|
35
|
+
* }).then(result => {
|
|
36
|
+
* console.log(result); // [2, 3, 4]
|
|
37
|
+
* });
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* Example as Array Prototype:
|
|
41
|
+
* ```typescript
|
|
42
|
+
* import "@telperion/js-utils/array/proto/async-map";
|
|
43
|
+
*
|
|
44
|
+
* const array = [1, 2, 3];
|
|
45
|
+
*
|
|
46
|
+
* const result = await array.asyncMap(async value => await addOneAfterASecond(value));
|
|
47
|
+
* // [2, 3, 4]
|
|
48
|
+
* ```
|
|
49
|
+
* * * *
|
|
50
|
+
* @param array Array to map
|
|
51
|
+
* @param mapper Callback async function to map the array
|
|
52
|
+
* @return A promise contains asynchronusly mapped array result
|
|
53
|
+
*/
|
|
54
|
+
async function asyncMap(array, mapper) {
|
|
55
|
+
const promises = array.map(mapper);
|
|
56
|
+
return await Promise.all(promises);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* #### Returns whether the entered value is truthy
|
|
61
|
+
*
|
|
62
|
+
* * * *
|
|
63
|
+
* Example usage:
|
|
64
|
+
* ```typescript
|
|
65
|
+
* import { isTruthy } from "@telperion/js-utils";
|
|
66
|
+
*
|
|
67
|
+
* isTruthy(undefined); // false
|
|
68
|
+
* isTruthy(true); // true
|
|
69
|
+
* isTruthy([]) // true
|
|
70
|
+
*
|
|
71
|
+
* const truthyValues = ["a", undefined, "b", "", "c"].filter(isTruthy); // ["a", "b", "c"]
|
|
72
|
+
* ```
|
|
73
|
+
* Static usage example:
|
|
74
|
+
* ```typescript
|
|
75
|
+
* import "@telperion/js-utils/dist/as-static/is-truthy";
|
|
76
|
+
*
|
|
77
|
+
* Boolean.isTruthy(undefined); // false
|
|
78
|
+
* Boolean.isTruthy(true); // true
|
|
79
|
+
* Boolean.isTruthy([]) // true
|
|
80
|
+
*
|
|
81
|
+
* const truthyValues = ["a", undefined, "b", "", "c"].filter(Boolean.isTruthy); // ["a", "b", "c"]
|
|
82
|
+
* ```
|
|
83
|
+
* * * *
|
|
84
|
+
* @param value value to be checked
|
|
85
|
+
*/
|
|
86
|
+
function isTruthy(value) {
|
|
87
|
+
return !!value;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* #### Filters falsy values of the given array
|
|
92
|
+
* * does not modify the original array
|
|
93
|
+
* * Values to be filtered: `[undefined, null, "", 0, false, NaN]`
|
|
94
|
+
*
|
|
95
|
+
* * * *
|
|
96
|
+
* Example usage:
|
|
97
|
+
* ```typescript
|
|
98
|
+
* import { compact } from "@telperion/js-utils/array";
|
|
99
|
+
*
|
|
100
|
+
* const arr = [undefined, "", false, 0, 1, "1"];
|
|
101
|
+
* const compacted = compact(arr); // [1, "1"];
|
|
102
|
+
*
|
|
103
|
+
* ```
|
|
104
|
+
*
|
|
105
|
+
* Example as Array Prototype:
|
|
106
|
+
* ```typescript
|
|
107
|
+
* import "@telperion/js-utils/array/proto/compact";
|
|
108
|
+
*
|
|
109
|
+
* const arr = [undefined, "", false, 0, 1, "1"];
|
|
110
|
+
* const compacted = arr.compact(); // [1, "1"];
|
|
111
|
+
* ```
|
|
112
|
+
* * * *
|
|
113
|
+
* @param arrayToCompact array to compact
|
|
114
|
+
* @typeparam T type of array
|
|
115
|
+
* @returns __a new__ compacted array
|
|
116
|
+
*/
|
|
117
|
+
function compact$2(arrayToCompact) {
|
|
118
|
+
return arrayToCompact.filter(isTruthy);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function difference(base, substraction, allDiff = false) {
|
|
122
|
+
if (base instanceof Set) {
|
|
123
|
+
return new Set(difference(Array.from(base), substraction));
|
|
124
|
+
}
|
|
125
|
+
const subs = Array.from(substraction);
|
|
126
|
+
return base.filter(value => {
|
|
127
|
+
const index = subs.indexOf(value);
|
|
128
|
+
if (index > -1 && allDiff) {
|
|
129
|
+
subs.splice(index, 1);
|
|
130
|
+
}
|
|
131
|
+
return index === -1;
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* #### Find By Key
|
|
137
|
+
*
|
|
138
|
+
* Finds an object in an array by matching the value set on the key
|
|
139
|
+
*
|
|
140
|
+
* * * *
|
|
141
|
+
* Example:
|
|
142
|
+
* ```typescript
|
|
143
|
+
* import { findByKey } from "@telperion/js-utils/array";
|
|
144
|
+
*
|
|
145
|
+
* const array = [{a: 1}, {a: 2}, {a: 3}];
|
|
146
|
+
*
|
|
147
|
+
* findByKey(array, "a", 2); // {a: 2}
|
|
148
|
+
* ```
|
|
149
|
+
*
|
|
150
|
+
* Prototype Example:
|
|
151
|
+
* ```typescript
|
|
152
|
+
* import "@telperion/js-utils/array/proto/find-by-key";
|
|
153
|
+
*
|
|
154
|
+
* const array = [{a: 1}, {a: 2}, {a: 3}];
|
|
155
|
+
*
|
|
156
|
+
* array.findByKey("a", 2); // {a: 2}
|
|
157
|
+
* ```
|
|
158
|
+
* * * *
|
|
159
|
+
* @param array Collection of the objects
|
|
160
|
+
* @param key Key to search the value on
|
|
161
|
+
* @param value Value to match for the key
|
|
162
|
+
* @return Found object or undefined
|
|
163
|
+
*/
|
|
164
|
+
function findByKey(array, key, value) {
|
|
165
|
+
return array.find(item => item[key] === value);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* #### Groups array items by a specified key into a Map
|
|
170
|
+
*
|
|
171
|
+
* Iterates over the array and groups items that share the same value for the given key.
|
|
172
|
+
* Returns a `Map` where each key is a unique value of the specified property and each value is an array of matching items.
|
|
173
|
+
*
|
|
174
|
+
* * * *
|
|
175
|
+
* Example usage:
|
|
176
|
+
* ```typescript
|
|
177
|
+
* import { gather } from "@telperion/js-utils/array";
|
|
178
|
+
*
|
|
179
|
+
* const users = [
|
|
180
|
+
* { role: "admin", name: "Alice" },
|
|
181
|
+
* { role: "user", name: "Bob" },
|
|
182
|
+
* { role: "admin", name: "Charlie" },
|
|
183
|
+
* ];
|
|
184
|
+
*
|
|
185
|
+
* const grouped = gather(users, "role");
|
|
186
|
+
* // Map {
|
|
187
|
+
* // "admin" => [{ role: "admin", name: "Alice" }, { role: "admin", name: "Charlie" }],
|
|
188
|
+
* // "user" => [{ role: "user", name: "Bob" }]
|
|
189
|
+
* // }
|
|
190
|
+
* ```
|
|
191
|
+
*
|
|
192
|
+
* Example as Array Prototype:
|
|
193
|
+
* ```typescript
|
|
194
|
+
* import "@telperion/js-utils/array/proto/gather";
|
|
195
|
+
*
|
|
196
|
+
* const users = [
|
|
197
|
+
* { role: "admin", name: "Alice" },
|
|
198
|
+
* { role: "user", name: "Bob" },
|
|
199
|
+
* { role: "admin", name: "Charlie" },
|
|
200
|
+
* ];
|
|
201
|
+
*
|
|
202
|
+
* const grouped = users.gather("role");
|
|
203
|
+
* // Map {
|
|
204
|
+
* // "admin" => [{ role: "admin", name: "Alice" }, { role: "admin", name: "Charlie" }],
|
|
205
|
+
* // "user" => [{ role: "user", name: "Bob" }]
|
|
206
|
+
* // }
|
|
207
|
+
* ```
|
|
208
|
+
* * * *
|
|
209
|
+
* @param array Array of objects to group
|
|
210
|
+
* @param key Key to group items by
|
|
211
|
+
* @typeparam T Type of array items
|
|
212
|
+
* @typeparam P Key type
|
|
213
|
+
* @returns A Map grouping items by the specified key's value
|
|
214
|
+
*/
|
|
215
|
+
function gather(array, key) {
|
|
216
|
+
return array.reduce((acc, item) => {
|
|
217
|
+
const groupKey = item[key];
|
|
218
|
+
if (!acc.has(groupKey)) {
|
|
219
|
+
acc.set(groupKey, []);
|
|
220
|
+
}
|
|
221
|
+
acc.get(groupKey).push(item);
|
|
222
|
+
return acc;
|
|
223
|
+
}, new Map());
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function intersection(base, inclusion, allEquals = true) {
|
|
227
|
+
if (base instanceof Set) {
|
|
228
|
+
return new Set(intersection(Array.from(base), inclusion));
|
|
229
|
+
}
|
|
230
|
+
const incs = Array.from(inclusion);
|
|
231
|
+
return base.filter(value => {
|
|
232
|
+
const index = incs.indexOf(value);
|
|
233
|
+
if (index > -1 && !allEquals) {
|
|
234
|
+
incs.splice(index, 1);
|
|
235
|
+
}
|
|
236
|
+
return index > -1;
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* #### Limit
|
|
242
|
+
*
|
|
243
|
+
* Returns first `n` children of an array
|
|
244
|
+
*
|
|
245
|
+
* * * *
|
|
246
|
+
* Example:
|
|
247
|
+
* ```typescript
|
|
248
|
+
* import { limit } from "@telperion/js-utils/array";
|
|
249
|
+
*
|
|
250
|
+
* const array = ["a", "b", "c", "d", "e", "f"];
|
|
251
|
+
*
|
|
252
|
+
* limit(array, 3); // ["a", "b", "c"]
|
|
253
|
+
* ```
|
|
254
|
+
*
|
|
255
|
+
* Prototype Example:
|
|
256
|
+
* ```typescript
|
|
257
|
+
* import "@telperion/js-utils/array/proto/limit";
|
|
258
|
+
*
|
|
259
|
+
* const array = ["a", "b", "c", "d", "e", "f"];
|
|
260
|
+
*
|
|
261
|
+
* array.limit(3); // ["a", "b", "c"]
|
|
262
|
+
* ```
|
|
263
|
+
* * * *
|
|
264
|
+
* @param array: Array to return its children
|
|
265
|
+
* @param count Limiter
|
|
266
|
+
* @return New array
|
|
267
|
+
*/
|
|
268
|
+
function limit$2(array, count) {
|
|
269
|
+
if (count < 0) {
|
|
270
|
+
count = Number.MAX_SAFE_INTEGER;
|
|
271
|
+
}
|
|
272
|
+
return array.slice(0, count);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* #### Remove
|
|
277
|
+
*
|
|
278
|
+
* Removes an item from the array
|
|
279
|
+
*
|
|
280
|
+
* Removes all item references if multi is set to `true`
|
|
281
|
+
*
|
|
282
|
+
* * * *
|
|
283
|
+
* Example:
|
|
284
|
+
* ```typescript
|
|
285
|
+
* import { remove } from "@telperion/js-utils/array";
|
|
286
|
+
*
|
|
287
|
+
* const array = ["a", "b", "c", "a", "b", "c"];
|
|
288
|
+
*
|
|
289
|
+
* remove(array, "b"); // ["a", "c", "a", "b", "c"]
|
|
290
|
+
* remove(array, "b", true); // ["a", "c", "a", "c"]
|
|
291
|
+
* ```
|
|
292
|
+
*
|
|
293
|
+
* Prototype Example:
|
|
294
|
+
* ```typescript
|
|
295
|
+
* import "@telperion/js-utils/array/proto/remove";
|
|
296
|
+
*
|
|
297
|
+
* const array = ["a", "b", "c", "a", "b", "c"];
|
|
298
|
+
*
|
|
299
|
+
* array.remove("b"); // ["a", "c", "a", "b", "c"]
|
|
300
|
+
* array.remove("b", true); // ["a", "c", "a", "c"]
|
|
301
|
+
* ```
|
|
302
|
+
* * * *
|
|
303
|
+
* @param array: Array to remove the item
|
|
304
|
+
* @param itemToRemove Item to remove
|
|
305
|
+
* @return New array
|
|
306
|
+
*/
|
|
307
|
+
function remove(array, item, multi = false) {
|
|
308
|
+
let index = array.indexOf(item);
|
|
309
|
+
if (index < 0) {
|
|
310
|
+
return [...array];
|
|
311
|
+
}
|
|
312
|
+
do {
|
|
313
|
+
array = [...array.slice(0, index), ...array.slice(index + 1)];
|
|
314
|
+
index = array.indexOf(item);
|
|
315
|
+
} while (multi && index > -1);
|
|
316
|
+
return array;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* Default Options for Replace Items Mode
|
|
321
|
+
*/
|
|
322
|
+
const REPLACE_ITEMS_DEFAULT_OPTIONS = {
|
|
323
|
+
deleteCount: 1,
|
|
324
|
+
itemsToReplace: [],
|
|
325
|
+
startingIndex: 0
|
|
326
|
+
};
|
|
327
|
+
/**
|
|
328
|
+
* Default Options for Replace By Map Mode
|
|
329
|
+
*/
|
|
330
|
+
const REPLACE_BY_MAP_DEFAULT_OPTIONS = {
|
|
331
|
+
itemsToReplace: new Map(),
|
|
332
|
+
multi: false
|
|
333
|
+
};
|
|
334
|
+
function replace(array, options, itemToReplace) {
|
|
335
|
+
if (arguments.length > 2) {
|
|
336
|
+
options = {
|
|
337
|
+
startingIndex: array.indexOf(options),
|
|
338
|
+
itemsToReplace: [itemToReplace]
|
|
339
|
+
};
|
|
340
|
+
}
|
|
341
|
+
if (options.itemsToReplace instanceof Array) {
|
|
342
|
+
return _replaceItems(array, options);
|
|
343
|
+
}
|
|
344
|
+
else {
|
|
345
|
+
return _replaceByMap(array, options);
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
/**
|
|
349
|
+
* Replace items by 'replace items' mode
|
|
350
|
+
*
|
|
351
|
+
* @param array Array to replace items
|
|
352
|
+
* @param options Replace options
|
|
353
|
+
*/
|
|
354
|
+
function _replaceItems(array, options) {
|
|
355
|
+
const { startingIndex, deleteCount, itemsToReplace } = Object.assign({}, REPLACE_ITEMS_DEFAULT_OPTIONS, options);
|
|
356
|
+
const newArray = [...array];
|
|
357
|
+
// If item not found or index number is negative; do nothing
|
|
358
|
+
if (startingIndex < 0) {
|
|
359
|
+
return newArray;
|
|
360
|
+
}
|
|
361
|
+
newArray.splice(startingIndex, deleteCount, ...itemsToReplace);
|
|
362
|
+
return newArray;
|
|
363
|
+
}
|
|
364
|
+
/**
|
|
365
|
+
* Replace items by 'replace by map' mode
|
|
366
|
+
*
|
|
367
|
+
* @param array Array to replace items
|
|
368
|
+
* @param options Replace options
|
|
369
|
+
*/
|
|
370
|
+
function _replaceByMap(array, options) {
|
|
371
|
+
const { itemsToReplace, multi } = Object.assign({}, REPLACE_BY_MAP_DEFAULT_OPTIONS, options);
|
|
372
|
+
const iterationMethod = multi ? Array.prototype.forEach : Array.prototype.some;
|
|
373
|
+
const newArray = [...array];
|
|
374
|
+
itemsToReplace.forEach((value, key) => {
|
|
375
|
+
iterationMethod.call(newArray, (item, index) => {
|
|
376
|
+
const matches = item === key;
|
|
377
|
+
if (matches) {
|
|
378
|
+
newArray.splice(index, 1, value);
|
|
379
|
+
}
|
|
380
|
+
return matches;
|
|
381
|
+
});
|
|
382
|
+
});
|
|
383
|
+
return newArray;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* #### Uniquify
|
|
388
|
+
*
|
|
389
|
+
* Removes repeated items from the array
|
|
390
|
+
*
|
|
391
|
+
* * * *
|
|
392
|
+
* Example:
|
|
393
|
+
* ```typescript
|
|
394
|
+
* import { uniquify } "@telperion/js-utils/array";
|
|
395
|
+
*
|
|
396
|
+
* const array = ["a", "b", "c", "a", "b", "c"];
|
|
397
|
+
*
|
|
398
|
+
* uniquify(array); // ["a", "b", "c"]
|
|
399
|
+
* ```
|
|
400
|
+
*
|
|
401
|
+
* Prototype Example:
|
|
402
|
+
* ```typescript
|
|
403
|
+
* import "@telperion/js-utils/array/proto/uniquify";
|
|
404
|
+
*
|
|
405
|
+
* const array = ["a", "b", "c", "a", "b", "c"];
|
|
406
|
+
*
|
|
407
|
+
* array.uniquify(); // ["a", "b", "c"]
|
|
408
|
+
* ```
|
|
409
|
+
* * * *
|
|
410
|
+
* @return The new uniquified array
|
|
411
|
+
*/
|
|
412
|
+
function uniquify(array) {
|
|
413
|
+
return [...new Set(array)];
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
/**
|
|
417
|
+
* #### Uniquify By Key
|
|
418
|
+
*
|
|
419
|
+
* Removes objects from the array which the value of its specifed key included before by another
|
|
420
|
+
*
|
|
421
|
+
* * * *
|
|
422
|
+
* Example:
|
|
423
|
+
* ```typescript
|
|
424
|
+
* import { uniquifyByKey } "@telperion/js-utils/array";
|
|
425
|
+
*
|
|
426
|
+
* const array = [{a: 1}, {a: 1}, {a: 2}, {a: 3}, {a: 3}, {a: 4}];
|
|
427
|
+
*
|
|
428
|
+
* uniquifyByKey(array, 'a'); // [{a: 1}, {a: 2}, {a: 3}, {a: 4}]
|
|
429
|
+
* ```
|
|
430
|
+
*
|
|
431
|
+
* Prototype Example:
|
|
432
|
+
* ```typescript
|
|
433
|
+
* import "@telperion/js-utils/array/proto/uniquify-by-key";
|
|
434
|
+
*
|
|
435
|
+
* const array = [{a: 1}, {a: 1}, {a: 2}, {a: 3}, {a: 3}, {a: 4}];
|
|
436
|
+
*
|
|
437
|
+
* array.uniquifyByKey('a'); // [{a: 1}, {a: 2}, {a: 3}, {a: 4}]
|
|
438
|
+
* ```
|
|
439
|
+
* * * *
|
|
440
|
+
* @param array Collection of the objects
|
|
441
|
+
* @param key Key to search the value on
|
|
442
|
+
* @return The new uniquified array
|
|
443
|
+
*/
|
|
444
|
+
function uniquifyByKey(array, key) {
|
|
445
|
+
const cache = new Set();
|
|
446
|
+
return array.filter(item => {
|
|
447
|
+
if (cache.has(item[key])) {
|
|
448
|
+
return false;
|
|
449
|
+
}
|
|
450
|
+
cache.add(item[key]);
|
|
451
|
+
return true;
|
|
452
|
+
});
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
456
|
+
const PROPS_TO_FILTER = ['constructor'];
|
|
457
|
+
/**
|
|
458
|
+
* #### Mixin
|
|
459
|
+
*
|
|
460
|
+
* Creates a class that combines two classes using the mixin pattern.
|
|
461
|
+
* The resulting class extends the first class and includes all properties and methods from the second class.
|
|
462
|
+
*
|
|
463
|
+
* * * *
|
|
464
|
+
* Example:
|
|
465
|
+
* ```typescript
|
|
466
|
+
* import { mixin } from "@telperion/js-utils/class";
|
|
467
|
+
*
|
|
468
|
+
* class Timestamped {
|
|
469
|
+
* timestamp = Date.now();
|
|
470
|
+
* getAge() {
|
|
471
|
+
* return Date.now() - this.timestamp;
|
|
472
|
+
* }
|
|
473
|
+
* }
|
|
474
|
+
*
|
|
475
|
+
* class User {
|
|
476
|
+
* constructor(public name: string) {}
|
|
477
|
+
* greet() {
|
|
478
|
+
* return `Hello, ${this.name}`;
|
|
479
|
+
* }
|
|
480
|
+
* }
|
|
481
|
+
*
|
|
482
|
+
* class TimestampedUser extends mixin(User, Timestamped) {}
|
|
483
|
+
*
|
|
484
|
+
* const user = new TimestampedUser(['John'], []);
|
|
485
|
+
* console.log(user.greet()); // "Hello, John"
|
|
486
|
+
* console.log(user.getAge()); // Time since creation
|
|
487
|
+
* ```
|
|
488
|
+
*
|
|
489
|
+
* @param Base The base class to extend
|
|
490
|
+
* @param Mixin The mixin class to combine
|
|
491
|
+
* @returns A new class that combines both classes
|
|
492
|
+
*/
|
|
493
|
+
function mixin(Base, Mixin) {
|
|
494
|
+
// @ts-ignore - TypeScript doesn't fully understand the mixin pattern
|
|
495
|
+
class Mixed extends Base {
|
|
496
|
+
constructor(baseArgs, mixinArgs) {
|
|
497
|
+
super(...baseArgs);
|
|
498
|
+
// Create an instance of the mixin class and copy its properties
|
|
499
|
+
const mixinInstance = new Mixin(...mixinArgs);
|
|
500
|
+
for (const key of Reflect.ownKeys(mixinInstance)) {
|
|
501
|
+
this[key] = mixinInstance[key];
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
// Copy prototype methods from mixin to the mixed class
|
|
506
|
+
const mixinKeys = Reflect.ownKeys(Mixin.prototype).filter((key) => !PROPS_TO_FILTER.includes(key));
|
|
507
|
+
for (const prop of mixinKeys) {
|
|
508
|
+
Object.defineProperty(Mixed.prototype, prop, Object.getOwnPropertyDescriptor(Mixin.prototype, prop));
|
|
509
|
+
}
|
|
510
|
+
return Mixed;
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
function call(func) {
|
|
514
|
+
return func();
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
/**
|
|
518
|
+
* #### Noop function
|
|
519
|
+
* * * *
|
|
520
|
+
* Example usage:
|
|
521
|
+
* ```typescript
|
|
522
|
+
* import { noop } from "@telperion/js-utils/function";
|
|
523
|
+
*
|
|
524
|
+
* noop();
|
|
525
|
+
* document.onload = noop;
|
|
526
|
+
* ```
|
|
527
|
+
* Static usage example:
|
|
528
|
+
* ```typescript
|
|
529
|
+
* import "@telperion/js-utils/function/static/noop";
|
|
530
|
+
*
|
|
531
|
+
* Function.noop();
|
|
532
|
+
* document.onload = Function.noop;
|
|
533
|
+
* ```
|
|
534
|
+
* * * *
|
|
535
|
+
*/
|
|
536
|
+
function noop() { }
|
|
537
|
+
|
|
538
|
+
/**
|
|
539
|
+
* #### Open Promise
|
|
540
|
+
* A promise constructor to resolve or reject from outside
|
|
541
|
+
*
|
|
542
|
+
* @deprecated Use the native `Promise.withResolvers()` instead, which provides the same functionality.
|
|
543
|
+
*
|
|
544
|
+
* * * *
|
|
545
|
+
* Example with native alternative:
|
|
546
|
+
* ```typescript
|
|
547
|
+
* // Instead of:
|
|
548
|
+
* // import { OpenPromise } from "@telperion/js-utils";
|
|
549
|
+
* // const aPromiseWillBeResolvedLater = new OpenPromise();
|
|
550
|
+
*
|
|
551
|
+
* // Use:
|
|
552
|
+
* const { promise, resolve, reject } = Promise.withResolvers();
|
|
553
|
+
*
|
|
554
|
+
* promise.then(val => console.log(val));
|
|
555
|
+
* ...
|
|
556
|
+
* ...
|
|
557
|
+
*
|
|
558
|
+
* resolve({x: 1});
|
|
559
|
+
* ```
|
|
560
|
+
*
|
|
561
|
+
* Legacy Example:
|
|
562
|
+
* ```typescript
|
|
563
|
+
* import { OpenPromise } from "@telperion/js-utils";
|
|
564
|
+
*
|
|
565
|
+
* const aPromiseWillBeResolvedLater = new OpenPromise();
|
|
566
|
+
*
|
|
567
|
+
* aPromiseWillBeResolvedLater.then(val => console.log(val));
|
|
568
|
+
* // aPromiseWillBeResolvedLater.finished // false
|
|
569
|
+
* ...
|
|
570
|
+
* ...
|
|
571
|
+
*
|
|
572
|
+
* aPromiseWillBeResolvedLater.resolve({x: 1});
|
|
573
|
+
* // aPromiseWillBeResolvedLater.finished // true
|
|
574
|
+
* ```
|
|
575
|
+
* * * *
|
|
576
|
+
* @template T typeof the value which is going to be resolved
|
|
577
|
+
*/
|
|
578
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
579
|
+
class OpenPromise extends Promise {
|
|
580
|
+
static #resolverHolder = noop;
|
|
581
|
+
static #rejectorHolder = noop;
|
|
582
|
+
#resolved = false;
|
|
583
|
+
#rejected = false;
|
|
584
|
+
#resolver = noop;
|
|
585
|
+
#rejector = noop;
|
|
586
|
+
/**
|
|
587
|
+
* Returns whether is the promise resolved
|
|
588
|
+
*/
|
|
589
|
+
get resolved() {
|
|
590
|
+
return this.#resolved;
|
|
591
|
+
}
|
|
592
|
+
/**
|
|
593
|
+
* Returns whether is the promise rejected
|
|
594
|
+
*/
|
|
595
|
+
get rejected() {
|
|
596
|
+
return this.#rejected;
|
|
597
|
+
}
|
|
598
|
+
/**
|
|
599
|
+
* Returns whether is the promise finished
|
|
600
|
+
*/
|
|
601
|
+
get finished() {
|
|
602
|
+
return this.#resolved || this.#rejected;
|
|
603
|
+
}
|
|
604
|
+
/**
|
|
605
|
+
* Open Promise Constructor
|
|
606
|
+
*/
|
|
607
|
+
constructor(executor = noop) {
|
|
608
|
+
super((resolve, reject) => {
|
|
609
|
+
OpenPromise.#resolverHolder = resolve;
|
|
610
|
+
OpenPromise.#rejectorHolder = reject;
|
|
611
|
+
executor(resolve, reject);
|
|
612
|
+
});
|
|
613
|
+
this.#resolver = OpenPromise.#resolverHolder;
|
|
614
|
+
this.#rejector = OpenPromise.#rejectorHolder;
|
|
615
|
+
OpenPromise.#resolverHolder = noop;
|
|
616
|
+
OpenPromise.#rejectorHolder = noop;
|
|
617
|
+
}
|
|
618
|
+
/**
|
|
619
|
+
* Resolves promise
|
|
620
|
+
* @param value Value to resolve the promise
|
|
621
|
+
*/
|
|
622
|
+
resolve(...args) {
|
|
623
|
+
if (this.finished)
|
|
624
|
+
throw new Error('Promise is already finished');
|
|
625
|
+
this.#resolved = true;
|
|
626
|
+
this.#resolver(...args);
|
|
627
|
+
}
|
|
628
|
+
/**
|
|
629
|
+
* Rejects promise
|
|
630
|
+
* @param reason Error to reject promise
|
|
631
|
+
*/
|
|
632
|
+
reject(...args) {
|
|
633
|
+
if (this.finished)
|
|
634
|
+
throw new Error('Promise is already finished');
|
|
635
|
+
this.#rejected = true;
|
|
636
|
+
this.#rejector(...args);
|
|
637
|
+
}
|
|
638
|
+
/**
|
|
639
|
+
* Binds a promise to the inner promise to resolve or reject with it
|
|
640
|
+
* @param promise A promise to bind inner promise
|
|
641
|
+
*/
|
|
642
|
+
bindTo(promise) {
|
|
643
|
+
promise
|
|
644
|
+
.then(e => { this.resolve(e); })
|
|
645
|
+
.catch(e => this.reject(e));
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
// Defined here for documentation
|
|
650
|
+
function promiseTimeoutInitializer() {
|
|
651
|
+
const REFERANCE_CACHE = new WeakMap();
|
|
652
|
+
const KEY_CACHE = new Map();
|
|
653
|
+
const timeout = (time, value, key) => {
|
|
654
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
655
|
+
const openPromise = new OpenPromise();
|
|
656
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
657
|
+
const id = setTimeout(() => {
|
|
658
|
+
openPromise.resolve(value);
|
|
659
|
+
}, time);
|
|
660
|
+
const cacheObject = { openPromise, id };
|
|
661
|
+
REFERANCE_CACHE.set(openPromise, cacheObject);
|
|
662
|
+
if (key) {
|
|
663
|
+
KEY_CACHE.set(key, cacheObject);
|
|
664
|
+
}
|
|
665
|
+
return openPromise;
|
|
666
|
+
};
|
|
667
|
+
Object.defineProperties(timeout, {
|
|
668
|
+
FINISHED_ALREADY: {
|
|
669
|
+
writable: false,
|
|
670
|
+
value: Symbol('Timeout finished already')
|
|
671
|
+
},
|
|
672
|
+
IDENTIFIER_NOT_FOUND: {
|
|
673
|
+
writable: false,
|
|
674
|
+
value: Symbol('Timeout not found via identifier')
|
|
675
|
+
},
|
|
676
|
+
TIMEOUT_CANCELLED: {
|
|
677
|
+
writable: false,
|
|
678
|
+
value: Symbol('Timeout cancelled')
|
|
679
|
+
}
|
|
680
|
+
});
|
|
681
|
+
timeout.cancel = async (identifier, error) => {
|
|
682
|
+
const { FINISHED_ALREADY, IDENTIFIER_NOT_FOUND, TIMEOUT_CANCELLED } = timeout;
|
|
683
|
+
if (typeof error === 'undefined') {
|
|
684
|
+
error = TIMEOUT_CANCELLED;
|
|
685
|
+
}
|
|
686
|
+
let cache;
|
|
687
|
+
if (typeof identifier === 'symbol') {
|
|
688
|
+
cache = KEY_CACHE.get(identifier);
|
|
689
|
+
}
|
|
690
|
+
else {
|
|
691
|
+
cache = REFERANCE_CACHE.get(identifier);
|
|
692
|
+
}
|
|
693
|
+
if (!cache) {
|
|
694
|
+
throw IDENTIFIER_NOT_FOUND;
|
|
695
|
+
}
|
|
696
|
+
if (cache.openPromise.finished) {
|
|
697
|
+
throw FINISHED_ALREADY;
|
|
698
|
+
}
|
|
699
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
700
|
+
clearTimeout(cache.id);
|
|
701
|
+
cache.openPromise.reject(error);
|
|
702
|
+
if (typeof identifier === 'symbol') {
|
|
703
|
+
KEY_CACHE.delete(identifier);
|
|
704
|
+
}
|
|
705
|
+
};
|
|
706
|
+
return timeout;
|
|
707
|
+
}
|
|
708
|
+
/**
|
|
709
|
+
* #### Promise Timeout
|
|
710
|
+
* Returns a promise which resolves after given time
|
|
711
|
+
*
|
|
712
|
+
* * * *
|
|
713
|
+
* Example:
|
|
714
|
+
* ```typescript
|
|
715
|
+
* import { timeout } from "@telperion/js-utils/promise";
|
|
716
|
+
*
|
|
717
|
+
* timeout(1000);
|
|
718
|
+
* .then(() => console.log("will be logged after a second"));
|
|
719
|
+
* ```
|
|
720
|
+
* Example with a resolve value:
|
|
721
|
+
* ```typescript
|
|
722
|
+
* import { timeout } from "@telperion/js-utils/promise";
|
|
723
|
+
*
|
|
724
|
+
* timeout(1000, "foo");
|
|
725
|
+
* .then(val => console.log("will log 'foo' after a second", val));
|
|
726
|
+
* ```
|
|
727
|
+
* Can be used in promise chaining:
|
|
728
|
+
* ```typescript
|
|
729
|
+
* import { timeout } from "@telperion/js-utils/promise";
|
|
730
|
+
*
|
|
731
|
+
* fetch("http://localhost:8080/anEndpoint") // Fetch something
|
|
732
|
+
* .then(val => timeout(1000, val)) // Wait a second after response
|
|
733
|
+
* .then(val => {
|
|
734
|
+
* ...do something else
|
|
735
|
+
* });
|
|
736
|
+
* ```
|
|
737
|
+
* Static usage example:
|
|
738
|
+
* ```typescript
|
|
739
|
+
* import "@telperion/js-utils/promise/static/timeout";
|
|
740
|
+
*
|
|
741
|
+
* Promise.timeout(1000);
|
|
742
|
+
* .then(() => console.log("will be logged after a second"));
|
|
743
|
+
* ```
|
|
744
|
+
* * * *
|
|
745
|
+
* @see PromiseTimeoutFunction#cancel for cancelling
|
|
746
|
+
*/
|
|
747
|
+
const timeout = promiseTimeoutInitializer();
|
|
748
|
+
|
|
749
|
+
/**
|
|
750
|
+
* Converts a promise or a value to a promise to ensure that the return value is a promise
|
|
751
|
+
*
|
|
752
|
+
* @param promiseOrValue The promise or value to convert to a promise
|
|
753
|
+
* @returns The promise of the given promise or value
|
|
754
|
+
*/
|
|
755
|
+
async function promisify(promiseOrValue) {
|
|
756
|
+
return await promiseOrValue;
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
const DEFAULT_DEBOUNCE_TIME = 180;
|
|
760
|
+
const KEY_REFERENCES = new Map();
|
|
761
|
+
const CACHE$2 = new Map();
|
|
762
|
+
function debounceWithKey(key, callback, time = DEFAULT_DEBOUNCE_TIME, thisObject = null, ...args) {
|
|
763
|
+
let timeoutPromise;
|
|
764
|
+
let promise;
|
|
765
|
+
if (CACHE$2.has(key)) {
|
|
766
|
+
const stocked = CACHE$2.get(key);
|
|
767
|
+
timeoutPromise = stocked.timeout;
|
|
768
|
+
promise = stocked.promise;
|
|
769
|
+
timeout.cancel(timeoutPromise);
|
|
770
|
+
}
|
|
771
|
+
else {
|
|
772
|
+
promise = new OpenPromise();
|
|
773
|
+
}
|
|
774
|
+
timeoutPromise = timeout(time);
|
|
775
|
+
CACHE$2.set(key, { promise, timeout: timeoutPromise });
|
|
776
|
+
timeoutPromise
|
|
777
|
+
.then(() => {
|
|
778
|
+
CACHE$2.delete(key);
|
|
779
|
+
const result = callback.call(thisObject, ...args);
|
|
780
|
+
promise.bindTo(promisify(result));
|
|
781
|
+
return result;
|
|
782
|
+
})
|
|
783
|
+
.catch(err => {
|
|
784
|
+
if (err !== timeout.TIMEOUT_CANCELLED) {
|
|
785
|
+
promise.reject(err);
|
|
786
|
+
}
|
|
787
|
+
});
|
|
788
|
+
return promise;
|
|
789
|
+
}
|
|
790
|
+
/**
|
|
791
|
+
* #### Debounces a function that delays invoking until after given time have elapsed since the last time the debounced function was invoked
|
|
792
|
+
*
|
|
793
|
+
* * * *
|
|
794
|
+
* Example usage:
|
|
795
|
+
* ```typescript
|
|
796
|
+
* import { debounce } from "@telperion/js-utils/promise";
|
|
797
|
+
*
|
|
798
|
+
* function foo() {
|
|
799
|
+
* console.log("hello");
|
|
800
|
+
* }
|
|
801
|
+
*
|
|
802
|
+
* for (let i = 0; i < 5; i++) {
|
|
803
|
+
* debounce(foo);
|
|
804
|
+
* }
|
|
805
|
+
*
|
|
806
|
+
* // logs "hello" only once
|
|
807
|
+
* ```
|
|
808
|
+
*
|
|
809
|
+
* Static usage example:
|
|
810
|
+
* ```typescript
|
|
811
|
+
* import "@telperion/js-utils/dist/as-proto/debounce";
|
|
812
|
+
*
|
|
813
|
+
* function foo() {
|
|
814
|
+
* console.log("hello");
|
|
815
|
+
* }
|
|
816
|
+
*
|
|
817
|
+
* for (let i = 0; i < 5; i++) {
|
|
818
|
+
* foo.debounce();
|
|
819
|
+
* }
|
|
820
|
+
*
|
|
821
|
+
* // logs "hello" only once
|
|
822
|
+
* ```
|
|
823
|
+
* * * *
|
|
824
|
+
* @param callback The function to execute only last of multiple execute requests by given time
|
|
825
|
+
* @param [time = 180] Time for debouncing
|
|
826
|
+
* @param [thisObject = null] This object to execute the callback function with
|
|
827
|
+
* @param args Function arguments
|
|
828
|
+
* @return A promise which resolves right after the debouncing sequence has been finished
|
|
829
|
+
*/
|
|
830
|
+
function debounce(callback, time = DEFAULT_DEBOUNCE_TIME, thisObject = null, ...args) {
|
|
831
|
+
if (!KEY_REFERENCES.has(callback)) {
|
|
832
|
+
KEY_REFERENCES.set(callback, Symbol());
|
|
833
|
+
}
|
|
834
|
+
return debounceWithKey(KEY_REFERENCES.get(callback), callback, time, thisObject, ...args);
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
/**
|
|
838
|
+
* #### Delays the execution of the passed function to increase the render performance
|
|
839
|
+
*
|
|
840
|
+
* * * *
|
|
841
|
+
* Example usage:
|
|
842
|
+
* ```typescript
|
|
843
|
+
* import { defer } from "@telperion/js-utils/function";
|
|
844
|
+
*
|
|
845
|
+
* defer(() => aFunctionToDefer())
|
|
846
|
+
* .then(res => ...)
|
|
847
|
+
* .catch(err => ...);
|
|
848
|
+
* ```
|
|
849
|
+
* Example as promise static method
|
|
850
|
+
* ```typescript
|
|
851
|
+
* import "@telperion/js-utils/function/static/defer";
|
|
852
|
+
*
|
|
853
|
+
* Function.defer(() => aFunctionToDefer())
|
|
854
|
+
* .then(res => ...)
|
|
855
|
+
* .catch(err => ...);
|
|
856
|
+
* ```
|
|
857
|
+
* * * *
|
|
858
|
+
* @param callback Callback function to be executed
|
|
859
|
+
* @typeparam T type of the return value of the callback
|
|
860
|
+
* @returns A promise which resolves with the value of the callback right after the execution
|
|
861
|
+
*/
|
|
862
|
+
function defer(callback = noop) {
|
|
863
|
+
return new Promise((resolve, reject) => {
|
|
864
|
+
setTimeout(() => {
|
|
865
|
+
try {
|
|
866
|
+
const returnValue = callback();
|
|
867
|
+
resolve(returnValue);
|
|
868
|
+
}
|
|
869
|
+
catch (error) {
|
|
870
|
+
reject(error);
|
|
871
|
+
}
|
|
872
|
+
}, 0);
|
|
873
|
+
});
|
|
874
|
+
}
|
|
875
|
+
|
|
876
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
877
|
+
// Symbols to protect overriding private methods & properties of SmartMap Class
|
|
878
|
+
const KEY__WEAKMAP = Symbol('SmartMap.weakMap');
|
|
879
|
+
/**
|
|
880
|
+
* ### SmartMap
|
|
881
|
+
*
|
|
882
|
+
* Like WeakMap but can also store values using primitive keys
|
|
883
|
+
*
|
|
884
|
+
* * * *
|
|
885
|
+
* Example usage:
|
|
886
|
+
* ```typescript
|
|
887
|
+
* import { SmartMap } from "@telperion/js-utils";
|
|
888
|
+
*
|
|
889
|
+
* const aMap = new SmartMap();
|
|
890
|
+
*
|
|
891
|
+
* aMap.set("foo", "foo");
|
|
892
|
+
* aMap.set(1, "thales rocks");
|
|
893
|
+
*
|
|
894
|
+
* console.log(aMap.size) // 2
|
|
895
|
+
*
|
|
896
|
+
* aMap.set({}, "thales rocks again");
|
|
897
|
+
* console.log(aMap.size) // 2
|
|
898
|
+
*
|
|
899
|
+
* const anObject = {};
|
|
900
|
+
* aMap.set(anObject, "thales rocks again and again");
|
|
901
|
+
* console.log(aMap.size) // 3
|
|
902
|
+
* console.log(aMap.get(anObject)) // "thales rocks again and again"
|
|
903
|
+
* ```
|
|
904
|
+
* * * *
|
|
905
|
+
* @template K Typeof Key
|
|
906
|
+
* @template V Typeof Value
|
|
907
|
+
*/
|
|
908
|
+
class SmartMap extends Map {
|
|
909
|
+
/**
|
|
910
|
+
* Symbol refference of weakmap property
|
|
911
|
+
*/
|
|
912
|
+
static KEY__WEAKMAP = KEY__WEAKMAP;
|
|
913
|
+
/**
|
|
914
|
+
* Allowed key types to store them in a weakmap instance
|
|
915
|
+
*/
|
|
916
|
+
static TYPES_TO_STORE_IN_WEAKMAP = Object.freeze(['object', 'function']);
|
|
917
|
+
/**
|
|
918
|
+
* Returns a boolean whether if the key is storable in weakmap or not
|
|
919
|
+
*
|
|
920
|
+
* @param key Key to check if it can be stored in weakmap
|
|
921
|
+
* @returns A boolean whether if the key is storable in weakmap or not
|
|
922
|
+
*/
|
|
923
|
+
static isStorableInWeakMap(key) {
|
|
924
|
+
return key !== null && SmartMap.TYPES_TO_STORE_IN_WEAKMAP.some(_key => typeof key === _key);
|
|
925
|
+
}
|
|
926
|
+
/**
|
|
927
|
+
* The WeakMap object to store values with associated non-primitive (object) keys
|
|
928
|
+
*/
|
|
929
|
+
[KEY__WEAKMAP] = new WeakMap();
|
|
930
|
+
/**
|
|
931
|
+
* Returns a boolean asserting whether a value has been associated to the key in the SmartMap object or not.
|
|
932
|
+
*
|
|
933
|
+
* @param key The key of the element to test for presence in the SmartMap object.
|
|
934
|
+
* @returns A boolean asserting whether a value has been associated to the key in the SmartMap object or not.
|
|
935
|
+
*/
|
|
936
|
+
has(key) {
|
|
937
|
+
if (SmartMap.isStorableInWeakMap(key)) {
|
|
938
|
+
return this[KEY__WEAKMAP].has(key);
|
|
939
|
+
}
|
|
940
|
+
return super.has(key);
|
|
941
|
+
}
|
|
942
|
+
/**
|
|
943
|
+
* Returns the value associated to the key, or undefined if there is none.
|
|
944
|
+
*
|
|
945
|
+
* @param key The key of the element to return from the SmartMap object.
|
|
946
|
+
* @returns The value associated to the key
|
|
947
|
+
*/
|
|
948
|
+
get(key) {
|
|
949
|
+
if (SmartMap.isStorableInWeakMap(key)) {
|
|
950
|
+
return this[KEY__WEAKMAP].get(key);
|
|
951
|
+
}
|
|
952
|
+
return super.get(key);
|
|
953
|
+
}
|
|
954
|
+
/**
|
|
955
|
+
* Sets the value for the key in the SmartMap object.
|
|
956
|
+
*
|
|
957
|
+
* @param key The key of the element to add to the SmartMap object.
|
|
958
|
+
* @param value The value of the element to add to the SmartMap object.
|
|
959
|
+
* @returns Returns the SmartMap object.
|
|
960
|
+
*/
|
|
961
|
+
set(key, value) {
|
|
962
|
+
if (SmartMap.isStorableInWeakMap(key)) {
|
|
963
|
+
this[KEY__WEAKMAP].set(key, value);
|
|
964
|
+
}
|
|
965
|
+
else {
|
|
966
|
+
super.set(key, value);
|
|
967
|
+
}
|
|
968
|
+
return this;
|
|
969
|
+
}
|
|
970
|
+
/**
|
|
971
|
+
* Removes the associated value from the SmartMap object,
|
|
972
|
+
*
|
|
973
|
+
* @param key The key of the element to remove from the SmartMap object.
|
|
974
|
+
* @returns `true` if an element in the SmartMap object existed and has been removed, or `false` if the element does not exist. `SmartMap.prototype.has(key)` will return false afterwards.
|
|
975
|
+
*/
|
|
976
|
+
delete(key) {
|
|
977
|
+
return this[KEY__WEAKMAP].delete(key) || super.delete(key);
|
|
978
|
+
}
|
|
979
|
+
}
|
|
980
|
+
|
|
981
|
+
/**
|
|
982
|
+
* Cache object to store previously created functions
|
|
983
|
+
*/
|
|
984
|
+
const CACHE$1 = new SmartMap();
|
|
985
|
+
/**
|
|
986
|
+
* ### Function Of
|
|
987
|
+
*
|
|
988
|
+
* Creates a function which returns the value given
|
|
989
|
+
*
|
|
990
|
+
* * * *
|
|
991
|
+
* Example:
|
|
992
|
+
* ```typescript
|
|
993
|
+
* import { of } from "@telperion/js-utils/function";
|
|
994
|
+
*
|
|
995
|
+
* const base = [1, 2, 5, {}, "x", "y"];
|
|
996
|
+
* const mapTo = of("thales rocks");
|
|
997
|
+
*
|
|
998
|
+
* const mapped = base.map(mapTo);
|
|
999
|
+
* // ["thales rocks", "thales rocks", "thales rocks", "thales rocks", "thales rocks", "thales rocks"]
|
|
1000
|
+
* ```
|
|
1001
|
+
* Function Static Example:
|
|
1002
|
+
* ```typescript
|
|
1003
|
+
* import "@telperion/js-utils/function/static/of";
|
|
1004
|
+
*
|
|
1005
|
+
* const base = [1, 2, 5, {}, "x", "y"];
|
|
1006
|
+
* const mapTo = Function.of("thales rocks");
|
|
1007
|
+
*
|
|
1008
|
+
* const mapped = base.map(mapTo);
|
|
1009
|
+
* // ["thales rocks", "thales rocks", "thales rocks", "thales rocks", "thales rocks", "thales rocks"]
|
|
1010
|
+
* ```
|
|
1011
|
+
* * * *
|
|
1012
|
+
* @param returnValue The value which created function returns
|
|
1013
|
+
* @returns A function which returns the `returnValue`
|
|
1014
|
+
*/
|
|
1015
|
+
function of(returnValue) {
|
|
1016
|
+
if (!CACHE$1.has(returnValue)) {
|
|
1017
|
+
CACHE$1.set(returnValue, function () { return returnValue; });
|
|
1018
|
+
}
|
|
1019
|
+
return CACHE$1.get(returnValue);
|
|
1020
|
+
}
|
|
1021
|
+
|
|
1022
|
+
/**
|
|
1023
|
+
* #### Map Merge
|
|
1024
|
+
*
|
|
1025
|
+
* Merges two maps
|
|
1026
|
+
*
|
|
1027
|
+
* * * *
|
|
1028
|
+
* Example usage:
|
|
1029
|
+
* ```typescript
|
|
1030
|
+
* import { merge } from "@telperion/js-utils/map";
|
|
1031
|
+
*
|
|
1032
|
+
* const first = new Map();
|
|
1033
|
+
* first.set("a", 1);
|
|
1034
|
+
*
|
|
1035
|
+
* const second = new Map();
|
|
1036
|
+
* second.set("b", 2);
|
|
1037
|
+
*
|
|
1038
|
+
* merge(first, second); // [{key: "a", value: 1}, {key: "b", value: 2}]
|
|
1039
|
+
* ```
|
|
1040
|
+
* * * *
|
|
1041
|
+
* @param first First map
|
|
1042
|
+
* @param second Second map
|
|
1043
|
+
* @returns A new merged map
|
|
1044
|
+
*/
|
|
1045
|
+
function merge(first, second) {
|
|
1046
|
+
const newMap = new Map(first);
|
|
1047
|
+
for (const key of second.keys()) {
|
|
1048
|
+
newMap.set(key, second.get(key));
|
|
1049
|
+
}
|
|
1050
|
+
return newMap;
|
|
1051
|
+
}
|
|
1052
|
+
|
|
1053
|
+
/**
|
|
1054
|
+
* #### Limits the value by specified range
|
|
1055
|
+
*
|
|
1056
|
+
* * * *
|
|
1057
|
+
* Example usage:
|
|
1058
|
+
* ```typescript
|
|
1059
|
+
* import { minMax } from "@telperion/js-utils/math";
|
|
1060
|
+
*
|
|
1061
|
+
* const limitedValue = minMax(200, 300, Math.random() * 1000);
|
|
1062
|
+
* ```
|
|
1063
|
+
*
|
|
1064
|
+
* Example as Math static:
|
|
1065
|
+
* ```typescript
|
|
1066
|
+
* import "@telperion/js-utils/math/static/min-max";
|
|
1067
|
+
*
|
|
1068
|
+
* const limitedValue = Math.minMax(200, 300, Math.random() * 1000);
|
|
1069
|
+
* ```
|
|
1070
|
+
* * * *
|
|
1071
|
+
* @param min minimum limit
|
|
1072
|
+
* @param max maximum limit
|
|
1073
|
+
* @param value value to limit
|
|
1074
|
+
* @return the limited value *(returns NaN if an argument is NaN)*
|
|
1075
|
+
*/
|
|
1076
|
+
function minMax(min, max, value) {
|
|
1077
|
+
return Math.max(Math.min(max, value), min);
|
|
1078
|
+
}
|
|
1079
|
+
|
|
1080
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
1081
|
+
/**
|
|
1082
|
+
* A placeholder to symbolize the cloning progress of `objectToClone` hasn't finished yet
|
|
1083
|
+
*/
|
|
1084
|
+
const CLONING_IN_PROGRESS = Symbol('Cloning In Progress');
|
|
1085
|
+
/**
|
|
1086
|
+
* Function Map to clone specific objects types
|
|
1087
|
+
*/
|
|
1088
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
1089
|
+
const DEFAULT_INSTANCE_CLONERS = new Map();
|
|
1090
|
+
// Set cloning functions
|
|
1091
|
+
DEFAULT_INSTANCE_CLONERS.set(Array, arrayCloner);
|
|
1092
|
+
DEFAULT_INSTANCE_CLONERS.set(Map, mapCloner);
|
|
1093
|
+
DEFAULT_INSTANCE_CLONERS.set(Set, setCloner);
|
|
1094
|
+
DEFAULT_INSTANCE_CLONERS.set(Object, objectCloner);
|
|
1095
|
+
/**
|
|
1096
|
+
* Default Array Cloner
|
|
1097
|
+
*/
|
|
1098
|
+
function arrayCloner(objectToClone, options, internalData) {
|
|
1099
|
+
return objectToClone.map((val, i, arr) => {
|
|
1100
|
+
if (options.propsToRefer.indexOf(i) > -1) {
|
|
1101
|
+
return val;
|
|
1102
|
+
}
|
|
1103
|
+
return cloner(val, options, { ...internalData, parent: arr, key: i });
|
|
1104
|
+
});
|
|
1105
|
+
}
|
|
1106
|
+
/**
|
|
1107
|
+
* Default Map Cloner
|
|
1108
|
+
*/
|
|
1109
|
+
function mapCloner(objectToClone, options, internalData) {
|
|
1110
|
+
const newMap = new Map();
|
|
1111
|
+
intersection(options.propsToRefer, Array.from(objectToClone.keys())).forEach(key => {
|
|
1112
|
+
newMap.set(key, objectToClone.get(key));
|
|
1113
|
+
});
|
|
1114
|
+
difference(Array.from(objectToClone.keys()), options.propsToRefer).forEach(key => {
|
|
1115
|
+
newMap.set(key, cloner(objectToClone.get(key), options, { ...internalData, key, parent: newMap }));
|
|
1116
|
+
});
|
|
1117
|
+
return newMap;
|
|
1118
|
+
}
|
|
1119
|
+
/**
|
|
1120
|
+
* Default Set Cloner
|
|
1121
|
+
*/
|
|
1122
|
+
function setCloner(objectToClone) {
|
|
1123
|
+
return new Set(objectToClone);
|
|
1124
|
+
}
|
|
1125
|
+
/**
|
|
1126
|
+
* Default Object Cloner
|
|
1127
|
+
*/
|
|
1128
|
+
function objectCloner(objectToClone, options, internalData) {
|
|
1129
|
+
const { propsToRefer } = options;
|
|
1130
|
+
/**
|
|
1131
|
+
* New Object Clone
|
|
1132
|
+
*/
|
|
1133
|
+
const _clone = {};
|
|
1134
|
+
/**
|
|
1135
|
+
* Object cloner
|
|
1136
|
+
* @param key incoming property of object to clone
|
|
1137
|
+
*/
|
|
1138
|
+
function cloneObject(key) {
|
|
1139
|
+
_clone[key] = cloner(objectToClone[key], options, { ...internalData, parent: _clone, key });
|
|
1140
|
+
}
|
|
1141
|
+
const allProperties = [...Object.keys(objectToClone), ...Object.getOwnPropertySymbols(objectToClone)];
|
|
1142
|
+
// Pass unwanted props
|
|
1143
|
+
intersection(propsToRefer, allProperties).forEach(prop => {
|
|
1144
|
+
_clone[prop] = objectToClone[prop];
|
|
1145
|
+
});
|
|
1146
|
+
// Clone remaining properties
|
|
1147
|
+
difference(allProperties, propsToRefer).forEach(cloneObject);
|
|
1148
|
+
return _clone;
|
|
1149
|
+
}
|
|
1150
|
+
/**
|
|
1151
|
+
* #### Deep Clone
|
|
1152
|
+
*
|
|
1153
|
+
* A function to recursively clone objects, arrays etc. with [cloning options]{@link ICloneOptions}
|
|
1154
|
+
*
|
|
1155
|
+
* *References Functions & Symbols by default*
|
|
1156
|
+
*
|
|
1157
|
+
* * * *
|
|
1158
|
+
* Example:
|
|
1159
|
+
* ```typescript
|
|
1160
|
+
* import { clone } from "@telperion/js-utils/object";
|
|
1161
|
+
*
|
|
1162
|
+
* const object = {a: 1, b: {c: true, d: ["x", "y"]}};
|
|
1163
|
+
*
|
|
1164
|
+
* // Clone all
|
|
1165
|
+
* const clonedObject = clone(object);
|
|
1166
|
+
* // {a: 1, b: {c: true, d: ["x", "y"]}}
|
|
1167
|
+
* // object.b.d === clonedObject.b.d // false
|
|
1168
|
+
*
|
|
1169
|
+
* // Clone all but reference "d"
|
|
1170
|
+
* const clonedObject = clone(object, {propsToRefer: ["d"]});
|
|
1171
|
+
* // {a: 1, b: {c: true, d: ["x", "y"]}}
|
|
1172
|
+
* // object.b.d === clonedObject.b.d // true
|
|
1173
|
+
* ```
|
|
1174
|
+
*
|
|
1175
|
+
* Static usage example:
|
|
1176
|
+
* ```typescript
|
|
1177
|
+
* import "@telperion/js-utils/object/static/clone";
|
|
1178
|
+
*
|
|
1179
|
+
* const object = {a: 1, b: 2};
|
|
1180
|
+
* const clonedObject = Object.clone(object); // {a: 1, b: 2}
|
|
1181
|
+
* ```
|
|
1182
|
+
* * * *
|
|
1183
|
+
* @param objectToClone Object to clone
|
|
1184
|
+
* @param options {ICloneOptions} Cloning Options
|
|
1185
|
+
* @see {@link ICloneOptions} for more information.
|
|
1186
|
+
*/
|
|
1187
|
+
function clone(objectToClone, { instancesToRefer = [], propsToRefer = [], valueFiltererToRefer = () => false, customCloners = new Map() } = {}) {
|
|
1188
|
+
/**
|
|
1189
|
+
* Whole options with defaults
|
|
1190
|
+
*/
|
|
1191
|
+
const mergedCloners = merge(customCloners, DEFAULT_INSTANCE_CLONERS);
|
|
1192
|
+
const allOptions = { instancesToRefer, propsToRefer, valueFiltererToRefer, customCloners: mergedCloners };
|
|
1193
|
+
const finalizers = [];
|
|
1194
|
+
const cloned = cloner(objectToClone, allOptions, { clonedObjects: new Map(), finalizers });
|
|
1195
|
+
finalizers.forEach(callback => { callback(); });
|
|
1196
|
+
return cloned;
|
|
1197
|
+
}
|
|
1198
|
+
function cloner(objectToClone, allOptions, internalData) {
|
|
1199
|
+
const { instancesToRefer, valueFiltererToRefer, customCloners } = allOptions;
|
|
1200
|
+
const { clonedObjects, finalizers, parent, key } = internalData;
|
|
1201
|
+
// Pass primitives & functions
|
|
1202
|
+
if (objectToClone === null || !(objectToClone instanceof Object) || objectToClone instanceof Function) {
|
|
1203
|
+
return objectToClone;
|
|
1204
|
+
}
|
|
1205
|
+
// Pass unwanted references
|
|
1206
|
+
if (instancesToRefer.some(instance => objectToClone instanceof instance)) {
|
|
1207
|
+
return objectToClone;
|
|
1208
|
+
}
|
|
1209
|
+
// Pass filterer results
|
|
1210
|
+
if (valueFiltererToRefer(objectToClone)) {
|
|
1211
|
+
return objectToClone;
|
|
1212
|
+
}
|
|
1213
|
+
// If cloned before, skip circular cloning
|
|
1214
|
+
if (clonedObjects.has(objectToClone)) {
|
|
1215
|
+
if (clonedObjects.get(objectToClone) === CLONING_IN_PROGRESS) {
|
|
1216
|
+
finalizers.push(() => {
|
|
1217
|
+
parent[key] = clonedObjects.get(objectToClone);
|
|
1218
|
+
});
|
|
1219
|
+
return null;
|
|
1220
|
+
}
|
|
1221
|
+
return clonedObjects.get(objectToClone);
|
|
1222
|
+
}
|
|
1223
|
+
// Mark object as cloning
|
|
1224
|
+
clonedObjects.set(objectToClone, CLONING_IN_PROGRESS);
|
|
1225
|
+
// Clone
|
|
1226
|
+
const customCloner = Array.from(customCloners.keys()).find(val => objectToClone instanceof val);
|
|
1227
|
+
const result = customCloners.get(customCloner)(objectToClone, allOptions, { ...internalData });
|
|
1228
|
+
clonedObjects.set(objectToClone, result);
|
|
1229
|
+
return result;
|
|
1230
|
+
}
|
|
1231
|
+
|
|
1232
|
+
/**
|
|
1233
|
+
* #### Removes `null` and `undefined` values and their keys from an object
|
|
1234
|
+
*
|
|
1235
|
+
* Additional values can be removed by passing to an array as the second argument
|
|
1236
|
+
*
|
|
1237
|
+
* * * * *
|
|
1238
|
+
* Example usage:
|
|
1239
|
+
* ```typescript
|
|
1240
|
+
* import { compact } from "@telperion/js-utils/object";
|
|
1241
|
+
*
|
|
1242
|
+
* const a = {
|
|
1243
|
+
* x: null,
|
|
1244
|
+
* y: undefined,
|
|
1245
|
+
* z: 20
|
|
1246
|
+
* };
|
|
1247
|
+
*
|
|
1248
|
+
* compact(a); // {z: 20}
|
|
1249
|
+
*
|
|
1250
|
+
* ```
|
|
1251
|
+
* Static usage example:
|
|
1252
|
+
* ```typescript
|
|
1253
|
+
* import "@telperion/js-utils/object/static/compact";
|
|
1254
|
+
*
|
|
1255
|
+
* const a = {
|
|
1256
|
+
* x: null,
|
|
1257
|
+
* y: undefined,
|
|
1258
|
+
* z: 20
|
|
1259
|
+
* };
|
|
1260
|
+
*
|
|
1261
|
+
* Object.compact(a); // {z: 20}
|
|
1262
|
+
* ```
|
|
1263
|
+
* * * *
|
|
1264
|
+
* @param object Object delete empty keys
|
|
1265
|
+
* @param additionalValuesToRemove Other values to delete
|
|
1266
|
+
* @returns Compacted object
|
|
1267
|
+
*/
|
|
1268
|
+
function compact$1(object, additionalValuesToRemove = []) {
|
|
1269
|
+
const newObject = { ...object };
|
|
1270
|
+
const valuesToRemove = [undefined, null, ...additionalValuesToRemove];
|
|
1271
|
+
for (const [key, value] of Object.entries(newObject)) {
|
|
1272
|
+
if (valuesToRemove.includes(value)) {
|
|
1273
|
+
delete newObject[key];
|
|
1274
|
+
}
|
|
1275
|
+
}
|
|
1276
|
+
return newObject;
|
|
1277
|
+
}
|
|
1278
|
+
|
|
1279
|
+
/**
|
|
1280
|
+
* #### Get deepest object in an object chain
|
|
1281
|
+
*
|
|
1282
|
+
* * * * *
|
|
1283
|
+
* Example usage:
|
|
1284
|
+
* ```typescript
|
|
1285
|
+
* import { deepest } from "@telperion/js-utils/object";
|
|
1286
|
+
*
|
|
1287
|
+
* const a = {
|
|
1288
|
+
* x: null
|
|
1289
|
+
* };
|
|
1290
|
+
*
|
|
1291
|
+
* const b = {
|
|
1292
|
+
* x: a
|
|
1293
|
+
* };
|
|
1294
|
+
*
|
|
1295
|
+
* const c = {
|
|
1296
|
+
* x: b
|
|
1297
|
+
* };
|
|
1298
|
+
*
|
|
1299
|
+
* deepest(c, 'x'); // {x: null} (a)
|
|
1300
|
+
*
|
|
1301
|
+
* ```
|
|
1302
|
+
* Static usage example:
|
|
1303
|
+
* ```typescript
|
|
1304
|
+
* import "@telperion/js-utils/object/static/deepest";
|
|
1305
|
+
*
|
|
1306
|
+
* const a = {
|
|
1307
|
+
* x: null
|
|
1308
|
+
* };
|
|
1309
|
+
*
|
|
1310
|
+
* const b = {
|
|
1311
|
+
* x: a
|
|
1312
|
+
* };
|
|
1313
|
+
*
|
|
1314
|
+
* const c = {
|
|
1315
|
+
* x: b
|
|
1316
|
+
* };
|
|
1317
|
+
*
|
|
1318
|
+
* Object.deepest(c, 'x'); // a
|
|
1319
|
+
* ```
|
|
1320
|
+
* * * *
|
|
1321
|
+
* @param object Object to deep dive
|
|
1322
|
+
* @param key key of the object which contains same type instance
|
|
1323
|
+
*/
|
|
1324
|
+
function deepest(object, key) {
|
|
1325
|
+
let obj = object;
|
|
1326
|
+
while (obj[key]) {
|
|
1327
|
+
obj = obj[key];
|
|
1328
|
+
}
|
|
1329
|
+
return obj;
|
|
1330
|
+
}
|
|
1331
|
+
|
|
1332
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
1333
|
+
function deepFreeze(object) {
|
|
1334
|
+
// Retrieve the property names defined on object
|
|
1335
|
+
const propNames = Reflect.ownKeys(object);
|
|
1336
|
+
// Freeze properties before freezing self
|
|
1337
|
+
for (const name of propNames) {
|
|
1338
|
+
const value = object[name];
|
|
1339
|
+
if ((value && typeof value === "object") || typeof value === "function") {
|
|
1340
|
+
deepFreeze(value);
|
|
1341
|
+
}
|
|
1342
|
+
}
|
|
1343
|
+
return Object.freeze(object);
|
|
1344
|
+
}
|
|
1345
|
+
|
|
1346
|
+
/**
|
|
1347
|
+
* #### Maps the values of an object using a callback function
|
|
1348
|
+
*
|
|
1349
|
+
* Similar to `Array.prototype.map`, but for objects. Iterates over own enumerable properties
|
|
1350
|
+
* and returns a new object with the same keys and transformed values.
|
|
1351
|
+
*
|
|
1352
|
+
* * * *
|
|
1353
|
+
* Example usage:
|
|
1354
|
+
* ```typescript
|
|
1355
|
+
* import { map } from "@telperion/js-utils/object";
|
|
1356
|
+
*
|
|
1357
|
+
* const obj = { a: 1, b: 2, c: 3 };
|
|
1358
|
+
*
|
|
1359
|
+
* map(obj, value => value * 2); // { a: 2, b: 4, c: 6 }
|
|
1360
|
+
* ```
|
|
1361
|
+
*
|
|
1362
|
+
* Example with key usage:
|
|
1363
|
+
* ```typescript
|
|
1364
|
+
* import { map } from "@telperion/js-utils/object";
|
|
1365
|
+
*
|
|
1366
|
+
* const obj = { x: 1, y: 2 };
|
|
1367
|
+
*
|
|
1368
|
+
* map(obj, (value, key) => `${key}_${value}`); // { x: "x_1", y: "y_2" }
|
|
1369
|
+
* ```
|
|
1370
|
+
* * * *
|
|
1371
|
+
* @param object Object to map
|
|
1372
|
+
* @param fn Callback function to transform each value, receives the value, key, and the entire object as arguments
|
|
1373
|
+
* @returns A new object with the same keys and transformed values
|
|
1374
|
+
*/
|
|
1375
|
+
function map(object, fn) {
|
|
1376
|
+
const result = {};
|
|
1377
|
+
for (const key in object) {
|
|
1378
|
+
if (object.hasOwnProperty(key)) {
|
|
1379
|
+
result[key] = fn(object[key], key, object);
|
|
1380
|
+
}
|
|
1381
|
+
}
|
|
1382
|
+
return result;
|
|
1383
|
+
}
|
|
1384
|
+
|
|
1385
|
+
/**
|
|
1386
|
+
* Chains promises in a sequential order
|
|
1387
|
+
*
|
|
1388
|
+
* @param promises The promise returning functions array in the order they should be executed
|
|
1389
|
+
* @returns A promise that resolves when all the promises in the array are resolved. The promise resolves with the value of the last promise in the array
|
|
1390
|
+
*/
|
|
1391
|
+
function chain(promises) {
|
|
1392
|
+
return promises.reduce((promise, next) => promise.then(next), Promise.resolve(null));
|
|
1393
|
+
}
|
|
1394
|
+
|
|
1395
|
+
/**
|
|
1396
|
+
* A promise which never resolves
|
|
1397
|
+
*
|
|
1398
|
+
* Example:
|
|
1399
|
+
* ```typescript
|
|
1400
|
+
* import { NEVER } from '@telperion/js-utils/promise';
|
|
1401
|
+
*
|
|
1402
|
+
* function foo(promise = NEVER) {
|
|
1403
|
+
* promise.then(val => {
|
|
1404
|
+
* ...
|
|
1405
|
+
* });
|
|
1406
|
+
* }
|
|
1407
|
+
* ```
|
|
1408
|
+
*/
|
|
1409
|
+
const NEVER = new Promise(noop);
|
|
1410
|
+
/**
|
|
1411
|
+
* Creates a promise which never resolves
|
|
1412
|
+
*
|
|
1413
|
+
* Example:
|
|
1414
|
+
* ```typescript
|
|
1415
|
+
* import { never } from '@telperion/js-utils/promise';
|
|
1416
|
+
*
|
|
1417
|
+
* function foo(promise) {
|
|
1418
|
+
* promise = promise || never();
|
|
1419
|
+
*
|
|
1420
|
+
* promise.then(val => {
|
|
1421
|
+
* ...
|
|
1422
|
+
* });
|
|
1423
|
+
* }
|
|
1424
|
+
* ```
|
|
1425
|
+
*
|
|
1426
|
+
* @returns the promise which never resolves
|
|
1427
|
+
*/
|
|
1428
|
+
function never() {
|
|
1429
|
+
return NEVER;
|
|
1430
|
+
}
|
|
1431
|
+
|
|
1432
|
+
/**
|
|
1433
|
+
* #### Revert Promise
|
|
1434
|
+
* Exchanges resolve state with rejection of a promise
|
|
1435
|
+
*
|
|
1436
|
+
* * * *
|
|
1437
|
+
* Example usage:
|
|
1438
|
+
* ```typescript
|
|
1439
|
+
* import { revert } from "@telperion/js-utils/promise";
|
|
1440
|
+
*
|
|
1441
|
+
* const errorPromise = Promise.reject(new Error('foo'));
|
|
1442
|
+
*
|
|
1443
|
+
* revert(errorPromise)
|
|
1444
|
+
* .then(err => {
|
|
1445
|
+
* console.log("this will be logged", err);
|
|
1446
|
+
* })
|
|
1447
|
+
* .catch(res => {
|
|
1448
|
+
* console.log("this won't be logged", res);
|
|
1449
|
+
* });
|
|
1450
|
+
* ```
|
|
1451
|
+
*
|
|
1452
|
+
* Proto usage example:
|
|
1453
|
+
* ```typescript
|
|
1454
|
+
* import "@telperion/js-utils/promise/proto/revert";
|
|
1455
|
+
*
|
|
1456
|
+
* Promise.reject(new Error('foo'))
|
|
1457
|
+
* .revert()
|
|
1458
|
+
* .then(err => {
|
|
1459
|
+
* console.log("this will be logged", err);
|
|
1460
|
+
* })
|
|
1461
|
+
* .catch(res => {
|
|
1462
|
+
* console.log("this won't be logged", res);
|
|
1463
|
+
* });
|
|
1464
|
+
* ```
|
|
1465
|
+
* * * *
|
|
1466
|
+
* @param promise The promise to revert its statements
|
|
1467
|
+
* @return the reverted promise
|
|
1468
|
+
*/
|
|
1469
|
+
function revert(promise) {
|
|
1470
|
+
return promise.then(res => { throw res; }, err => err);
|
|
1471
|
+
}
|
|
1472
|
+
|
|
1473
|
+
async function tryCatch(promise, defaultResult) {
|
|
1474
|
+
try {
|
|
1475
|
+
const result = await promise;
|
|
1476
|
+
return [null, result];
|
|
1477
|
+
}
|
|
1478
|
+
catch (error) {
|
|
1479
|
+
return [error, defaultResult ?? null];
|
|
1480
|
+
}
|
|
1481
|
+
}
|
|
1482
|
+
|
|
1483
|
+
/**
|
|
1484
|
+
* #### Try One By One
|
|
1485
|
+
*
|
|
1486
|
+
* Tries 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.
|
|
1487
|
+
*
|
|
1488
|
+
* * * *
|
|
1489
|
+
* Example:
|
|
1490
|
+
* ```typescript
|
|
1491
|
+
* import { tryOneByOne } from "@telperion/js-utils/promise";
|
|
1492
|
+
*
|
|
1493
|
+
* async function fooFunction() {
|
|
1494
|
+
* const foo = await tryOneByOne([
|
|
1495
|
+
* () => someCall(),
|
|
1496
|
+
* (err) => anotherCall(),
|
|
1497
|
+
* (err) => fooPromise()
|
|
1498
|
+
* ]);
|
|
1499
|
+
*
|
|
1500
|
+
* // do stuff
|
|
1501
|
+
* }
|
|
1502
|
+
*
|
|
1503
|
+
* ```
|
|
1504
|
+
*
|
|
1505
|
+
* Static Example:
|
|
1506
|
+
* ```typescript
|
|
1507
|
+
* import "@telperion/js-utils/promise/static/try-one-by-one";
|
|
1508
|
+
*
|
|
1509
|
+
* async function fooFunction() {
|
|
1510
|
+
* const foo = await Promise.tryOneByOne([
|
|
1511
|
+
* () => someCall(),
|
|
1512
|
+
* (err) => anotherCall(),
|
|
1513
|
+
* (err) => fooPromise()
|
|
1514
|
+
* ]);
|
|
1515
|
+
*
|
|
1516
|
+
* // do stuff
|
|
1517
|
+
* }
|
|
1518
|
+
*
|
|
1519
|
+
* ```
|
|
1520
|
+
* * * *
|
|
1521
|
+
* @param promises List of promises to try one by one with the order
|
|
1522
|
+
* @returns
|
|
1523
|
+
*/
|
|
1524
|
+
async function tryOneByOne(promises) {
|
|
1525
|
+
return await tryPromises([...promises], null);
|
|
1526
|
+
}
|
|
1527
|
+
async function tryPromises(promises, lastError) {
|
|
1528
|
+
const promiseFn = promises.shift();
|
|
1529
|
+
if (!promiseFn)
|
|
1530
|
+
throw lastError;
|
|
1531
|
+
try {
|
|
1532
|
+
const promise = typeof promiseFn === "function" ? promiseFn(lastError) : promiseFn;
|
|
1533
|
+
return await promise;
|
|
1534
|
+
}
|
|
1535
|
+
catch (err) {
|
|
1536
|
+
return tryPromises(promises, err);
|
|
1537
|
+
}
|
|
1538
|
+
}
|
|
1539
|
+
|
|
1540
|
+
/**
|
|
1541
|
+
* Converts seconds to milliseconds
|
|
1542
|
+
*
|
|
1543
|
+
* #### Example
|
|
1544
|
+
* ```typescript
|
|
1545
|
+
* import { seconds } from "@telperion/js-utils/time";
|
|
1546
|
+
*
|
|
1547
|
+
* const delay = seconds(5); // 5000 milliseconds
|
|
1548
|
+
* setTimeout(() => console.log('Done!'), delay);
|
|
1549
|
+
* ```
|
|
1550
|
+
*
|
|
1551
|
+
* @param value - Number of seconds
|
|
1552
|
+
* @returns Equivalent value in milliseconds
|
|
1553
|
+
*/
|
|
1554
|
+
function seconds(value) {
|
|
1555
|
+
return value * 1000;
|
|
1556
|
+
}
|
|
1557
|
+
|
|
1558
|
+
/**
|
|
1559
|
+
* Converts minutes to milliseconds
|
|
1560
|
+
*
|
|
1561
|
+
* #### Example
|
|
1562
|
+
* ```typescript
|
|
1563
|
+
* import { minutes } from "@telperion/js-utils/time";
|
|
1564
|
+
*
|
|
1565
|
+
* const delay = minutes(2); // 120000 milliseconds
|
|
1566
|
+
* setTimeout(() => console.log('Done!'), delay);
|
|
1567
|
+
* ```
|
|
1568
|
+
*
|
|
1569
|
+
* @param value - Number of minutes
|
|
1570
|
+
* @returns Equivalent value in milliseconds
|
|
1571
|
+
*/
|
|
1572
|
+
function minutes(value) {
|
|
1573
|
+
return seconds(value) * 60;
|
|
1574
|
+
}
|
|
1575
|
+
|
|
1576
|
+
/**
|
|
1577
|
+
* Converts hours to milliseconds
|
|
1578
|
+
*
|
|
1579
|
+
* #### Example
|
|
1580
|
+
* ```typescript
|
|
1581
|
+
* import { hours } from "@telperion/js-utils/time";
|
|
1582
|
+
*
|
|
1583
|
+
* const delay = hours(1); // 3600000 milliseconds
|
|
1584
|
+
* setTimeout(() => console.log('Done!'), delay);
|
|
1585
|
+
* ```
|
|
1586
|
+
*
|
|
1587
|
+
* @param value - Number of hours
|
|
1588
|
+
* @returns Equivalent value in milliseconds
|
|
1589
|
+
*/
|
|
1590
|
+
function hours(value) {
|
|
1591
|
+
return minutes(value) * 60;
|
|
1592
|
+
}
|
|
1593
|
+
|
|
1594
|
+
/**
|
|
1595
|
+
* Converts days to milliseconds
|
|
1596
|
+
*
|
|
1597
|
+
* #### Example
|
|
1598
|
+
* ```typescript
|
|
1599
|
+
* import { days } from "@telperion/js-utils/time";
|
|
1600
|
+
*
|
|
1601
|
+
* const expiryTime = Date.now() + days(7); // 7 days from now
|
|
1602
|
+
* console.log(new Date(expiryTime));
|
|
1603
|
+
* ```
|
|
1604
|
+
*
|
|
1605
|
+
* @param value - Number of days
|
|
1606
|
+
* @returns Equivalent value in milliseconds
|
|
1607
|
+
*/
|
|
1608
|
+
function days(value) {
|
|
1609
|
+
return hours(value) * 24;
|
|
1610
|
+
}
|
|
1611
|
+
|
|
1612
|
+
/**
|
|
1613
|
+
* Converts weeks to milliseconds
|
|
1614
|
+
*
|
|
1615
|
+
* #### Example
|
|
1616
|
+
* ```typescript
|
|
1617
|
+
* import { weeks } from "@telperion/js-utils/time";
|
|
1618
|
+
*
|
|
1619
|
+
* const expiryTime = Date.now() + weeks(2); // 2 weeks from now
|
|
1620
|
+
* console.log(new Date(expiryTime));
|
|
1621
|
+
* ```
|
|
1622
|
+
*
|
|
1623
|
+
* @param value - Number of weeks
|
|
1624
|
+
* @returns Equivalent value in milliseconds
|
|
1625
|
+
*/
|
|
1626
|
+
function weeks(value) {
|
|
1627
|
+
return days(value) * 7;
|
|
1628
|
+
}
|
|
1629
|
+
|
|
1630
|
+
/**
|
|
1631
|
+
* Converts months to milliseconds (assumes 30 days per month)
|
|
1632
|
+
*
|
|
1633
|
+
* #### Example
|
|
1634
|
+
* ```typescript
|
|
1635
|
+
* import { months } from "@telperion/js-utils/time";
|
|
1636
|
+
*
|
|
1637
|
+
* const expiryTime = Date.now() + months(3); // ~3 months from now
|
|
1638
|
+
* console.log(new Date(expiryTime));
|
|
1639
|
+
* ```
|
|
1640
|
+
*
|
|
1641
|
+
* @param value - Number of months
|
|
1642
|
+
* @returns Equivalent value in milliseconds (30 days per month)
|
|
1643
|
+
*/
|
|
1644
|
+
function months(value) {
|
|
1645
|
+
return days(value) * 30;
|
|
1646
|
+
}
|
|
1647
|
+
|
|
1648
|
+
/**
|
|
1649
|
+
* Converts years to milliseconds (assumes 365 days per year)
|
|
1650
|
+
*
|
|
1651
|
+
* #### Example
|
|
1652
|
+
* ```typescript
|
|
1653
|
+
* import { years } from "@telperion/js-utils/time";
|
|
1654
|
+
*
|
|
1655
|
+
* const expiryTime = Date.now() + years(1); // ~1 year from now
|
|
1656
|
+
* console.log(new Date(expiryTime));
|
|
1657
|
+
* ```
|
|
1658
|
+
*
|
|
1659
|
+
* @param value - Number of years
|
|
1660
|
+
* @returns Equivalent value in milliseconds (365 days per year)
|
|
1661
|
+
*/
|
|
1662
|
+
function years(value) {
|
|
1663
|
+
return days(value) * 365;
|
|
1664
|
+
}
|
|
1665
|
+
|
|
1666
|
+
/**
|
|
1667
|
+
* #### Compact
|
|
1668
|
+
*
|
|
1669
|
+
* Filters falsy values of the given array
|
|
1670
|
+
* Removes `null` and `undefined` values and their keys from an object
|
|
1671
|
+
*
|
|
1672
|
+
* * * *
|
|
1673
|
+
* Example usage:
|
|
1674
|
+
* ```typescript
|
|
1675
|
+
* import { compact } from "@telperion/js-utils";
|
|
1676
|
+
*
|
|
1677
|
+
* const arr = [undefined, "", false, 0, 1, "1"];
|
|
1678
|
+
* const compacted = compact(arr); // [1, "1"];
|
|
1679
|
+
*
|
|
1680
|
+
* const object = {
|
|
1681
|
+
* x: null,
|
|
1682
|
+
* y: undefined,
|
|
1683
|
+
* z: 20
|
|
1684
|
+
* };
|
|
1685
|
+
*
|
|
1686
|
+
* const compacted = compact(object); // {z: 20}
|
|
1687
|
+
* ```
|
|
1688
|
+
* * * *
|
|
1689
|
+
* @param arrayOrObject Array or Object to compact
|
|
1690
|
+
*/
|
|
1691
|
+
function compact(arrayOrObject) {
|
|
1692
|
+
if (arrayOrObject instanceof Array) {
|
|
1693
|
+
return compact$2(arrayOrObject);
|
|
1694
|
+
}
|
|
1695
|
+
if (arrayOrObject instanceof Object) {
|
|
1696
|
+
return compact$1(arrayOrObject);
|
|
1697
|
+
}
|
|
1698
|
+
throw new TypeError('Value is not object nor array');
|
|
1699
|
+
}
|
|
1700
|
+
|
|
1701
|
+
/**
|
|
1702
|
+
* #### Returns whether the entered value is falsy
|
|
1703
|
+
*
|
|
1704
|
+
* * * *
|
|
1705
|
+
* Example usage:
|
|
1706
|
+
* ```typescript
|
|
1707
|
+
* import { isFalsy } from "@telperion/js-utils";
|
|
1708
|
+
*
|
|
1709
|
+
* isFalsy(undefined); // true
|
|
1710
|
+
* isFalsy(true); // false
|
|
1711
|
+
* isFalsy([]) // false
|
|
1712
|
+
*
|
|
1713
|
+
* const falsyValues = ["a", undefined, "b", "", "c"].filter(isFalsy); // [undefined, ""]
|
|
1714
|
+
* ```
|
|
1715
|
+
* Static usage example:
|
|
1716
|
+
* ```typescript
|
|
1717
|
+
* import "@telperion/js-utils/dist/as-static/is-falsy";
|
|
1718
|
+
*
|
|
1719
|
+
* Boolean.isFalsy(undefined); // true
|
|
1720
|
+
* Boolean.isFalsy(true); // false
|
|
1721
|
+
* Boolean.isFalsy([]) // false
|
|
1722
|
+
*
|
|
1723
|
+
* const falsyValues = ["a", undefined, "b", "", "c"].filter(Boolean.isFalsy); // [undefined, ""]
|
|
1724
|
+
* ```
|
|
1725
|
+
* * * *
|
|
1726
|
+
* @param value value to be checked
|
|
1727
|
+
*/
|
|
1728
|
+
function isFalsy(value) {
|
|
1729
|
+
return !value;
|
|
1730
|
+
}
|
|
1731
|
+
|
|
1732
|
+
const CACHE = {};
|
|
1733
|
+
/**
|
|
1734
|
+
* Unique Id Generator
|
|
1735
|
+
*
|
|
1736
|
+
* @param {string} [prefix]
|
|
1737
|
+
*/
|
|
1738
|
+
function* uniqueIdGenerator(prefix) {
|
|
1739
|
+
let counter = 0;
|
|
1740
|
+
while (true) {
|
|
1741
|
+
yield prefix ? `${prefix}-${counter++}` : counter++;
|
|
1742
|
+
}
|
|
1743
|
+
}
|
|
1744
|
+
function uniqueId(prefix) {
|
|
1745
|
+
if (!prefix || typeof prefix !== 'string') {
|
|
1746
|
+
prefix = undefined;
|
|
1747
|
+
}
|
|
1748
|
+
if (!(prefix in CACHE)) {
|
|
1749
|
+
CACHE[prefix] = uniqueIdGenerator(prefix);
|
|
1750
|
+
}
|
|
1751
|
+
return CACHE[prefix].next().value;
|
|
1752
|
+
}
|
|
1753
|
+
|
|
1754
|
+
/**
|
|
1755
|
+
* #### Limit
|
|
1756
|
+
*
|
|
1757
|
+
* Limits the string to `n` character
|
|
1758
|
+
*
|
|
1759
|
+
* * * *
|
|
1760
|
+
* Example:
|
|
1761
|
+
* ```typescript
|
|
1762
|
+
* import { limit } from "@telperion/js-utils/string";
|
|
1763
|
+
*
|
|
1764
|
+
* const str = 'foobarbaz';
|
|
1765
|
+
*
|
|
1766
|
+
* limit(str, 3); // 'foo'
|
|
1767
|
+
* ```
|
|
1768
|
+
*
|
|
1769
|
+
* Prototype Example:
|
|
1770
|
+
* ```typescript
|
|
1771
|
+
* import "@telperion/js-utils/string/proto/limit";
|
|
1772
|
+
*
|
|
1773
|
+
* const str = 'foobarbaz';
|
|
1774
|
+
*
|
|
1775
|
+
* str.limit(3); // 'foo'
|
|
1776
|
+
* ```
|
|
1777
|
+
* * * *
|
|
1778
|
+
* @param str: String to limit
|
|
1779
|
+
* @param count Count to limit string character size
|
|
1780
|
+
* @return Limited string
|
|
1781
|
+
*/
|
|
1782
|
+
function limit$1(str, count) {
|
|
1783
|
+
return limit$2(str.split(''), count).join('');
|
|
1784
|
+
}
|
|
1785
|
+
|
|
1786
|
+
/**
|
|
1787
|
+
* #### Limit
|
|
1788
|
+
*
|
|
1789
|
+
* Limits the string or array to `n` character
|
|
1790
|
+
*
|
|
1791
|
+
* * * *
|
|
1792
|
+
* Example usage:
|
|
1793
|
+
* ```typescript
|
|
1794
|
+
* import { limit } from "@telperion/js-utils";
|
|
1795
|
+
*
|
|
1796
|
+
* const str = 'foobarbaz';
|
|
1797
|
+
* const array = ["a", "b", "c", "d", "e", "f"];
|
|
1798
|
+
*
|
|
1799
|
+
* limit(str, 3); // 'foo'
|
|
1800
|
+
* limit(array, 3); // ["a", "b", "c"]
|
|
1801
|
+
* ```
|
|
1802
|
+
* * * *
|
|
1803
|
+
* @param arrayOrString value to limit
|
|
1804
|
+
* @param count Max count
|
|
1805
|
+
*/
|
|
1806
|
+
function limit(arrayOrString, count) {
|
|
1807
|
+
if (typeof arrayOrString === 'string') {
|
|
1808
|
+
return limit$1(arrayOrString, count);
|
|
1809
|
+
}
|
|
1810
|
+
if (arrayOrString instanceof Array) {
|
|
1811
|
+
return limit$2(arrayOrString, count);
|
|
1812
|
+
}
|
|
1813
|
+
throw new Error('Value is not string nor array');
|
|
1814
|
+
}
|
|
1815
|
+
|
|
1816
|
+
export { DEFAULT_DEBOUNCE_TIME, NEVER, OpenPromise, SmartMap, arrayize, asyncMap, call, chain, clone, compact, days, debounce, debounceWithKey, deepFreeze, deepest, defer, difference, findByKey, gather, hours, intersection, isFalsy, isTruthy, limit, map, merge, minMax, minutes, mixin, months, never, noop, of, promisify, remove, replace, revert, seconds, timeout, tryCatch, tryOneByOne, uniqueId, uniquify, uniquifyByKey, weeks, years };
|
|
1817
|
+
//# sourceMappingURL=js-utils.mjs.map
|