@kdtlabs/utils 0.0.4 → 0.0.5
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/SKILL.md +7 -9
- package/{common → dist/common}/assertions.d.ts +1 -1
- package/{common → dist/common}/assertions.d.ts.map +1 -1
- package/{common → dist/common}/types.d.ts +1 -1
- package/{common → dist/common}/types.d.ts.map +1 -1
- package/{functions → dist/functions}/executions.d.ts +1 -1
- package/{functions → dist/functions}/executions.d.ts.map +1 -1
- package/dist/index.js +7 -0
- package/dist/index.js.map +99 -0
- package/{objects → dist/objects}/transformations.d.ts +1 -1
- package/{objects → dist/objects}/transformations.d.ts.map +1 -1
- package/{objects → dist/objects}/types.d.ts +1 -1
- package/{objects → dist/objects}/types.d.ts.map +1 -1
- package/{promises → dist/promises}/deferred.d.ts +1 -1
- package/{promises → dist/promises}/deferred.d.ts.map +1 -1
- package/{promises → dist/promises}/timers.d.ts +1 -1
- package/{promises → dist/promises}/timers.d.ts.map +1 -1
- package/{serializer → dist/serializer}/context.d.ts +1 -1
- package/{serializer → dist/serializer}/context.d.ts.map +1 -1
- package/{serializer → dist/serializer}/serialize.d.ts +1 -1
- package/{serializer → dist/serializer}/serialize.d.ts.map +1 -1
- package/{serializer → dist/serializer}/serializers/array.d.ts +1 -1
- package/{serializer → dist/serializer}/serializers/array.d.ts.map +1 -1
- package/{serializer → dist/serializer}/serializers/binary.d.ts +1 -1
- package/dist/serializer/serializers/binary.d.ts.map +1 -0
- package/{serializer → dist/serializer}/serializers/blob.d.ts +1 -1
- package/dist/serializer/serializers/blob.d.ts.map +1 -0
- package/{serializer → dist/serializer}/serializers/collection.d.ts +1 -1
- package/{serializer → dist/serializer}/serializers/collection.d.ts.map +1 -1
- package/{serializer → dist/serializer}/serializers/compound.d.ts +1 -1
- package/{serializer → dist/serializer}/serializers/compound.d.ts.map +1 -1
- package/{serializer → dist/serializer}/serializers/error.d.ts +1 -1
- package/{serializer → dist/serializer}/serializers/error.d.ts.map +1 -1
- package/{serializer → dist/serializer}/serializers/function.d.ts +1 -1
- package/{serializer → dist/serializer}/serializers/function.d.ts.map +1 -1
- package/{serializer → dist/serializer}/serializers/leaf-object.d.ts +1 -1
- package/{serializer → dist/serializer}/serializers/leaf-object.d.ts.map +1 -1
- package/{serializer → dist/serializer}/serializers/number.d.ts +1 -1
- package/dist/serializer/serializers/number.d.ts.map +1 -0
- package/{serializer → dist/serializer}/serializers/object.d.ts +1 -1
- package/{serializer → dist/serializer}/serializers/object.d.ts.map +1 -1
- package/{serializer → dist/serializer}/serializers/opaque.d.ts +1 -1
- package/dist/serializer/serializers/opaque.d.ts.map +1 -0
- package/{serializer → dist/serializer}/serializers/primitive.d.ts +1 -1
- package/{serializer → dist/serializer}/serializers/primitive.d.ts.map +1 -1
- package/{serializer → dist/serializer}/types.d.ts +1 -1
- package/{serializer → dist/serializer}/types.d.ts.map +1 -1
- package/{serializer → dist/serializer}/utils.d.ts +1 -1
- package/{serializer → dist/serializer}/utils.d.ts.map +1 -1
- package/{system → dist/system}/fetch.d.ts +2 -2
- package/{system → dist/system}/fetch.d.ts.map +1 -1
- package/dist/system/types.d.ts +3 -0
- package/{system → dist/system}/types.d.ts.map +1 -1
- package/{times → dist/times}/conversions.d.ts +1 -1
- package/{times → dist/times}/conversions.d.ts.map +1 -1
- package/package.json +11 -73
- package/src/arrays/accessors.ts +25 -0
- package/src/arrays/conversions.ts +16 -0
- package/src/arrays/factories.ts +13 -0
- package/src/arrays/guards.ts +7 -0
- package/src/arrays/index.ts +8 -0
- package/src/arrays/operations.ts +103 -0
- package/src/arrays/set-operations.ts +11 -0
- package/src/arrays/types.ts +21 -0
- package/src/buffers/conversions.ts +26 -0
- package/src/buffers/guards.ts +13 -0
- package/src/buffers/index.ts +5 -0
- package/src/buffers/operations.ts +44 -0
- package/src/buffers/types.ts +1 -0
- package/src/collections/fifo-map.ts +33 -0
- package/src/collections/fifo-set.ts +29 -0
- package/src/collections/guards.ts +11 -0
- package/src/collections/index.ts +10 -0
- package/src/collections/linked-base.ts +117 -0
- package/src/collections/linked-map.ts +82 -0
- package/src/collections/linked-set.ts +69 -0
- package/src/collections/lru-map.ts +36 -0
- package/src/collections/lru-set.ts +25 -0
- package/src/collections/types.ts +12 -0
- package/src/common/assertions.ts +11 -0
- package/src/common/guards.ts +58 -0
- package/src/common/index.ts +5 -0
- package/src/common/transformations.ts +20 -0
- package/src/common/types.ts +3 -0
- package/src/core/constants.ts +1 -0
- package/src/core/conversions.ts +9 -0
- package/src/core/guards.ts +57 -0
- package/src/core/index.ts +5 -0
- package/src/core/types.ts +15 -0
- package/src/errors/base-error.ts +48 -0
- package/src/errors/factories.ts +74 -0
- package/src/errors/guards.ts +12 -0
- package/src/errors/index.ts +7 -0
- package/src/errors/operations.ts +15 -0
- package/src/errors/stringify.ts +131 -0
- package/src/errors/types.ts +11 -0
- package/src/events/emitter.ts +117 -0
- package/src/events/index.ts +2 -0
- package/src/events/types.ts +11 -0
- package/src/functions/compositions.ts +17 -0
- package/src/functions/debounce.ts +34 -0
- package/src/functions/executions.ts +12 -0
- package/src/functions/guards.ts +5 -0
- package/src/functions/index.ts +11 -0
- package/src/functions/memoize.ts +33 -0
- package/src/functions/once.ts +33 -0
- package/src/functions/pipe.ts +39 -0
- package/src/functions/throttle.ts +59 -0
- package/src/functions/timer-state.ts +33 -0
- package/src/functions/types.ts +8 -0
- package/src/index.ts +16 -0
- package/src/json-rpc/constants.ts +15 -0
- package/src/json-rpc/factories.ts +22 -0
- package/src/json-rpc/guards.ts +58 -0
- package/src/json-rpc/index.ts +5 -0
- package/src/json-rpc/types.ts +48 -0
- package/src/numbers/bigint-math.ts +163 -0
- package/src/numbers/constants.ts +14 -0
- package/src/numbers/conversions.ts +83 -0
- package/src/numbers/factories.ts +6 -0
- package/src/numbers/formats.ts +52 -0
- package/src/numbers/guards.ts +22 -0
- package/src/numbers/index.ts +10 -0
- package/src/numbers/maths.ts +28 -0
- package/src/numbers/ranges.ts +17 -0
- package/src/numbers/types.ts +7 -0
- package/src/objects/deep-merge.ts +76 -0
- package/src/objects/guards.ts +21 -0
- package/src/objects/index.ts +5 -0
- package/src/objects/transformations.ts +34 -0
- package/src/objects/types.ts +33 -0
- package/src/promises/abortable.ts +37 -0
- package/src/promises/compositions.ts +18 -0
- package/src/promises/deferred.ts +131 -0
- package/src/promises/guards.ts +8 -0
- package/src/promises/index.ts +10 -0
- package/src/promises/pipe.ts +39 -0
- package/src/promises/poll.ts +92 -0
- package/src/promises/retry.ts +138 -0
- package/src/promises/timers.ts +16 -0
- package/src/promises/types.ts +1 -0
- package/src/serializer/constants.ts +5 -0
- package/src/serializer/context.ts +24 -0
- package/src/serializer/index.ts +8 -0
- package/src/serializer/serialize.ts +51 -0
- package/src/serializer/serializers/array.ts +17 -0
- package/src/serializer/serializers/binary.ts +24 -0
- package/src/serializer/serializers/blob.ts +18 -0
- package/src/serializer/serializers/collection.ts +25 -0
- package/src/serializer/serializers/compound.ts +79 -0
- package/src/serializer/serializers/error.ts +75 -0
- package/src/serializer/serializers/function.ts +13 -0
- package/src/serializer/serializers/index.ts +12 -0
- package/src/serializer/serializers/leaf-object.ts +18 -0
- package/src/serializer/serializers/number.ts +14 -0
- package/src/serializer/serializers/object.ts +62 -0
- package/src/serializer/serializers/opaque.ts +36 -0
- package/src/serializer/serializers/primitive.ts +32 -0
- package/src/serializer/symbol-registry.ts +28 -0
- package/src/serializer/types.ts +42 -0
- package/src/serializer/utils.ts +15 -0
- package/src/strings/constants.ts +1 -0
- package/src/strings/factories.ts +9 -0
- package/src/strings/guards.ts +51 -0
- package/src/strings/index.ts +6 -0
- package/src/strings/manipulations.ts +99 -0
- package/src/strings/types.ts +3 -0
- package/src/system/env.ts +32 -0
- package/src/system/fetch.ts +23 -0
- package/src/system/graceful-exit.ts +46 -0
- package/src/system/index.ts +6 -0
- package/src/system/path.ts +12 -0
- package/src/system/types.ts +3 -0
- package/src/times/constants.ts +6 -0
- package/src/times/conversions.ts +85 -0
- package/src/times/factories.ts +3 -0
- package/src/times/guards.ts +3 -0
- package/src/times/index.ts +5 -0
- package/src/times/operations.ts +9 -0
- package/CHANGELOG.md +0 -57
- package/arrays/index.js +0 -4
- package/arrays/index.js.map +0 -9
- package/buffers/index.js +0 -4
- package/buffers/index.js.map +0 -9
- package/chunk-25ja9350.js +0 -4
- package/chunk-25ja9350.js.map +0 -13
- package/chunk-3w6nt7kb.js +0 -4
- package/chunk-3w6nt7kb.js.map +0 -12
- package/chunk-5txwcr6j.js +0 -4
- package/chunk-5txwcr6j.js.map +0 -17
- package/chunk-6dxad51h.js +0 -4
- package/chunk-6dxad51h.js.map +0 -12
- package/chunk-6kdnnxe0.js +0 -4
- package/chunk-6kdnnxe0.js.map +0 -17
- package/chunk-7cndek91.js +0 -4
- package/chunk-7cndek91.js.map +0 -15
- package/chunk-bee0nxse.js +0 -6
- package/chunk-bee0nxse.js.map +0 -14
- package/chunk-bjmntg2y.js +0 -4
- package/chunk-bjmntg2y.js.map +0 -18
- package/chunk-d0d0d285.js +0 -4
- package/chunk-d0d0d285.js.map +0 -26
- package/chunk-jny2gdyy.js +0 -4
- package/chunk-jny2gdyy.js.map +0 -12
- package/chunk-kbzgn0z4.js +0 -4
- package/chunk-kbzgn0z4.js.map +0 -10
- package/chunk-qmbgp0vr.js +0 -4
- package/chunk-qmbgp0vr.js.map +0 -12
- package/chunk-qn6n0ff5.js +0 -4
- package/chunk-qn6n0ff5.js.map +0 -17
- package/chunk-r3maskdb.js +0 -5
- package/chunk-r3maskdb.js.map +0 -13
- package/chunk-st3dxvqt.js +0 -4
- package/chunk-st3dxvqt.js.map +0 -14
- package/chunk-xp18wdk6.js +0 -4
- package/chunk-xp18wdk6.js.map +0 -12
- package/collections/index.js +0 -4
- package/collections/index.js.map +0 -9
- package/common/index.js +0 -4
- package/common/index.js.map +0 -9
- package/core/index.js +0 -4
- package/core/index.js.map +0 -9
- package/errors/index.js +0 -4
- package/errors/index.js.map +0 -9
- package/events/index.js +0 -4
- package/events/index.js.map +0 -9
- package/functions/index.js +0 -4
- package/functions/index.js.map +0 -9
- package/index.js +0 -4
- package/index.js.map +0 -9
- package/json-rpc/index.js +0 -4
- package/json-rpc/index.js.map +0 -9
- package/meta.json +0 -2998
- package/meta.md +0 -1816
- package/numbers/index.js +0 -4
- package/numbers/index.js.map +0 -9
- package/objects/index.js +0 -4
- package/objects/index.js.map +0 -9
- package/promises/index.js +0 -4
- package/promises/index.js.map +0 -9
- package/serializer/index.js +0 -4
- package/serializer/index.js.map +0 -9
- package/serializer/serializers/binary.d.ts.map +0 -1
- package/serializer/serializers/blob.d.ts.map +0 -1
- package/serializer/serializers/number.d.ts.map +0 -1
- package/serializer/serializers/opaque.d.ts.map +0 -1
- package/strings/index.js +0 -4
- package/strings/index.js.map +0 -9
- package/system/index.js +0 -4
- package/system/index.js.map +0 -9
- package/system/types.d.ts +0 -3
- package/times/index.js +0 -4
- package/times/index.js.map +0 -9
- /package/{arrays → dist/arrays}/accessors.d.ts +0 -0
- /package/{arrays → dist/arrays}/accessors.d.ts.map +0 -0
- /package/{arrays → dist/arrays}/conversions.d.ts +0 -0
- /package/{arrays → dist/arrays}/conversions.d.ts.map +0 -0
- /package/{arrays → dist/arrays}/factories.d.ts +0 -0
- /package/{arrays → dist/arrays}/factories.d.ts.map +0 -0
- /package/{arrays → dist/arrays}/guards.d.ts +0 -0
- /package/{arrays → dist/arrays}/guards.d.ts.map +0 -0
- /package/{arrays → dist/arrays}/index.d.ts +0 -0
- /package/{arrays → dist/arrays}/index.d.ts.map +0 -0
- /package/{arrays → dist/arrays}/operations.d.ts +0 -0
- /package/{arrays → dist/arrays}/operations.d.ts.map +0 -0
- /package/{arrays → dist/arrays}/set-operations.d.ts +0 -0
- /package/{arrays → dist/arrays}/set-operations.d.ts.map +0 -0
- /package/{arrays → dist/arrays}/types.d.ts +0 -0
- /package/{arrays → dist/arrays}/types.d.ts.map +0 -0
- /package/{buffers → dist/buffers}/conversions.d.ts +0 -0
- /package/{buffers → dist/buffers}/conversions.d.ts.map +0 -0
- /package/{buffers → dist/buffers}/guards.d.ts +0 -0
- /package/{buffers → dist/buffers}/guards.d.ts.map +0 -0
- /package/{buffers → dist/buffers}/index.d.ts +0 -0
- /package/{buffers → dist/buffers}/index.d.ts.map +0 -0
- /package/{buffers → dist/buffers}/operations.d.ts +0 -0
- /package/{buffers → dist/buffers}/operations.d.ts.map +0 -0
- /package/{buffers → dist/buffers}/types.d.ts +0 -0
- /package/{buffers → dist/buffers}/types.d.ts.map +0 -0
- /package/{collections → dist/collections}/fifo-map.d.ts +0 -0
- /package/{collections → dist/collections}/fifo-map.d.ts.map +0 -0
- /package/{collections → dist/collections}/fifo-set.d.ts +0 -0
- /package/{collections → dist/collections}/fifo-set.d.ts.map +0 -0
- /package/{collections → dist/collections}/guards.d.ts +0 -0
- /package/{collections → dist/collections}/guards.d.ts.map +0 -0
- /package/{collections → dist/collections}/index.d.ts +0 -0
- /package/{collections → dist/collections}/index.d.ts.map +0 -0
- /package/{collections → dist/collections}/linked-base.d.ts +0 -0
- /package/{collections → dist/collections}/linked-base.d.ts.map +0 -0
- /package/{collections → dist/collections}/linked-map.d.ts +0 -0
- /package/{collections → dist/collections}/linked-map.d.ts.map +0 -0
- /package/{collections → dist/collections}/linked-set.d.ts +0 -0
- /package/{collections → dist/collections}/linked-set.d.ts.map +0 -0
- /package/{collections → dist/collections}/lru-map.d.ts +0 -0
- /package/{collections → dist/collections}/lru-map.d.ts.map +0 -0
- /package/{collections → dist/collections}/lru-set.d.ts +0 -0
- /package/{collections → dist/collections}/lru-set.d.ts.map +0 -0
- /package/{collections → dist/collections}/types.d.ts +0 -0
- /package/{collections → dist/collections}/types.d.ts.map +0 -0
- /package/{common → dist/common}/guards.d.ts +0 -0
- /package/{common → dist/common}/guards.d.ts.map +0 -0
- /package/{common → dist/common}/index.d.ts +0 -0
- /package/{common → dist/common}/index.d.ts.map +0 -0
- /package/{common → dist/common}/transformations.d.ts +0 -0
- /package/{common → dist/common}/transformations.d.ts.map +0 -0
- /package/{core → dist/core}/constants.d.ts +0 -0
- /package/{core → dist/core}/constants.d.ts.map +0 -0
- /package/{core → dist/core}/conversions.d.ts +0 -0
- /package/{core → dist/core}/conversions.d.ts.map +0 -0
- /package/{core → dist/core}/guards.d.ts +0 -0
- /package/{core → dist/core}/guards.d.ts.map +0 -0
- /package/{core → dist/core}/index.d.ts +0 -0
- /package/{core → dist/core}/index.d.ts.map +0 -0
- /package/{core → dist/core}/types.d.ts +0 -0
- /package/{core → dist/core}/types.d.ts.map +0 -0
- /package/{errors → dist/errors}/base-error.d.ts +0 -0
- /package/{errors → dist/errors}/base-error.d.ts.map +0 -0
- /package/{errors → dist/errors}/factories.d.ts +0 -0
- /package/{errors → dist/errors}/factories.d.ts.map +0 -0
- /package/{errors → dist/errors}/guards.d.ts +0 -0
- /package/{errors → dist/errors}/guards.d.ts.map +0 -0
- /package/{errors → dist/errors}/index.d.ts +0 -0
- /package/{errors → dist/errors}/index.d.ts.map +0 -0
- /package/{errors → dist/errors}/operations.d.ts +0 -0
- /package/{errors → dist/errors}/operations.d.ts.map +0 -0
- /package/{errors → dist/errors}/stringify.d.ts +0 -0
- /package/{errors → dist/errors}/stringify.d.ts.map +0 -0
- /package/{errors → dist/errors}/types.d.ts +0 -0
- /package/{errors → dist/errors}/types.d.ts.map +0 -0
- /package/{events → dist/events}/emitter.d.ts +0 -0
- /package/{events → dist/events}/emitter.d.ts.map +0 -0
- /package/{events → dist/events}/index.d.ts +0 -0
- /package/{events → dist/events}/index.d.ts.map +0 -0
- /package/{events → dist/events}/types.d.ts +0 -0
- /package/{events → dist/events}/types.d.ts.map +0 -0
- /package/{functions → dist/functions}/compositions.d.ts +0 -0
- /package/{functions → dist/functions}/compositions.d.ts.map +0 -0
- /package/{functions → dist/functions}/debounce.d.ts +0 -0
- /package/{functions → dist/functions}/debounce.d.ts.map +0 -0
- /package/{functions → dist/functions}/guards.d.ts +0 -0
- /package/{functions → dist/functions}/guards.d.ts.map +0 -0
- /package/{functions → dist/functions}/index.d.ts +0 -0
- /package/{functions → dist/functions}/index.d.ts.map +0 -0
- /package/{functions → dist/functions}/memoize.d.ts +0 -0
- /package/{functions → dist/functions}/memoize.d.ts.map +0 -0
- /package/{functions → dist/functions}/once.d.ts +0 -0
- /package/{functions → dist/functions}/once.d.ts.map +0 -0
- /package/{functions → dist/functions}/pipe.d.ts +0 -0
- /package/{functions → dist/functions}/pipe.d.ts.map +0 -0
- /package/{functions → dist/functions}/throttle.d.ts +0 -0
- /package/{functions → dist/functions}/throttle.d.ts.map +0 -0
- /package/{functions → dist/functions}/timer-state.d.ts +0 -0
- /package/{functions → dist/functions}/timer-state.d.ts.map +0 -0
- /package/{functions → dist/functions}/types.d.ts +0 -0
- /package/{functions → dist/functions}/types.d.ts.map +0 -0
- /package/{index.d.ts → dist/index.d.ts} +0 -0
- /package/{index.d.ts.map → dist/index.d.ts.map} +0 -0
- /package/{json-rpc → dist/json-rpc}/constants.d.ts +0 -0
- /package/{json-rpc → dist/json-rpc}/constants.d.ts.map +0 -0
- /package/{json-rpc → dist/json-rpc}/factories.d.ts +0 -0
- /package/{json-rpc → dist/json-rpc}/factories.d.ts.map +0 -0
- /package/{json-rpc → dist/json-rpc}/guards.d.ts +0 -0
- /package/{json-rpc → dist/json-rpc}/guards.d.ts.map +0 -0
- /package/{json-rpc → dist/json-rpc}/index.d.ts +0 -0
- /package/{json-rpc → dist/json-rpc}/index.d.ts.map +0 -0
- /package/{json-rpc → dist/json-rpc}/types.d.ts +0 -0
- /package/{json-rpc → dist/json-rpc}/types.d.ts.map +0 -0
- /package/{numbers → dist/numbers}/bigint-math.d.ts +0 -0
- /package/{numbers → dist/numbers}/bigint-math.d.ts.map +0 -0
- /package/{numbers → dist/numbers}/constants.d.ts +0 -0
- /package/{numbers → dist/numbers}/constants.d.ts.map +0 -0
- /package/{numbers → dist/numbers}/conversions.d.ts +0 -0
- /package/{numbers → dist/numbers}/conversions.d.ts.map +0 -0
- /package/{numbers → dist/numbers}/factories.d.ts +0 -0
- /package/{numbers → dist/numbers}/factories.d.ts.map +0 -0
- /package/{numbers → dist/numbers}/formats.d.ts +0 -0
- /package/{numbers → dist/numbers}/formats.d.ts.map +0 -0
- /package/{numbers → dist/numbers}/guards.d.ts +0 -0
- /package/{numbers → dist/numbers}/guards.d.ts.map +0 -0
- /package/{numbers → dist/numbers}/index.d.ts +0 -0
- /package/{numbers → dist/numbers}/index.d.ts.map +0 -0
- /package/{numbers → dist/numbers}/maths.d.ts +0 -0
- /package/{numbers → dist/numbers}/maths.d.ts.map +0 -0
- /package/{numbers → dist/numbers}/ranges.d.ts +0 -0
- /package/{numbers → dist/numbers}/ranges.d.ts.map +0 -0
- /package/{numbers → dist/numbers}/types.d.ts +0 -0
- /package/{numbers → dist/numbers}/types.d.ts.map +0 -0
- /package/{objects → dist/objects}/deep-merge.d.ts +0 -0
- /package/{objects → dist/objects}/deep-merge.d.ts.map +0 -0
- /package/{objects → dist/objects}/guards.d.ts +0 -0
- /package/{objects → dist/objects}/guards.d.ts.map +0 -0
- /package/{objects → dist/objects}/index.d.ts +0 -0
- /package/{objects → dist/objects}/index.d.ts.map +0 -0
- /package/{promises → dist/promises}/abortable.d.ts +0 -0
- /package/{promises → dist/promises}/abortable.d.ts.map +0 -0
- /package/{promises → dist/promises}/compositions.d.ts +0 -0
- /package/{promises → dist/promises}/compositions.d.ts.map +0 -0
- /package/{promises → dist/promises}/guards.d.ts +0 -0
- /package/{promises → dist/promises}/guards.d.ts.map +0 -0
- /package/{promises → dist/promises}/index.d.ts +0 -0
- /package/{promises → dist/promises}/index.d.ts.map +0 -0
- /package/{promises → dist/promises}/pipe.d.ts +0 -0
- /package/{promises → dist/promises}/pipe.d.ts.map +0 -0
- /package/{promises → dist/promises}/poll.d.ts +0 -0
- /package/{promises → dist/promises}/poll.d.ts.map +0 -0
- /package/{promises → dist/promises}/retry.d.ts +0 -0
- /package/{promises → dist/promises}/retry.d.ts.map +0 -0
- /package/{promises → dist/promises}/types.d.ts +0 -0
- /package/{promises → dist/promises}/types.d.ts.map +0 -0
- /package/{serializer → dist/serializer}/constants.d.ts +0 -0
- /package/{serializer → dist/serializer}/constants.d.ts.map +0 -0
- /package/{serializer → dist/serializer}/index.d.ts +0 -0
- /package/{serializer → dist/serializer}/index.d.ts.map +0 -0
- /package/{serializer → dist/serializer}/serializers/index.d.ts +0 -0
- /package/{serializer → dist/serializer}/serializers/index.d.ts.map +0 -0
- /package/{serializer → dist/serializer}/symbol-registry.d.ts +0 -0
- /package/{serializer → dist/serializer}/symbol-registry.d.ts.map +0 -0
- /package/{strings → dist/strings}/constants.d.ts +0 -0
- /package/{strings → dist/strings}/constants.d.ts.map +0 -0
- /package/{strings → dist/strings}/factories.d.ts +0 -0
- /package/{strings → dist/strings}/factories.d.ts.map +0 -0
- /package/{strings → dist/strings}/guards.d.ts +0 -0
- /package/{strings → dist/strings}/guards.d.ts.map +0 -0
- /package/{strings → dist/strings}/index.d.ts +0 -0
- /package/{strings → dist/strings}/index.d.ts.map +0 -0
- /package/{strings → dist/strings}/manipulations.d.ts +0 -0
- /package/{strings → dist/strings}/manipulations.d.ts.map +0 -0
- /package/{strings → dist/strings}/types.d.ts +0 -0
- /package/{strings → dist/strings}/types.d.ts.map +0 -0
- /package/{system → dist/system}/env.d.ts +0 -0
- /package/{system → dist/system}/env.d.ts.map +0 -0
- /package/{system → dist/system}/graceful-exit.d.ts +0 -0
- /package/{system → dist/system}/graceful-exit.d.ts.map +0 -0
- /package/{system → dist/system}/index.d.ts +0 -0
- /package/{system → dist/system}/index.d.ts.map +0 -0
- /package/{system → dist/system}/path.d.ts +0 -0
- /package/{system → dist/system}/path.d.ts.map +0 -0
- /package/{times → dist/times}/constants.d.ts +0 -0
- /package/{times → dist/times}/constants.d.ts.map +0 -0
- /package/{times → dist/times}/factories.d.ts +0 -0
- /package/{times → dist/times}/factories.d.ts.map +0 -0
- /package/{times → dist/times}/guards.d.ts +0 -0
- /package/{times → dist/times}/guards.d.ts.map +0 -0
- /package/{times → dist/times}/index.d.ts +0 -0
- /package/{times → dist/times}/index.d.ts.map +0 -0
- /package/{times → dist/times}/operations.d.ts +0 -0
- /package/{times → dist/times}/operations.d.ts.map +0 -0
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { Args, Fn, TimingControlled } from './types'
|
|
2
|
+
import { createTimerState, flushTimer, invokeTimer } from './timer-state'
|
|
3
|
+
|
|
4
|
+
export type Throttled<T extends Fn> = TimingControlled<T>
|
|
5
|
+
|
|
6
|
+
export function throttle<T extends Fn>(fn: T, wait: number): Throttled<T> {
|
|
7
|
+
const state = createTimerState<T>()
|
|
8
|
+
|
|
9
|
+
let lastCallTime: number | undefined
|
|
10
|
+
|
|
11
|
+
const invoke = () => {
|
|
12
|
+
lastCallTime = Date.now()
|
|
13
|
+
invokeTimer(state, fn)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const throttled: Throttled<T> = function (this: unknown, ...args: Args<T>) {
|
|
17
|
+
const now = Date.now()
|
|
18
|
+
|
|
19
|
+
state.lastArgs = args
|
|
20
|
+
state.lastThis = this
|
|
21
|
+
|
|
22
|
+
if (lastCallTime === undefined) {
|
|
23
|
+
lastCallTime = now
|
|
24
|
+
state.lastResult = fn.apply(this, args)
|
|
25
|
+
|
|
26
|
+
return
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const remaining = wait - (now - lastCallTime)
|
|
30
|
+
|
|
31
|
+
if (remaining <= 0) {
|
|
32
|
+
if (state.timeoutId !== undefined) {
|
|
33
|
+
clearTimeout(state.timeoutId)
|
|
34
|
+
state.timeoutId = undefined
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
lastCallTime = now
|
|
38
|
+
state.lastResult = fn.apply(this, args)
|
|
39
|
+
} else {
|
|
40
|
+
state.timeoutId ??= setTimeout(invoke, remaining)
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
throttled.cancel = () => {
|
|
45
|
+
if (state.timeoutId !== undefined) {
|
|
46
|
+
clearTimeout(state.timeoutId)
|
|
47
|
+
state.timeoutId = undefined
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
state.lastArgs = undefined
|
|
51
|
+
lastCallTime = undefined
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
throttled.flush = () => {
|
|
55
|
+
return flushTimer(state, invoke)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return throttled
|
|
59
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { Args, Fn } from './types'
|
|
2
|
+
|
|
3
|
+
export interface TimerState<T extends Fn> {
|
|
4
|
+
lastArgs: Args<T> | undefined
|
|
5
|
+
lastResult: ReturnType<T> | undefined
|
|
6
|
+
lastThis: unknown
|
|
7
|
+
timeoutId: ReturnType<typeof setTimeout> | undefined
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export const createTimerState = <T extends Fn>(): TimerState<T> => ({
|
|
11
|
+
lastArgs: undefined,
|
|
12
|
+
lastResult: undefined,
|
|
13
|
+
lastThis: undefined,
|
|
14
|
+
timeoutId: undefined,
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
export function invokeTimer<T extends Fn>(state: TimerState<T>, fn: T) {
|
|
18
|
+
const args = state.lastArgs!
|
|
19
|
+
const thisArg = state.lastThis
|
|
20
|
+
|
|
21
|
+
state.timeoutId = undefined
|
|
22
|
+
state.lastArgs = undefined
|
|
23
|
+
state.lastResult = fn.apply(thisArg, args)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function flushTimer<T extends Fn>(state: TimerState<T>, invoke: () => void) {
|
|
27
|
+
if (state.timeoutId !== undefined) {
|
|
28
|
+
clearTimeout(state.timeoutId)
|
|
29
|
+
invoke()
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return state.lastResult
|
|
33
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export type Fn = (...args: any[]) => any
|
|
2
|
+
|
|
3
|
+
export type Args<F extends Fn> = F extends (...args: infer A) => any ? A : never
|
|
4
|
+
|
|
5
|
+
export type TimingControlled<T extends Fn> = ((...args: Args<T>) => void) & {
|
|
6
|
+
cancel: () => void
|
|
7
|
+
flush: () => ReturnType<T> | undefined
|
|
8
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export * from './arrays'
|
|
2
|
+
export * from './buffers'
|
|
3
|
+
export * from './collections'
|
|
4
|
+
export * from './common'
|
|
5
|
+
export * from './core'
|
|
6
|
+
export * from './errors'
|
|
7
|
+
export * from './events'
|
|
8
|
+
export * from './functions'
|
|
9
|
+
export * from './json-rpc'
|
|
10
|
+
export * from './numbers'
|
|
11
|
+
export * from './objects'
|
|
12
|
+
export * from './promises'
|
|
13
|
+
export * from './serializer'
|
|
14
|
+
export * from './strings'
|
|
15
|
+
export * from './system'
|
|
16
|
+
export * from './times'
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export enum JsonRpcErrorCode {
|
|
2
|
+
ParseError = -32_700,
|
|
3
|
+
InternalError = -32_603,
|
|
4
|
+
InvalidParams = -32_602,
|
|
5
|
+
MethodNotFound = -32_601,
|
|
6
|
+
InvalidRequest = -32_600,
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export const JSON_RPC_ERROR_MESSAGES: Record<JsonRpcErrorCode, string> = {
|
|
10
|
+
[JsonRpcErrorCode.InternalError]: 'Internal error',
|
|
11
|
+
[JsonRpcErrorCode.InvalidParams]: 'Invalid params',
|
|
12
|
+
[JsonRpcErrorCode.InvalidRequest]: 'Invalid request',
|
|
13
|
+
[JsonRpcErrorCode.MethodNotFound]: 'Method not found',
|
|
14
|
+
[JsonRpcErrorCode.ParseError]: 'Parse error',
|
|
15
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { JsonRpcErrorObject, JsonRpcErrorResponseMessage, JsonRpcNotifyMessage, JsonRpcRequestMessage, JsonRpcResponseId, JsonRpcSuccessResponseMessage } from './types'
|
|
2
|
+
import { notUndefined } from '../core'
|
|
3
|
+
|
|
4
|
+
export const createJsonRpcNotifyMessage = <TParams>(method: string, params?: TParams): JsonRpcNotifyMessage<TParams> => ({
|
|
5
|
+
jsonrpc: '2.0', method, ...(notUndefined(params) ? { params } : {}),
|
|
6
|
+
})
|
|
7
|
+
|
|
8
|
+
export const createJsonRpcRequestMessage = <TParams>(id: number | string, method: string, params?: TParams): JsonRpcRequestMessage<TParams> => ({
|
|
9
|
+
id, jsonrpc: '2.0', method, ...(notUndefined(params) ? { params } : {}),
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
export const createJsonRpcSuccessResponseMessage = <TResult = any>(id: JsonRpcResponseId, result: TResult): JsonRpcSuccessResponseMessage<TResult> => ({
|
|
13
|
+
id, jsonrpc: '2.0', result,
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
export const createJsonRpcErrorObject = <TData>(code: number, message: string, data?: TData): JsonRpcErrorObject<TData> => ({
|
|
17
|
+
code, message, ...(notUndefined(data) ? { data } : {}),
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
export const createJsonRpcErrorResponseMessage = <TData>(id: JsonRpcResponseId, error: JsonRpcErrorObject<TData>): JsonRpcErrorResponseMessage<TData> => ({
|
|
21
|
+
error, id, jsonrpc: '2.0',
|
|
22
|
+
})
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { JsonRpcBatchRequest, JsonRpcBatchResponse, JsonRpcErrorObject, JsonRpcErrorResponseMessage, JsonRpcMessage, JsonRpcNotifyMessage, JsonRpcRequestMessage, JsonRpcResponseId, JsonRpcResponseMessage, JsonRpcResponseMessageWithNonNullId, JsonRpcSuccessResponseMessage } from './types'
|
|
2
|
+
import { isArray } from '../arrays'
|
|
3
|
+
import { isNull, isNumber, isString, notNullish } from '../core'
|
|
4
|
+
import { isKeysOf, isObject } from '../objects'
|
|
5
|
+
|
|
6
|
+
export const isJsonRpcMessage = <TParams = any, TResult = any, TErrorData = any>(message: unknown): message is JsonRpcMessage<TParams, TResult, TErrorData> => (
|
|
7
|
+
isObject(message) && message.jsonrpc === '2.0'
|
|
8
|
+
)
|
|
9
|
+
|
|
10
|
+
export const isValidJsonRpcId = (id: unknown): id is JsonRpcResponseId => isString(id) || isNumber(id) || isNull(id)
|
|
11
|
+
|
|
12
|
+
export const isJsonRpcRequestMessage = <TParams = any>(message: JsonRpcMessage): message is JsonRpcRequestMessage<TParams> => (
|
|
13
|
+
isKeysOf(message, 'id', 'method') && isValidJsonRpcId(message.id) && isString(message.method)
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
export const isJsonRpcNotifyMessage = <TParams = any>(message: JsonRpcMessage): message is JsonRpcNotifyMessage<TParams> => (
|
|
17
|
+
isKeysOf(message, 'method') && isString(message.method) && !isKeysOf(message, 'id')
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
export const isJsonRpcResponseMessage = <TResult = any, TErrorData = any>(message: JsonRpcMessage): message is JsonRpcResponseMessage<TResult, TErrorData> => (
|
|
21
|
+
isKeysOf(message, 'id') && isValidJsonRpcId(message.id) && !isKeysOf(message, 'method')
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
export const isJsonRpcError = <TData = any>(message: unknown): message is JsonRpcErrorObject<TData> => (
|
|
25
|
+
isObject(message) && isKeysOf(message, 'code', 'message')
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
export const isJsonRpcErrorResponseMessage = <TData = any>(message: JsonRpcMessage): message is JsonRpcErrorResponseMessage<TData> => (
|
|
29
|
+
isKeysOf(message, 'error') && isJsonRpcError(message.error)
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
export const isJsonRpcSuccessResponseMessage = <TResult = any>(message: JsonRpcMessage): message is JsonRpcSuccessResponseMessage<TResult> => (
|
|
33
|
+
isKeysOf(message, 'result') && !isKeysOf(message, 'error')
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
export const isJsonRpcResponseHasNonNullableId = <TResult = any, TErrorData = any>(response: JsonRpcResponseMessage): response is JsonRpcResponseMessageWithNonNullId<TResult, TErrorData> => (
|
|
37
|
+
notNullish(response.id)
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
export const isJsonRpcBatchRequest = <TParams = any>(value: unknown): value is JsonRpcBatchRequest<TParams> => (
|
|
41
|
+
isArray(value) && value.length > 0 && value.every((item) => {
|
|
42
|
+
if (!isJsonRpcMessage(item)) {
|
|
43
|
+
return false
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return isJsonRpcRequestMessage(item) || isJsonRpcNotifyMessage(item)
|
|
47
|
+
})
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
export const isJsonRpcBatchResponse = <TResult = any, TErrorData = any>(value: unknown): value is JsonRpcBatchResponse<TResult, TErrorData> => (
|
|
51
|
+
isArray(value) && value.length > 0 && value.every((item) => {
|
|
52
|
+
if (!isJsonRpcMessage(item)) {
|
|
53
|
+
return false
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return isJsonRpcResponseMessage(item)
|
|
57
|
+
})
|
|
58
|
+
)
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
export interface BaseJsonRpcMessage {
|
|
2
|
+
jsonrpc: '2.0'
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
export interface JsonRpcRequestMessage<TParams = any> extends BaseJsonRpcMessage {
|
|
6
|
+
id: number | string
|
|
7
|
+
method: string
|
|
8
|
+
params?: TParams
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface JsonRpcNotifyMessage<TParams = any> extends BaseJsonRpcMessage {
|
|
12
|
+
method: string
|
|
13
|
+
params?: TParams
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface JsonRpcErrorObject<TData = any> {
|
|
17
|
+
code: number
|
|
18
|
+
data?: TData
|
|
19
|
+
message: string
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export type JsonRpcResponseId = number | string | null
|
|
23
|
+
|
|
24
|
+
export interface BaseJsonRpcResponseMessage extends BaseJsonRpcMessage {
|
|
25
|
+
id: JsonRpcResponseId
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface JsonRpcSuccessResponseMessage<TResult = any> extends BaseJsonRpcResponseMessage {
|
|
29
|
+
result: TResult
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface JsonRpcErrorResponseMessage<TData = any> extends BaseJsonRpcResponseMessage {
|
|
33
|
+
error: JsonRpcErrorObject<TData>
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export type JsonRpcResponseMessage<TResult = any, TErrorData = any> = JsonRpcErrorResponseMessage<TErrorData> | JsonRpcSuccessResponseMessage<TResult>
|
|
37
|
+
|
|
38
|
+
export type JsonRpcResponseBodyWithoutId<TResult = any, TErrorData = any> = Omit<JsonRpcErrorResponseMessage<TErrorData>, 'id'> | Omit<JsonRpcSuccessResponseMessage<TResult>, 'id'>
|
|
39
|
+
|
|
40
|
+
export type JsonRpcResponseMessageWithNonNullId<TResult = any, TErrorData = any> = JsonRpcResponseBodyWithoutId<TResult, TErrorData> & {
|
|
41
|
+
id: NonNullable<JsonRpcResponseId>
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export type JsonRpcMessage<TParams = any, TResult = any, TErrorData = any> = JsonRpcNotifyMessage<TParams> | JsonRpcRequestMessage<TParams> | JsonRpcResponseMessage<TResult, TErrorData>
|
|
45
|
+
|
|
46
|
+
export type JsonRpcBatchRequest<TParams = any> = Array<JsonRpcNotifyMessage<TParams> | JsonRpcRequestMessage<TParams>>
|
|
47
|
+
|
|
48
|
+
export type JsonRpcBatchResponse<TResult = any, TErrorData = any> = Array<JsonRpcResponseMessage<TResult, TErrorData>>
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
export const BigIntMath = {
|
|
2
|
+
isEven(a: bigint) {
|
|
3
|
+
return a % 2n === 0n
|
|
4
|
+
},
|
|
5
|
+
|
|
6
|
+
isOdd(a: bigint) {
|
|
7
|
+
return a % 2n !== 0n
|
|
8
|
+
},
|
|
9
|
+
|
|
10
|
+
divMod(a: bigint, b: bigint): [quotient: bigint, remainder: bigint] {
|
|
11
|
+
if (b === 0n) {
|
|
12
|
+
throw new RangeError('Division by zero')
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return [a / b, a % b]
|
|
16
|
+
},
|
|
17
|
+
|
|
18
|
+
abs(a: bigint) {
|
|
19
|
+
return a < 0n ? -a : a
|
|
20
|
+
},
|
|
21
|
+
|
|
22
|
+
max(a: bigint, b: bigint) {
|
|
23
|
+
return a > b ? a : b
|
|
24
|
+
},
|
|
25
|
+
|
|
26
|
+
min(a: bigint, b: bigint) {
|
|
27
|
+
return a < b ? a : b
|
|
28
|
+
},
|
|
29
|
+
|
|
30
|
+
pow(a: bigint, b: bigint) {
|
|
31
|
+
return a ** b
|
|
32
|
+
},
|
|
33
|
+
|
|
34
|
+
sign(a: bigint) {
|
|
35
|
+
return a === 0n ? 0n : (a < 0n ? -1n : 1n)
|
|
36
|
+
},
|
|
37
|
+
|
|
38
|
+
mod(a: bigint, b: bigint) {
|
|
39
|
+
return ((a % b) + b) % b
|
|
40
|
+
},
|
|
41
|
+
|
|
42
|
+
modPow(base: bigint, exponent: bigint, modulus: bigint) {
|
|
43
|
+
if (exponent < 0n) {
|
|
44
|
+
throw new RangeError('Exponent must be non-negative')
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (modulus === 1n) {
|
|
48
|
+
return 0n
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
let result = 1n
|
|
52
|
+
|
|
53
|
+
base = BigIntMath.mod(base, modulus)
|
|
54
|
+
|
|
55
|
+
while (exponent > 0n) {
|
|
56
|
+
if (exponent % 2n === 1n) {
|
|
57
|
+
result = BigIntMath.mod(result * base, modulus)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
exponent >>= 1n
|
|
61
|
+
base = BigIntMath.mod(base * base, modulus)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return result
|
|
65
|
+
},
|
|
66
|
+
|
|
67
|
+
gcd(a: bigint, b: bigint) {
|
|
68
|
+
a = BigIntMath.abs(a)
|
|
69
|
+
b = BigIntMath.abs(b)
|
|
70
|
+
|
|
71
|
+
while (b !== 0n) {
|
|
72
|
+
[a, b] = [b, a % b]
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return a
|
|
76
|
+
},
|
|
77
|
+
|
|
78
|
+
lcm(a: bigint, b: bigint) {
|
|
79
|
+
return (a / BigIntMath.gcd(a, b)) * b
|
|
80
|
+
},
|
|
81
|
+
|
|
82
|
+
clamp(value: bigint, min: bigint, max: bigint) {
|
|
83
|
+
return BigIntMath.min(BigIntMath.max(value, min), max)
|
|
84
|
+
},
|
|
85
|
+
|
|
86
|
+
sqrt(n: bigint) {
|
|
87
|
+
if (n < 0n) {
|
|
88
|
+
throw new RangeError('Square root of negative bigint is undefined')
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (n < 2n) {
|
|
92
|
+
return n
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
let x = n
|
|
96
|
+
let y = (x + 1n) / 2n
|
|
97
|
+
|
|
98
|
+
while (y < x) {
|
|
99
|
+
x = y
|
|
100
|
+
y = (x + n / x) / 2n
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return x
|
|
104
|
+
},
|
|
105
|
+
|
|
106
|
+
factorial(n: bigint) {
|
|
107
|
+
if (n < 0n) {
|
|
108
|
+
throw new RangeError('Factorial of negative bigint is undefined')
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (n === 0n) {
|
|
112
|
+
return 1n
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
let result = 1n
|
|
116
|
+
|
|
117
|
+
for (let i = 1n; i <= n; i++) {
|
|
118
|
+
result *= i
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return result
|
|
122
|
+
},
|
|
123
|
+
|
|
124
|
+
sum(array: bigint[]) {
|
|
125
|
+
return array.reduce((a, b) => a + b, 0n)
|
|
126
|
+
},
|
|
127
|
+
|
|
128
|
+
avg(array: bigint[]) {
|
|
129
|
+
if (array.length === 0) {
|
|
130
|
+
return 0n
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return BigIntMath.sum(array) / BigInt(array.length)
|
|
134
|
+
},
|
|
135
|
+
|
|
136
|
+
isPrime(n: bigint) {
|
|
137
|
+
if (n < 2n) {
|
|
138
|
+
return false
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (n < 4n) {
|
|
142
|
+
return true
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if (n % 2n === 0n || n % 3n === 0n) {
|
|
146
|
+
return false
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const limit = BigIntMath.sqrt(n)
|
|
150
|
+
|
|
151
|
+
for (let i = 5n; i <= limit; i += 6n) {
|
|
152
|
+
if (n % i === 0n || n % (i + 2n) === 0n) {
|
|
153
|
+
return false
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
return true
|
|
158
|
+
},
|
|
159
|
+
|
|
160
|
+
compare(a: bigint, b: bigint) {
|
|
161
|
+
return a === b ? 0 : (a < b ? -1 : 1)
|
|
162
|
+
},
|
|
163
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export const SPECIAL_NUMBER_STRINGS = new Set(['+Infinity', '-Infinity', 'Infinity', 'NaN'])
|
|
2
|
+
|
|
3
|
+
export const SUBSCRIPT_CHARS: Record<string, string> = {
|
|
4
|
+
0: '\u2080',
|
|
5
|
+
1: '\u2081',
|
|
6
|
+
2: '\u2082',
|
|
7
|
+
3: '\u2083',
|
|
8
|
+
4: '\u2084',
|
|
9
|
+
5: '\u2085',
|
|
10
|
+
6: '\u2086',
|
|
11
|
+
7: '\u2087',
|
|
12
|
+
8: '\u2088',
|
|
13
|
+
9: '\u2089',
|
|
14
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import type { Numberish } from './types'
|
|
2
|
+
import { SUBSCRIPT_CHARS } from './constants'
|
|
3
|
+
import { roundTo } from './maths'
|
|
4
|
+
|
|
5
|
+
export function toSubscriptDigits(input: Numberish) {
|
|
6
|
+
let result = ''
|
|
7
|
+
|
|
8
|
+
for (const char of input.toString()) {
|
|
9
|
+
result += SUBSCRIPT_CHARS[char] ?? char
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
return result
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function parseExponential(input: Numberish) {
|
|
16
|
+
const str = input.toString().toLowerCase()
|
|
17
|
+
const [mantissa, rawExp] = str.split('e')
|
|
18
|
+
|
|
19
|
+
if (rawExp === undefined) {
|
|
20
|
+
return str
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const [rawInt, fracPart = ''] = mantissa?.split('.') ?? []
|
|
24
|
+
|
|
25
|
+
if (!rawInt) {
|
|
26
|
+
return str
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const sign = rawInt.startsWith('-') ? '-' : ''
|
|
30
|
+
const intPart = sign ? rawInt.slice(1) : rawInt
|
|
31
|
+
const exp = Number(rawExp)
|
|
32
|
+
|
|
33
|
+
if (exp >= 0) {
|
|
34
|
+
const neededZeros = Math.max(0, exp - fracPart.length)
|
|
35
|
+
const padded = fracPart + '0'.repeat(neededZeros)
|
|
36
|
+
|
|
37
|
+
if (padded.length > exp) {
|
|
38
|
+
return `${sign}${intPart}${padded.slice(0, exp)}.${padded.slice(exp)}`
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return `${sign}${intPart}${padded}`
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const absExp = -exp
|
|
45
|
+
const digits = intPart + fracPart
|
|
46
|
+
const totalLeadingZeros = Math.max(0, absExp - intPart.length)
|
|
47
|
+
|
|
48
|
+
return `${sign}0.${'0'.repeat(totalLeadingZeros)}${digits}`
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function toOrdinal(n: number) {
|
|
52
|
+
const remainder100 = Math.abs(n) % 100
|
|
53
|
+
const remainder10 = remainder100 % 10
|
|
54
|
+
|
|
55
|
+
if (remainder100 >= 11 && remainder100 <= 13) {
|
|
56
|
+
return `${n}th`
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
switch (remainder10) {
|
|
60
|
+
case 1:
|
|
61
|
+
return `${n}st`
|
|
62
|
+
case 2:
|
|
63
|
+
return `${n}nd`
|
|
64
|
+
case 3:
|
|
65
|
+
return `${n}rd`
|
|
66
|
+
default:
|
|
67
|
+
return `${n}th`
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function toPercent(value: number, total: number, decimals?: number) {
|
|
72
|
+
if (total === 0) {
|
|
73
|
+
return 0
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const result = (value / total) * 100
|
|
77
|
+
|
|
78
|
+
if (decimals === undefined) {
|
|
79
|
+
return result
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return roundTo(result, decimals)
|
|
83
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { Numberish } from './types'
|
|
2
|
+
import { parseExponential, toSubscriptDigits } from './conversions'
|
|
3
|
+
|
|
4
|
+
export function countLeadingZeros(input: string) {
|
|
5
|
+
let count = 0
|
|
6
|
+
|
|
7
|
+
for (let i = 0; i < input.length && input[i] === '0'; i++) {
|
|
8
|
+
count++
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
return count
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface FormatNumberOptions extends Intl.NumberFormatOptions {
|
|
15
|
+
formatLeadingZeros?: (count: number) => string
|
|
16
|
+
groupFractionLeadingZeros?: boolean
|
|
17
|
+
locales?: Intl.LocalesArgument
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function formatNumber(input: Numberish, options_: FormatNumberOptions = {}) {
|
|
21
|
+
const { formatLeadingZeros = (count: number) => `0${toSubscriptDigits(count)}`, groupFractionLeadingZeros = true, locales = 'en-US', maximumFractionDigits = 4, ...options } = options_
|
|
22
|
+
|
|
23
|
+
const formatInput = (digits: number) => (
|
|
24
|
+
new Intl.NumberFormat(locales, { ...options, maximumFractionDigits: digits }).format(input as Intl.StringNumericLiteral)
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
if (!groupFractionLeadingZeros) {
|
|
28
|
+
return formatInput(maximumFractionDigits)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const numericStr = parseExponential(input)
|
|
32
|
+
const [, fractionPart = ''] = numericStr.split('.', 2)
|
|
33
|
+
const leadingZerosCount = countLeadingZeros(fractionPart)
|
|
34
|
+
|
|
35
|
+
if (leadingZerosCount <= 1) {
|
|
36
|
+
return formatInput(maximumFractionDigits)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const replaceFractionZeros = (part: Intl.NumberFormatPart) => {
|
|
40
|
+
if (part.type === 'fraction') {
|
|
41
|
+
return `${formatLeadingZeros(leadingZerosCount)}${part.value.slice(leadingZerosCount)}`
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return part.value
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const parts = new Intl.NumberFormat(locales, {
|
|
48
|
+
...options, maximumFractionDigits: maximumFractionDigits + leadingZerosCount,
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
return parts.formatToParts(input as Intl.StringNumericLiteral).map(replaceFractionZeros).join('')
|
|
52
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { Numberish, NumberString, Percentage } from './types'
|
|
2
|
+
import { isBigInt, isNumber, isString } from '../core'
|
|
3
|
+
import { SPECIAL_NUMBER_STRINGS } from './constants'
|
|
4
|
+
|
|
5
|
+
// eslint-disable-next-line security/detect-unsafe-regex -- anchored regex with no backtracking risk on bounded input
|
|
6
|
+
const NUMERIC_PATTERN = /^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?$/iu
|
|
7
|
+
|
|
8
|
+
export const isSpecialNumberString = (value: string) => SPECIAL_NUMBER_STRINGS.has(value)
|
|
9
|
+
|
|
10
|
+
export function isNumberString<TStrict extends boolean = true>(value: string): value is NumberString<TStrict> {
|
|
11
|
+
if (isSpecialNumberString(value)) {
|
|
12
|
+
return true
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return NUMERIC_PATTERN.test(value)
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export const isNumberish = <TStrict extends boolean = true>(input: unknown): input is Numberish<TStrict> => (
|
|
19
|
+
isNumber(input) || isBigInt(input) || (isString(input) && isNumberString(input))
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
export const isPercentage = (value: number): value is Percentage => value >= 0 && value <= 100
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from './bigint-math'
|
|
2
|
+
export * from './constants'
|
|
3
|
+
export * from './conversions'
|
|
4
|
+
export * from './factories'
|
|
5
|
+
export * from './formats'
|
|
6
|
+
export * from './guards'
|
|
7
|
+
export * from './maths'
|
|
8
|
+
export * from './ranges'
|
|
9
|
+
|
|
10
|
+
export type * from './types'
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { transform } from '../functions'
|
|
2
|
+
|
|
3
|
+
export const sum = (array: number[]) => array.reduce((a, b) => a + b, 0)
|
|
4
|
+
|
|
5
|
+
export const avg = (array: number[]) => (array.length === 0 ? 0 : sum(array) / array.length)
|
|
6
|
+
|
|
7
|
+
export const clamp = (value: number, min: number, max: number) => Math.min(Math.max(value, min), max)
|
|
8
|
+
|
|
9
|
+
export const roundTo = (value: number, decimals: number) => (
|
|
10
|
+
transform(10 ** decimals, (factor) => Math.round(value * factor) / factor)
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
export const lerp = (start: number, end: number, t: number) => start + (end - start) * t
|
|
14
|
+
|
|
15
|
+
export function median(array: number[]) {
|
|
16
|
+
if (array.length === 0) {
|
|
17
|
+
return 0
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const sorted = [...array].toSorted((a, b) => a - b)
|
|
21
|
+
const mid = Math.floor(sorted.length / 2)
|
|
22
|
+
|
|
23
|
+
if (sorted.length % 2 === 0) {
|
|
24
|
+
return (sorted[mid - 1]! + sorted[mid]!) / 2
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return sorted[mid]!
|
|
28
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { notNullish } from '../core'
|
|
2
|
+
|
|
3
|
+
export function isValidRange<T extends bigint | number>(start: T, end: T, inclusive = true, min?: T, max?: T) {
|
|
4
|
+
if (notNullish(min) && start < min) {
|
|
5
|
+
return false
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
if (notNullish(max) && end > max) {
|
|
9
|
+
return false
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
return inclusive ? start <= end : start < end
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const isInRange = <T extends bigint | number>(value: T, min: T, max: T, inclusive = true) => (
|
|
16
|
+
inclusive ? value >= min && value <= max : value > min && value < max
|
|
17
|
+
)
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export type SpecialNumberString = '+Infinity' | '-Infinity' | 'Infinity' | 'NaN'
|
|
2
|
+
|
|
3
|
+
export type NumberString<TStrict extends boolean = false> = SpecialNumberString | (TStrict extends true ? `${number}` : string)
|
|
4
|
+
|
|
5
|
+
export type Numberish<TStrict extends boolean = false> = NumberString<TStrict> | bigint | number
|
|
6
|
+
|
|
7
|
+
export type Percentage = { __brand: 'Percentage' } & number
|