extra-utils 5.5.2 → 5.7.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/README.md CHANGED
@@ -72,6 +72,11 @@ function isntNaN(val: number): boolean
72
72
 
73
73
  function isBigInt(val: unknown): val is bigint
74
74
  function isntBigInt<T>(val: T): val is Exclude<T, bigint>
75
+
76
+ function clamp(
77
+ val: number
78
+ , [min, max]: [min: number, max: number]
79
+ ): number
75
80
  ```
76
81
 
77
82
  ### Object
@@ -209,6 +214,11 @@ function enumValues<T extends Record<string, string | number>>(
209
214
  function enumEntries<T extends Record<string, string | number>>(
210
215
  _enum: T
211
216
  ): Array<{ [Key in keyof T]: [Key, T[Key]] }[keyof T]>
217
+
218
+ function getEnumKey<T extends Record<string, string | number>>(
219
+ _enum: T
220
+ , enumValue: T[keyof T]
221
+ ): keyof T
212
222
  ```
213
223
 
214
224
  ### Date
@@ -0,0 +1 @@
1
+ export declare function getEnumKey<T extends Record<string, string | number>>(_enum: T, enumValue: T[keyof T]): keyof T;
@@ -0,0 +1,10 @@
1
+ import { enumEntries } from './enum-entries.js';
2
+ export function getEnumKey(_enum, enumValue) {
3
+ for (const [key, value] of enumEntries(_enum)) {
4
+ if (value === enumValue) {
5
+ return key;
6
+ }
7
+ }
8
+ throw new Error('There are no enumeration items matching the given value.');
9
+ }
10
+ //# sourceMappingURL=get-enum-key.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"get-enum-key.js","sourceRoot":"","sources":["../../src/enum/get-enum-key.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAE/C,MAAM,UAAU,UAAU,CACxB,KAAQ,EACR,SAAqB;IAErB,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,WAAW,CAAC,KAAK,CAAC,EAAE;QAC7C,IAAI,KAAK,KAAK,SAAS,EAAE;YACvB,OAAO,GAAG,CAAA;SACX;KACF;IAED,MAAM,IAAI,KAAK,CAAC,0DAA0D,CAAC,CAAA;AAC7E,CAAC"}
@@ -2,3 +2,4 @@ export * from './in-enum.js';
2
2
  export * from './enum-keys.js';
3
3
  export * from './enum-values.js';
4
4
  export * from './enum-entries.js';
5
+ export * from './get-enum-key.js';
package/lib/enum/index.js CHANGED
@@ -2,4 +2,5 @@ export * from './in-enum.js';
2
2
  export * from './enum-keys.js';
3
3
  export * from './enum-values.js';
4
4
  export * from './enum-entries.js';
5
+ export * from './get-enum-key.js';
5
6
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/enum/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAA;AAC5B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,kBAAkB,CAAA;AAChC,cAAc,mBAAmB,CAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/enum/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAA;AAC5B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,kBAAkB,CAAA;AAChC,cAAc,mBAAmB,CAAA;AACjC,cAAc,mBAAmB,CAAA"}
@@ -0,0 +1 @@
1
+ export declare function clamp(val: number, [min, max]: [min: number, max: number]): number;
@@ -0,0 +1,4 @@
1
+ export function clamp(val, [min, max]) {
2
+ return Math.max(min, Math.min(val, max));
3
+ }
4
+ //# sourceMappingURL=clamp.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"clamp.js","sourceRoot":"","sources":["../../src/number/clamp.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,KAAK,CACnB,GAAW,EACX,CAAC,GAAG,EAAE,GAAG,CAA6B;IAEtC,OAAO,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,CAAA;AAC1C,CAAC"}
@@ -3,3 +3,4 @@ export * from './is-finite.js';
3
3
  export * from './is-infinity.js';
4
4
  export * from './is-nan.js';
5
5
  export * from './is-number.js';
6
+ export * from './clamp.js';
@@ -3,4 +3,5 @@ export * from './is-finite.js';
3
3
  export * from './is-infinity.js';
4
4
  export * from './is-nan.js';
5
5
  export * from './is-number.js';
6
+ export * from './clamp.js';
6
7
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/number/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAA;AAC9B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,kBAAkB,CAAA;AAChC,cAAc,aAAa,CAAA;AAC3B,cAAc,gBAAgB,CAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/number/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAA;AAC9B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,kBAAkB,CAAA;AAChC,cAAc,aAAa,CAAA;AAC3B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,YAAY,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "extra-utils",
3
- "version": "5.5.2",
3
+ "version": "5.7.0",
4
4
  "description": "Utilities for JavaScript and Typescript",
5
5
  "files": [
6
6
  "src",
@@ -0,0 +1,14 @@
1
+ import { enumEntries } from './enum-entries.js'
2
+
3
+ export function getEnumKey<T extends Record<string, string | number>>(
4
+ _enum: T
5
+ , enumValue: T[keyof T]
6
+ ): keyof T {
7
+ for (const [key, value] of enumEntries(_enum)) {
8
+ if (value === enumValue) {
9
+ return key
10
+ }
11
+ }
12
+
13
+ throw new Error('There are no enumeration items matching the given value.')
14
+ }
package/src/enum/index.ts CHANGED
@@ -2,3 +2,4 @@ export * from './in-enum.js'
2
2
  export * from './enum-keys.js'
3
3
  export * from './enum-values.js'
4
4
  export * from './enum-entries.js'
5
+ export * from './get-enum-key.js'
@@ -0,0 +1,6 @@
1
+ export function clamp(
2
+ val: number
3
+ , [min, max]: [min: number, max: number]
4
+ ): number {
5
+ return Math.max(min, Math.min(val, max))
6
+ }
@@ -3,3 +3,4 @@ export * from './is-finite.js'
3
3
  export * from './is-infinity.js'
4
4
  export * from './is-nan.js'
5
5
  export * from './is-number.js'
6
+ export * from './clamp.js'