source: node_modules/es-toolkit/dist/compat/array/some.mjs@ a762898

Last change on this file since a762898 was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago

Added visualizations

  • Property mode set to 100644
File size: 2.7 KB
Line 
1import { identity } from '../../function/identity.mjs';
2import { property } from '../object/property.mjs';
3import { matches } from '../predicate/matches.mjs';
4import { matchesProperty } from '../predicate/matchesProperty.mjs';
5
6function some(source, predicate, guard) {
7 if (!source) {
8 return false;
9 }
10 if (guard != null) {
11 predicate = undefined;
12 }
13 if (predicate == null) {
14 predicate = identity;
15 }
16 const values = Array.isArray(source) ? source : Object.values(source);
17 switch (typeof predicate) {
18 case 'function': {
19 if (!Array.isArray(source)) {
20 const keys = Object.keys(source);
21 for (let i = 0; i < keys.length; i++) {
22 const key = keys[i];
23 const value = source[key];
24 if (predicate(value, key, source)) {
25 return true;
26 }
27 }
28 return false;
29 }
30 for (let i = 0; i < source.length; i++) {
31 if (predicate(source[i], i, source)) {
32 return true;
33 }
34 }
35 return false;
36 }
37 case 'object': {
38 if (Array.isArray(predicate) && predicate.length === 2) {
39 const key = predicate[0];
40 const value = predicate[1];
41 const matchFunc = matchesProperty(key, value);
42 if (Array.isArray(source)) {
43 for (let i = 0; i < source.length; i++) {
44 if (matchFunc(source[i])) {
45 return true;
46 }
47 }
48 return false;
49 }
50 return values.some(matchFunc);
51 }
52 else {
53 const matchFunc = matches(predicate);
54 if (Array.isArray(source)) {
55 for (let i = 0; i < source.length; i++) {
56 if (matchFunc(source[i])) {
57 return true;
58 }
59 }
60 return false;
61 }
62 return values.some(matchFunc);
63 }
64 }
65 case 'number':
66 case 'symbol':
67 case 'string': {
68 const propFunc = property(predicate);
69 if (Array.isArray(source)) {
70 for (let i = 0; i < source.length; i++) {
71 if (propFunc(source[i])) {
72 return true;
73 }
74 }
75 return false;
76 }
77 return values.some(propFunc);
78 }
79 }
80}
81
82export { some };
Note: See TracBrowser for help on using the repository browser.