es-toolkit 1.24.0-dev.778 → 1.24.0-dev.779
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/dist/browser.global.js +1 -1
- package/dist/browser.global.js.map +1 -1
- package/dist/compat/array/sample.d.mts +80 -0
- package/dist/compat/array/sample.d.ts +80 -0
- package/dist/compat/array/sample.mjs +14 -0
- package/dist/compat/index.d.mts +1 -1
- package/dist/compat/index.d.ts +1 -1
- package/dist/compat/index.js +11 -1
- package/dist/compat/index.mjs +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a random element from an array.
|
|
3
|
+
*
|
|
4
|
+
* @template T
|
|
5
|
+
* @param {T[]} array - The array to sample from.
|
|
6
|
+
* @returns {T | undefined} A random element from the array, or `undefined` if the array is empty.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* const array = [1, 2, 3];
|
|
10
|
+
* const result = sample(array);
|
|
11
|
+
* console.log(result); // Output: 1, 2, or 3 (randomly selected)
|
|
12
|
+
*/
|
|
13
|
+
declare function sample<T>(array: readonly T[]): T | undefined;
|
|
14
|
+
/**
|
|
15
|
+
* Returns a random character from a string.
|
|
16
|
+
*
|
|
17
|
+
* @param {string} str - The string to sample from.
|
|
18
|
+
* @returns {string | undefined} A random character from the string, or `undefined` if the string is empty.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* const str = "hello";
|
|
22
|
+
* const result = sample(str);
|
|
23
|
+
* console.log(result); // Output: 'h', 'e', 'l', 'l', or 'o' (randomly selected)
|
|
24
|
+
*/
|
|
25
|
+
declare function sample(str: string): string | undefined;
|
|
26
|
+
/**
|
|
27
|
+
* Returns a random element from an array.
|
|
28
|
+
*
|
|
29
|
+
* @template T
|
|
30
|
+
* @param {ArrayLike<T>} array - The array-like object to sample from.
|
|
31
|
+
* @returns {T | undefined} A random element from the array, or `undefined` if the array is empty.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* const arrayLike: ArrayLike<string> = { 0: 'a', 1: 'b', 2: 'c', length: 3 };
|
|
35
|
+
* const result = sample(arrayLike);
|
|
36
|
+
* console.log(result); // Output: 'a', 'b', or 'c' (randomly selected)
|
|
37
|
+
*/
|
|
38
|
+
declare function sample<T>(array: ArrayLike<T>): T | undefined;
|
|
39
|
+
/**
|
|
40
|
+
* Returns a random value from an object.
|
|
41
|
+
*
|
|
42
|
+
* @template T - The type of values in the object.
|
|
43
|
+
* @param {Record<string, T>} obj - The object to sample from.
|
|
44
|
+
* @returns {T | undefined} A random value from the object, or `undefined` if the object is empty.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* const obj = { a: 1, b: 2, c: 3 };
|
|
48
|
+
* const result = sample(obj);
|
|
49
|
+
* console.log(result); // Output: 1, 2, or 3 (randomly selected)
|
|
50
|
+
*/
|
|
51
|
+
declare function sample<T>(obj: Record<string, T>): T | undefined;
|
|
52
|
+
/**
|
|
53
|
+
* Returns a random element from an array-like object or a regular object.
|
|
54
|
+
*
|
|
55
|
+
* This function takes an array-like object (such as an array or string) or a regular object,
|
|
56
|
+
* and returns a randomly selected element or value. If the collection is empty or invalid, it returns `undefined`.
|
|
57
|
+
*
|
|
58
|
+
* @template T - The type of elements in the collection.
|
|
59
|
+
* @param {ArrayLike<T> | Record<string, T>} collection - The collection to sample from.
|
|
60
|
+
* @returns {T | string | undefined} A random element from the collection, or `undefined` if the collection is empty or invalid.
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* // Array example
|
|
64
|
+
* const array = [1, 2, 3];
|
|
65
|
+
* const result = sample(array);
|
|
66
|
+
* console.log(result); // Output: 1, 2, or 3 (randomly selected)
|
|
67
|
+
*
|
|
68
|
+
* // String example
|
|
69
|
+
* const str = 'abc';
|
|
70
|
+
* const result2 = sample(str);
|
|
71
|
+
* console.log(result2); // Output: 'a', 'b', or 'c' (randomly selected)
|
|
72
|
+
*
|
|
73
|
+
* // Object example
|
|
74
|
+
* const obj = { a: 1, b: 2, c: 3 };
|
|
75
|
+
* const result3 = sample(obj);
|
|
76
|
+
* console.log(result3); // Output: 1, 2, or 3 (randomly selected)
|
|
77
|
+
*/
|
|
78
|
+
declare function sample<T>(collection: ArrayLike<T> | Record<string, T>): T | string | undefined;
|
|
79
|
+
|
|
80
|
+
export { sample };
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a random element from an array.
|
|
3
|
+
*
|
|
4
|
+
* @template T
|
|
5
|
+
* @param {T[]} array - The array to sample from.
|
|
6
|
+
* @returns {T | undefined} A random element from the array, or `undefined` if the array is empty.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* const array = [1, 2, 3];
|
|
10
|
+
* const result = sample(array);
|
|
11
|
+
* console.log(result); // Output: 1, 2, or 3 (randomly selected)
|
|
12
|
+
*/
|
|
13
|
+
declare function sample<T>(array: readonly T[]): T | undefined;
|
|
14
|
+
/**
|
|
15
|
+
* Returns a random character from a string.
|
|
16
|
+
*
|
|
17
|
+
* @param {string} str - The string to sample from.
|
|
18
|
+
* @returns {string | undefined} A random character from the string, or `undefined` if the string is empty.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* const str = "hello";
|
|
22
|
+
* const result = sample(str);
|
|
23
|
+
* console.log(result); // Output: 'h', 'e', 'l', 'l', or 'o' (randomly selected)
|
|
24
|
+
*/
|
|
25
|
+
declare function sample(str: string): string | undefined;
|
|
26
|
+
/**
|
|
27
|
+
* Returns a random element from an array.
|
|
28
|
+
*
|
|
29
|
+
* @template T
|
|
30
|
+
* @param {ArrayLike<T>} array - The array-like object to sample from.
|
|
31
|
+
* @returns {T | undefined} A random element from the array, or `undefined` if the array is empty.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* const arrayLike: ArrayLike<string> = { 0: 'a', 1: 'b', 2: 'c', length: 3 };
|
|
35
|
+
* const result = sample(arrayLike);
|
|
36
|
+
* console.log(result); // Output: 'a', 'b', or 'c' (randomly selected)
|
|
37
|
+
*/
|
|
38
|
+
declare function sample<T>(array: ArrayLike<T>): T | undefined;
|
|
39
|
+
/**
|
|
40
|
+
* Returns a random value from an object.
|
|
41
|
+
*
|
|
42
|
+
* @template T - The type of values in the object.
|
|
43
|
+
* @param {Record<string, T>} obj - The object to sample from.
|
|
44
|
+
* @returns {T | undefined} A random value from the object, or `undefined` if the object is empty.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* const obj = { a: 1, b: 2, c: 3 };
|
|
48
|
+
* const result = sample(obj);
|
|
49
|
+
* console.log(result); // Output: 1, 2, or 3 (randomly selected)
|
|
50
|
+
*/
|
|
51
|
+
declare function sample<T>(obj: Record<string, T>): T | undefined;
|
|
52
|
+
/**
|
|
53
|
+
* Returns a random element from an array-like object or a regular object.
|
|
54
|
+
*
|
|
55
|
+
* This function takes an array-like object (such as an array or string) or a regular object,
|
|
56
|
+
* and returns a randomly selected element or value. If the collection is empty or invalid, it returns `undefined`.
|
|
57
|
+
*
|
|
58
|
+
* @template T - The type of elements in the collection.
|
|
59
|
+
* @param {ArrayLike<T> | Record<string, T>} collection - The collection to sample from.
|
|
60
|
+
* @returns {T | string | undefined} A random element from the collection, or `undefined` if the collection is empty or invalid.
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* // Array example
|
|
64
|
+
* const array = [1, 2, 3];
|
|
65
|
+
* const result = sample(array);
|
|
66
|
+
* console.log(result); // Output: 1, 2, or 3 (randomly selected)
|
|
67
|
+
*
|
|
68
|
+
* // String example
|
|
69
|
+
* const str = 'abc';
|
|
70
|
+
* const result2 = sample(str);
|
|
71
|
+
* console.log(result2); // Output: 'a', 'b', or 'c' (randomly selected)
|
|
72
|
+
*
|
|
73
|
+
* // Object example
|
|
74
|
+
* const obj = { a: 1, b: 2, c: 3 };
|
|
75
|
+
* const result3 = sample(obj);
|
|
76
|
+
* console.log(result3); // Output: 1, 2, or 3 (randomly selected)
|
|
77
|
+
*/
|
|
78
|
+
declare function sample<T>(collection: ArrayLike<T> | Record<string, T>): T | string | undefined;
|
|
79
|
+
|
|
80
|
+
export { sample };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { sample as sample$1 } from '../../array/sample.mjs';
|
|
2
|
+
import { isArrayLike } from '../predicate/isArrayLike.mjs';
|
|
3
|
+
|
|
4
|
+
function sample(collection) {
|
|
5
|
+
if (collection == null) {
|
|
6
|
+
return undefined;
|
|
7
|
+
}
|
|
8
|
+
if (isArrayLike(collection)) {
|
|
9
|
+
return sample$1(Array.from(collection));
|
|
10
|
+
}
|
|
11
|
+
return sample$1(Object.values(collection));
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export { sample };
|
package/dist/compat/index.d.mts
CHANGED
|
@@ -16,7 +16,6 @@ export { maxBy } from '../array/maxBy.mjs';
|
|
|
16
16
|
export { minBy } from '../array/minBy.mjs';
|
|
17
17
|
export { partition } from '../array/partition.mjs';
|
|
18
18
|
export { pullAt } from '../array/pullAt.mjs';
|
|
19
|
-
export { sample } from '../array/sample.mjs';
|
|
20
19
|
export { sampleSize } from '../array/sampleSize.mjs';
|
|
21
20
|
export { shuffle } from '../array/shuffle.mjs';
|
|
22
21
|
export { takeRightWhile } from '../array/takeRightWhile.mjs';
|
|
@@ -105,6 +104,7 @@ export { indexOf } from './array/indexOf.mjs';
|
|
|
105
104
|
export { join } from './array/join.mjs';
|
|
106
105
|
export { last } from './array/last.mjs';
|
|
107
106
|
export { orderBy } from './array/orderBy.mjs';
|
|
107
|
+
export { sample } from './array/sample.mjs';
|
|
108
108
|
export { size } from './array/size.mjs';
|
|
109
109
|
export { slice } from './array/slice.mjs';
|
|
110
110
|
export { some } from './array/some.mjs';
|
package/dist/compat/index.d.ts
CHANGED
|
@@ -16,7 +16,6 @@ export { maxBy } from '../array/maxBy.js';
|
|
|
16
16
|
export { minBy } from '../array/minBy.js';
|
|
17
17
|
export { partition } from '../array/partition.js';
|
|
18
18
|
export { pullAt } from '../array/pullAt.js';
|
|
19
|
-
export { sample } from '../array/sample.js';
|
|
20
19
|
export { sampleSize } from '../array/sampleSize.js';
|
|
21
20
|
export { shuffle } from '../array/shuffle.js';
|
|
22
21
|
export { takeRightWhile } from '../array/takeRightWhile.js';
|
|
@@ -105,6 +104,7 @@ export { indexOf } from './array/indexOf.js';
|
|
|
105
104
|
export { join } from './array/join.js';
|
|
106
105
|
export { last } from './array/last.js';
|
|
107
106
|
export { orderBy } from './array/orderBy.js';
|
|
107
|
+
export { sample } from './array/sample.js';
|
|
108
108
|
export { size } from './array/size.js';
|
|
109
109
|
export { slice } from './array/slice.js';
|
|
110
110
|
export { some } from './array/some.js';
|
package/dist/compat/index.js
CHANGED
|
@@ -898,6 +898,16 @@ function orderBy(collection, criteria, orders) {
|
|
|
898
898
|
.map(item => item.original);
|
|
899
899
|
}
|
|
900
900
|
|
|
901
|
+
function sample(collection) {
|
|
902
|
+
if (collection == null) {
|
|
903
|
+
return undefined;
|
|
904
|
+
}
|
|
905
|
+
if (isArrayLike(collection)) {
|
|
906
|
+
return zipWith.sample(Array.from(collection));
|
|
907
|
+
}
|
|
908
|
+
return zipWith.sample(Object.values(collection));
|
|
909
|
+
}
|
|
910
|
+
|
|
901
911
|
function size(target) {
|
|
902
912
|
if (isWeakSet$1.isNil(target)) {
|
|
903
913
|
return 0;
|
|
@@ -2172,7 +2182,6 @@ exports.maxBy = zipWith.maxBy;
|
|
|
2172
2182
|
exports.minBy = zipWith.minBy;
|
|
2173
2183
|
exports.partition = zipWith.partition;
|
|
2174
2184
|
exports.pullAt = zipWith.pullAt;
|
|
2175
|
-
exports.sample = zipWith.sample;
|
|
2176
2185
|
exports.sampleSize = zipWith.sampleSize;
|
|
2177
2186
|
exports.shuffle = zipWith.shuffle;
|
|
2178
2187
|
exports.takeRightWhile = zipWith.takeRightWhile;
|
|
@@ -2340,6 +2349,7 @@ exports.rearg = rearg;
|
|
|
2340
2349
|
exports.repeat = repeat;
|
|
2341
2350
|
exports.rest = rest;
|
|
2342
2351
|
exports.round = round;
|
|
2352
|
+
exports.sample = sample;
|
|
2343
2353
|
exports.set = set;
|
|
2344
2354
|
exports.size = size;
|
|
2345
2355
|
exports.slice = slice;
|
package/dist/compat/index.mjs
CHANGED
|
@@ -16,7 +16,6 @@ export { maxBy } from '../array/maxBy.mjs';
|
|
|
16
16
|
export { minBy } from '../array/minBy.mjs';
|
|
17
17
|
export { partition } from '../array/partition.mjs';
|
|
18
18
|
export { pullAt } from '../array/pullAt.mjs';
|
|
19
|
-
export { sample } from '../array/sample.mjs';
|
|
20
19
|
export { sampleSize } from '../array/sampleSize.mjs';
|
|
21
20
|
export { shuffle } from '../array/shuffle.mjs';
|
|
22
21
|
export { takeRightWhile } from '../array/takeRightWhile.mjs';
|
|
@@ -107,6 +106,7 @@ export { indexOf } from './array/indexOf.mjs';
|
|
|
107
106
|
export { join } from './array/join.mjs';
|
|
108
107
|
export { last } from './array/last.mjs';
|
|
109
108
|
export { orderBy } from './array/orderBy.mjs';
|
|
109
|
+
export { sample } from './array/sample.mjs';
|
|
110
110
|
export { size } from './array/size.mjs';
|
|
111
111
|
export { slice } from './array/slice.mjs';
|
|
112
112
|
export { some } from './array/some.mjs';
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "es-toolkit",
|
|
3
3
|
"description": "A state-of-the-art, high-performance JavaScript utility library with a small bundle size and strong type annotations.",
|
|
4
|
-
"version": "1.24.0-dev.
|
|
4
|
+
"version": "1.24.0-dev.779+be5ba711",
|
|
5
5
|
"homepage": "https://es-toolkit.slash.page",
|
|
6
6
|
"bugs": "https://github.com/toss/es-toolkit/issues",
|
|
7
7
|
"repository": {
|