object-array-utils
Advanced tools
+4
-4
| { | ||
| "name": "object-array-utils", | ||
| "version": "2.12.0", | ||
| "version": "2.13.0", | ||
| "description": "Utilities for working with arrays and objects", | ||
@@ -10,5 +10,5 @@ "scripts": { | ||
| "devDependencies": { | ||
| "@babel/core": "^7.19.6", | ||
| "@babel/preset-env": "^7.19.4", | ||
| "jest": "^29.2.2" | ||
| "@babel/core": "^7.21.3", | ||
| "@babel/preset-env": "^7.20.2", | ||
| "jest": "^29.5.0" | ||
| }, | ||
@@ -15,0 +15,0 @@ "dependencies": {}, |
+11
-0
@@ -61,2 +61,13 @@ declare module "object-array-utils" { | ||
| function sortProperties(o: Record<string, unknown>): Record<string, unknown>; | ||
| type RangeOptions = { | ||
| start?: number; | ||
| count?: number; | ||
| endInclusive?: number; | ||
| endExclusive?: number; | ||
| }; | ||
| function range(o: RangeOptions): number[]; | ||
| function duplicate(value: unknown, count: number): unknown[]; | ||
| } |
+31
-0
@@ -478,2 +478,31 @@ function isNullOrUndefined(v) { | ||
| function range(options) { | ||
| const unknownOptions = rejectProperties(options, ['start', 'count', 'endInclusive', 'endExclusive']); | ||
| if (!isEmptyObjectLiteral(unknownOptions)) { | ||
| throw new Error(`unknown options: ${Object.keys(unknownOptions).join(', ')}`); | ||
| } | ||
| if (!options.endInclusive && !options.endExclusive && !options.count) { | ||
| throw new Error('expected either `endInclusive`, `endExclusive` or `count` to be specified'); | ||
| } | ||
| if (options.endInclusive && options.start > options.endInclusive) { | ||
| throw new Error('`endInclusive` should be greater or equal than `start`'); | ||
| } | ||
| if (options.endExclusive && options.start >= options.endExclusive) { | ||
| throw new Error('`endExclusive` should be greater than `start`'); | ||
| } | ||
| const start = options.start ?? 0; | ||
| const count = options.count ?? ((options.endInclusive) ? options.endInclusive - start + 1 : options.endExclusive - start); | ||
| return [...Array(count).keys()].map(i => i + start); | ||
| } | ||
| function duplicate(value, count) { | ||
| return Array.from({ length: count }, () => value); | ||
| } | ||
| export { | ||
@@ -484,2 +513,3 @@ areArraysEqual, | ||
| deepFreeze, | ||
| duplicate, | ||
| filterProperties, | ||
@@ -504,2 +534,3 @@ hasProperty, | ||
| isPrimitive, | ||
| range, | ||
| rejectProperties, | ||
@@ -506,0 +537,0 @@ removeArrayElement, |
+17
-0
@@ -6,2 +6,3 @@ import { | ||
| deepFreeze, | ||
| duplicate, | ||
| filterProperties, | ||
@@ -17,2 +18,3 @@ hasProperties, | ||
| isPrimitive, | ||
| range, | ||
| rejectProperties, | ||
@@ -245,1 +247,16 @@ removeArrayElement, | ||
| }); | ||
| test('range', () => { | ||
| expect(range({ count: 2 })).toEqual([0, 1]); | ||
| expect(range({ endInclusive: 2 })).toEqual([0, 1, 2]); | ||
| expect(range({ endExclusive: 2 })).toEqual([0, 1]); | ||
| expect(range({ start: 5, count: 2 })).toEqual([5, 6]); | ||
| expect(range({ start: 5, endInclusive: 7 })).toEqual([5, 6, 7]); | ||
| expect(range({ start: 5, endInclusive: 5 })).toEqual([5]); | ||
| expect(range({ start: 5, endExclusive: 7 })).toEqual([5, 6]); | ||
| }); | ||
| test('duplicate', () => { | ||
| expect(duplicate(2, 3)).toEqual([2, 2, 2]); | ||
| expect(duplicate(['foo', 0], 3)).toEqual([['foo', 0], ['foo', 0], ['foo', 0]]); | ||
| }); |
43228
4.66%679
7.27%