@ayonli/jsext
Advanced tools
Comparing version 0.6.30 to 0.6.31
@@ -13,3 +13,4 @@ import { | ||
split as _split, | ||
uniq as _uniq | ||
uniq as _uniq, | ||
uniqBy as _uniqBy, | ||
} from "./index.ts"; | ||
@@ -39,2 +40,7 @@ | ||
/** | ||
* Returns a subset of the array that contains only unique items filtered by the | ||
* given callback function. | ||
*/ | ||
uniqBy<K extends string | number | symbol>(fn: (item: T, i: number) => K): T[]; | ||
/** | ||
* Reorganizes the elements in the array in random order. | ||
@@ -53,2 +59,4 @@ * | ||
orderBy(key: keyof T, order?: "asc" | "desc"): T[]; | ||
/** Orders the items of the array according to the given callback function. */ | ||
orderBy(fn: (item: T, i: number) => string | number | bigint, order?: "asc" | "desc"): T[]; | ||
/** | ||
@@ -107,2 +115,6 @@ * Groups the items of the array according to the comparable values returned by a provided | ||
Array.prototype.uniqBy = function uniqBy(fn) { | ||
return _uniqBy(this, fn); | ||
}; | ||
Array.prototype.shuffle = function shuffle() { | ||
@@ -129,6 +141,6 @@ return _shuffle(this); | ||
Array.prototype.orderBy = function orderBy(key, order = "asc") { | ||
return _orderBy(this, key, order); | ||
return _orderBy(this, key as any, order); | ||
}; | ||
Array.prototype.groupBy = function orderBy( | ||
Array.prototype.groupBy = function groupBy( | ||
fn: (item: any, i: number) => any, | ||
@@ -135,0 +147,0 @@ type: ObjectConstructor | MapConstructor = Object |
@@ -70,2 +70,21 @@ import { isSubclassOf } from "../mixins.ts"; | ||
/** | ||
* Returns a subset of the array that contains only unique items filtered by the | ||
* given callback function. | ||
*/ | ||
export function uniqBy<T, K extends string | number | symbol>( | ||
arr: T[], | ||
fn: (item: T, i: number) => K | ||
): T[] { | ||
const map = new Map() as Map<K, T>; | ||
for (let i = 0; i < arr.length; i++) { | ||
const item = arr[i] as T; | ||
const key = fn(item, i); | ||
map.has(key) || map.set(key, item); | ||
} | ||
return [...map.values()]; | ||
} | ||
/** | ||
* Reorganizes the elements in the array in random order. | ||
@@ -88,4 +107,23 @@ * | ||
*/ | ||
export function orderBy<T>(arr: T[], key: keyof T, order: "asc" | "desc" = "asc"): T[] { | ||
export function orderBy<T>(arr: T[], key: keyof T, order?: "asc" | "desc"): T[]; | ||
/** Orders the items of the array according to the given callback function. */ | ||
export function orderBy<T>( | ||
arr: T[], | ||
fn: (item: T, i: number) => string | number | bigint, | ||
order?: "asc" | "desc" | ||
): T[]; | ||
export function orderBy<T>( | ||
arr: T[], | ||
key: keyof T | ((item: T, i: number) => string | number | bigint), | ||
order: "asc" | "desc" = "asc" | ||
): T[] { | ||
const items = arr.slice(); | ||
if (typeof key === "function") { | ||
return orderBy(items.map((item, i) => ({ | ||
key: key(item, i), | ||
value: item, | ||
})), "key", order).map(({ value }) => value); | ||
} | ||
items.sort((a, b) => { | ||
@@ -92,0 +130,0 @@ if (typeof a !== "object" || typeof b !== "object" || |
@@ -29,2 +29,5 @@ 'use strict'; | ||
}; | ||
Array.prototype.uniqBy = function uniqBy(fn) { | ||
return array_index.uniqBy(this, fn); | ||
}; | ||
Array.prototype.shuffle = function shuffle() { | ||
@@ -49,3 +52,3 @@ return array_index.shuffle(this); | ||
}; | ||
Array.prototype.groupBy = function orderBy(fn, type = Object) { | ||
Array.prototype.groupBy = function groupBy(fn, type = Object) { | ||
return array_index.groupBy(this, fn, type); | ||
@@ -52,0 +55,0 @@ }; |
@@ -60,2 +60,15 @@ 'use strict'; | ||
/** | ||
* Returns a subset of the array that contains only unique items filtered by the | ||
* given callback function. | ||
*/ | ||
function uniqBy(arr, fn) { | ||
const map = new Map(); | ||
for (let i = 0; i < arr.length; i++) { | ||
const item = arr[i]; | ||
const key = fn(item, i); | ||
map.has(key) || map.set(key, item); | ||
} | ||
return [...map.values()]; | ||
} | ||
/** | ||
* Reorganizes the elements in the array in random order. | ||
@@ -72,8 +85,10 @@ * | ||
} | ||
/** | ||
* Orders the items of the array according to the specified comparable `key` (whose value | ||
* must either be a numeric or string). | ||
*/ | ||
function orderBy(arr, key, order = "asc") { | ||
const items = arr.slice(); | ||
if (typeof key === "function") { | ||
return orderBy(items.map((item, i) => ({ | ||
key: key(item, i), | ||
value: item, | ||
})), "key", order).map(({ value }) => value); | ||
} | ||
items.sort((a, b) => { | ||
@@ -179,2 +194,3 @@ if (typeof a !== "object" || typeof b !== "object" || | ||
exports.uniq = uniq; | ||
exports.uniqBy = uniqBy; | ||
//# sourceMappingURL=index.js.map |
@@ -1,2 +0,2 @@ | ||
import { first, last, random, count, equals, split, chunk, uniq, shuffle, orderBy, groupBy, keyBy } from './index.js'; | ||
import { first, last, random, count, equals, split, chunk, uniq, uniqBy, shuffle, orderBy, groupBy, keyBy } from './index.js'; | ||
@@ -27,2 +27,5 @@ Array.prototype.first = function first$1() { | ||
}; | ||
Array.prototype.uniqBy = function uniqBy$1(fn) { | ||
return uniqBy(this, fn); | ||
}; | ||
Array.prototype.shuffle = function shuffle$1() { | ||
@@ -47,3 +50,3 @@ return shuffle(this); | ||
}; | ||
Array.prototype.groupBy = function orderBy(fn, type = Object) { | ||
Array.prototype.groupBy = function groupBy$1(fn, type = Object) { | ||
return groupBy(this, fn, type); | ||
@@ -50,0 +53,0 @@ }; |
@@ -58,2 +58,15 @@ import { isSubclassOf } from '../mixins.js'; | ||
/** | ||
* Returns a subset of the array that contains only unique items filtered by the | ||
* given callback function. | ||
*/ | ||
function uniqBy(arr, fn) { | ||
const map = new Map(); | ||
for (let i = 0; i < arr.length; i++) { | ||
const item = arr[i]; | ||
const key = fn(item, i); | ||
map.has(key) || map.set(key, item); | ||
} | ||
return [...map.values()]; | ||
} | ||
/** | ||
* Reorganizes the elements in the array in random order. | ||
@@ -70,8 +83,10 @@ * | ||
} | ||
/** | ||
* Orders the items of the array according to the specified comparable `key` (whose value | ||
* must either be a numeric or string). | ||
*/ | ||
function orderBy(arr, key, order = "asc") { | ||
const items = arr.slice(); | ||
if (typeof key === "function") { | ||
return orderBy(items.map((item, i) => ({ | ||
key: key(item, i), | ||
value: item, | ||
})), "key", order).map(({ value }) => value); | ||
} | ||
items.sort((a, b) => { | ||
@@ -165,3 +180,3 @@ if (typeof a !== "object" || typeof b !== "object" || | ||
export { chunk, count, equals, first, groupBy, keyBy, last, orderBy, random, shuffle, split, uniq }; | ||
export { chunk, count, equals, first, groupBy, keyBy, last, orderBy, random, shuffle, split, uniq, uniqBy }; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@ayonli/jsext", | ||
"version": "0.6.30", | ||
"version": "0.6.31", | ||
"description": "Additional functions for JavaScript to build strong applications.", | ||
@@ -5,0 +5,0 @@ "main": "./cjs/index.js", |
@@ -23,2 +23,7 @@ declare global { | ||
/** | ||
* Returns a subset of the array that contains only unique items filtered by the | ||
* given callback function. | ||
*/ | ||
uniqBy<K extends string | number | symbol>(fn: (item: T, i: number) => K): T[]; | ||
/** | ||
* Reorganizes the elements in the array in random order. | ||
@@ -37,2 +42,4 @@ * | ||
orderBy(key: keyof T, order?: "asc" | "desc"): T[]; | ||
/** Orders the items of the array according to the given callback function. */ | ||
orderBy(fn: (item: T, i: number) => string | number | bigint, order?: "asc" | "desc"): T[]; | ||
/** | ||
@@ -39,0 +46,0 @@ * Groups the items of the array according to the comparable values returned by a provided |
@@ -21,2 +21,7 @@ /** Returns the first element of the array, or `undefined` if the array is empty. */ | ||
/** | ||
* Returns a subset of the array that contains only unique items filtered by the | ||
* given callback function. | ||
*/ | ||
export declare function uniqBy<T, K extends string | number | symbol>(arr: T[], fn: (item: T, i: number) => K): T[]; | ||
/** | ||
* Reorganizes the elements in the array in random order. | ||
@@ -32,2 +37,4 @@ * | ||
export declare function orderBy<T>(arr: T[], key: keyof T, order?: "asc" | "desc"): T[]; | ||
/** Orders the items of the array according to the given callback function. */ | ||
export declare function orderBy<T>(arr: T[], fn: (item: T, i: number) => string | number | bigint, order?: "asc" | "desc"): T[]; | ||
/** | ||
@@ -34,0 +41,0 @@ * Groups the items of the array according to the comparable values returned by a provided |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
1261132
17266