@ariesclark/array
Advanced tools
Comparing version 0.2.4 to 0.3.0
@@ -1,6 +0,7 @@ | ||
import type { RandomFunction } from "@ariesclark/number"; | ||
import { type RandomFunction } from "@ariesclark/number/random-function"; | ||
/** | ||
* Get a random index from an array, with an optional random number generator. | ||
* Get a random index from an array. | ||
* | ||
* @example | ||
* ```typescript | ||
* const value = ["lorem", "ipsum", "dolor", "sit", "amet"]; | ||
@@ -11,2 +12,3 @@ * | ||
* randomIndex(value); // 2, eventually. | ||
* ``` | ||
*/ | ||
@@ -13,0 +15,0 @@ export declare function randomIndex(array: { |
@@ -1,3 +0,3 @@ | ||
import { randomFloat } from "@ariesclark/number/random-float"; | ||
export function randomIndex(array, random = randomFloat) { | ||
import { randomFunction } from "@ariesclark/number/random-function"; | ||
export function randomIndex(array, random = randomFunction()) { | ||
if (array.length === 0) | ||
@@ -4,0 +4,0 @@ return null; |
@@ -1,2 +0,2 @@ | ||
import type { RandomFunction } from "@ariesclark/number"; | ||
import type { RandomFunction } from "@ariesclark/number/random-function"; | ||
/** | ||
@@ -3,0 +3,0 @@ * Get a random item from an array, with an optional random number generator. |
@@ -1,4 +0,3 @@ | ||
import { randomFloat } from "@ariesclark/number/random-float"; | ||
import { randomIndex } from "./random-index"; | ||
export function randomItem(array, random = randomFloat) { | ||
export function randomItem(array, random) { | ||
if (array.length === 0) | ||
@@ -5,0 +4,0 @@ return null; |
/** | ||
* Returns a new array with unique values based on the `by` function. | ||
* | ||
* If multiple values are considered equal, the first value is kept. | ||
* | ||
* @param array The array to filter. | ||
* @param by The function to determine uniqueness, defaulting to {@link identity}. | ||
* | ||
* @example | ||
* ```typescript | ||
* import { unique } from "@ariesclark/array"; | ||
* import { identity } from "@ariesclark/object"; | ||
* | ||
* // without `by`, uses the identity function. | ||
* unique([1, 1, 2]); // [1, 2] | ||
* unique([1, 1, 2], identity); // [1, 2], same as above. | ||
* | ||
* unique([ | ||
* { id: 1 }, | ||
* { id: 1 }, | ||
* { id: 2 } | ||
* ], ({ id }) => id); | ||
* // [{ id: 1 }, { id: 2 }] | ||
* ``` | ||
*/ | ||
export declare function unique<T>(array: Array<T>, by: (value: T) => unknown): NonNullable<T>[]; | ||
export declare function unique<T>(array: ArrayLike<T> & Iterable<T>, by?: (value: T) => unknown): Array<T>; |
@@ -0,11 +1,42 @@ | ||
import { identity } from "@ariesclark/object/identity"; | ||
/** | ||
* Returns a new array with unique values based on the `by` function. | ||
* | ||
* If multiple values are considered equal, the first value is kept. | ||
* | ||
* @param array The array to filter. | ||
* @param by The function to determine uniqueness, defaulting to {@link identity}. | ||
* | ||
* @example | ||
* ```typescript | ||
* import { unique } from "@ariesclark/array"; | ||
* import { identity } from "@ariesclark/object"; | ||
* | ||
* // without `by`, uses the identity function. | ||
* unique([1, 1, 2]); // [1, 2] | ||
* unique([1, 1, 2], identity); // [1, 2], same as above. | ||
* | ||
* unique([ | ||
* { id: 1 }, | ||
* { id: 1 }, | ||
* { id: 2 } | ||
* ], ({ id }) => id); | ||
* // [{ id: 1 }, { id: 2 }] | ||
* ``` | ||
*/ | ||
export function unique(array, by) { | ||
export function unique(array, by = identity) { | ||
if (array.length === 0) | ||
return []; | ||
return [...new Set(array.map((value) => by(value)))].map((key) => { | ||
// Fast path for identity function, using a Set. | ||
if (by === identity) | ||
return [...new Set(array)]; | ||
const seen = new Map(); | ||
for (const element of array) { | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
return array.find((value) => by(value) === key); | ||
}); | ||
const value = element; | ||
const key = by(value); | ||
if (!seen.has(key)) | ||
seen.set(key, value); | ||
} | ||
return [...seen.values()]; | ||
} |
@@ -5,3 +5,3 @@ { | ||
"sideEffects": false, | ||
"version": "0.2.4", | ||
"version": "0.3.0", | ||
"description": "Fast, efficient, and easy-to-use array extensions for TypeScript.", | ||
@@ -28,12 +28,12 @@ "files": [ | ||
"dependencies": { | ||
"@ariesclark/number": "^0.2.3" | ||
"@ariesclark/number": "^0.3.0" | ||
}, | ||
"devDependencies": { | ||
"ts-readme": "^1.1.3", | ||
"@ariesclark/object": "^0.2.3" | ||
"@ariesclark/object": "^0.3.0" | ||
}, | ||
"scripts": { | ||
"build": "rm -rf dist/* && tsc && ts-readme", | ||
"build": "rm -rf dist/* && tsc", | ||
"test": "bun test" | ||
} | ||
} |
@@ -49,2 +49,9 @@ <div align="center"> | ||
</a> | ||
<a href="https://npm.im/@ariesclark/time"> | ||
<img | ||
src="https://files.aries.fyi/2024/04/01/d668dcdee6a6b8ce.png" | ||
alt="@ariesclark/time" | ||
width="32%" | ||
/> | ||
</a> | ||
</div> | ||
@@ -78,3 +85,3 @@ | ||
Get a random index from an array, with an optional random number generator. | ||
Get a random index from an array. | ||
@@ -88,3 +95,3 @@ **Parameters:** | ||
```tsx | ||
```typescript | ||
const value = ['lorem', 'ipsum', 'dolor', 'sit', 'amet']; | ||
@@ -91,0 +98,0 @@ |
9618
169
170
+ Added@ariesclark/number@0.3.1(transitive)
- Removed@ariesclark/number@0.2.3(transitive)
Updated@ariesclark/number@^0.3.0