|
| 1 | +/* eslint-disable consistent-return */ |
| 2 | +/* eslint-disable @typescript-eslint/no-unused-vars */ |
| 3 | +/* eslint-disable @typescript-eslint/ban-types */ |
| 4 | +/* eslint-disable no-restricted-syntax */ |
| 5 | +/* eslint-disable no-plusplus */ |
| 6 | +/* eslint-disable no-nested-ternary */ |
| 7 | +import { isArr, isObj, isStr } from './types'; |
| 8 | + |
| 9 | +type EachArrayIterator<T> = (currentValue: T, key: number) => void | boolean; |
| 10 | +type EachStringIterator = (currentValue: string, key: number) => void | boolean; |
| 11 | +type EachObjectIterator<T = any> = (currentValue: T, key: string) => void | boolean; |
| 12 | +type MapArrayIterator<TItem, TResult> = (currentValue: TItem, key: number) => TResult; |
| 13 | +type MapStringIterator<TResult> = (currentValue: string, key: number) => TResult; |
| 14 | +type MapObjectIterator<TItem, TResult> = (currentValue: TItem, key: string) => TResult; |
| 15 | +type MemoArrayIterator<T, U> = (previousValue: U, currentValue: T, key: number) => U; |
| 16 | +type MemoStringIterator<T> = (previousValue: T, currentValue: string, key: number) => T; |
| 17 | +type MemoObjectIterator<TValue, TResult> = ( |
| 18 | + previousValue: TResult, |
| 19 | + currentValue: TValue, |
| 20 | + key: string, |
| 21 | +) => TResult; |
| 22 | + |
| 23 | +export const toArr = (val: any): any[] => (isArr(val) ? val : val ? [val] : []); |
| 24 | +export function each(val: string, iterator: EachStringIterator, revert?: boolean): void; |
| 25 | +export function each<T>(val: T[], iterator: EachArrayIterator<T>, revert?: boolean): void; |
| 26 | +export function each<T extends {}, TValue = T[keyof T]>( |
| 27 | + val: T, |
| 28 | + iterator: EachObjectIterator<TValue>, |
| 29 | + revert?: boolean, |
| 30 | +): void; |
| 31 | +export function each(val: any, iterator: any, revert?: boolean): void { |
| 32 | + if (isArr(val) || isStr(val)) { |
| 33 | + if (revert) { |
| 34 | + for (let i: number = val.length - 1; i >= 0; i--) { |
| 35 | + if (iterator(val[i], i) === false) { |
| 36 | + return; |
| 37 | + } |
| 38 | + } |
| 39 | + } else { |
| 40 | + for (let i = 0; i < val.length; i++) { |
| 41 | + if (iterator(val[i], i) === false) { |
| 42 | + return; |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + } else if (isObj(val)) { |
| 47 | + let key: string; |
| 48 | + for (key in val) { |
| 49 | + if (Object.hasOwnProperty.call(val, key)) { |
| 50 | + if (iterator(val[key], key) === false) { |
| 51 | + return; |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +export function map<T>(val: string, iterator: MapStringIterator<T>, revert?: boolean): any; |
| 59 | +export function map<TItem, TResult>( |
| 60 | + val: TItem[], |
| 61 | + iterator: MapArrayIterator<TItem, TResult>, |
| 62 | + revert?: boolean, |
| 63 | +): any; |
| 64 | +export function map<T extends {}, TResult>( |
| 65 | + val: T, |
| 66 | + iterator: MapObjectIterator<T[keyof T], TResult>, |
| 67 | + revert?: boolean, |
| 68 | +): any; |
| 69 | +export function map(val: any, iterator: any, revert?: boolean): any { |
| 70 | + const res = isArr(val) || isStr(val) ? [] : {}; |
| 71 | + each( |
| 72 | + val, |
| 73 | + (item, key) => { |
| 74 | + const value = iterator(item, key); |
| 75 | + if (isArr(res)) { |
| 76 | + (res as any).push(value); |
| 77 | + } else { |
| 78 | + res[key] = value; |
| 79 | + } |
| 80 | + }, |
| 81 | + revert, |
| 82 | + ); |
| 83 | + return res; |
| 84 | +} |
| 85 | + |
| 86 | +export function reduce<T, U>( |
| 87 | + val: T[], |
| 88 | + iterator: MemoArrayIterator<T, U>, |
| 89 | + accumulator?: U, |
| 90 | + revert?: boolean, |
| 91 | +): U; |
| 92 | +export function reduce<T>( |
| 93 | + val: string, |
| 94 | + iterator: MemoStringIterator<T>, |
| 95 | + accumulator?: T, |
| 96 | + revert?: boolean, |
| 97 | +): T; |
| 98 | +export function reduce<T extends {}, TValue = T[keyof T], TResult = any>( |
| 99 | + val: T, |
| 100 | + iterator: MemoObjectIterator<TValue, TResult>, |
| 101 | + accumulator?: TResult, |
| 102 | + revert?: boolean, |
| 103 | +): TResult; |
| 104 | +export function reduce(val: any, iterator: any, accumulator?: any, revert?: boolean): any { |
| 105 | + let result = accumulator; |
| 106 | + each( |
| 107 | + val, |
| 108 | + (item, key) => { |
| 109 | + result = iterator(result, item, key); |
| 110 | + }, |
| 111 | + revert, |
| 112 | + ); |
| 113 | + return result; |
| 114 | +} |
| 115 | + |
| 116 | +export function every<T extends string>( |
| 117 | + val: T, |
| 118 | + iterator: EachStringIterator, |
| 119 | + revert?: boolean, |
| 120 | +): boolean; |
| 121 | +export function every<T>(val: T[], iterator: EachArrayIterator<T>, revert?: boolean): boolean; |
| 122 | +export function every<T extends {}, TValue = T[keyof T]>( |
| 123 | + val: T, |
| 124 | + iterator: EachObjectIterator, |
| 125 | + revert?: boolean, |
| 126 | +): boolean; |
| 127 | +export function every(val: any, iterator: any, revert?: boolean): boolean { |
| 128 | + let res = true; |
| 129 | + each( |
| 130 | + val, |
| 131 | + (item, key) => { |
| 132 | + if (!iterator(item, key)) { |
| 133 | + res = false; |
| 134 | + return false; |
| 135 | + } |
| 136 | + }, |
| 137 | + revert, |
| 138 | + ); |
| 139 | + return res; |
| 140 | +} |
| 141 | + |
| 142 | +export function some<T extends string>( |
| 143 | + val: T, |
| 144 | + iterator: EachStringIterator, |
| 145 | + revert?: boolean, |
| 146 | +): boolean; |
| 147 | +export function some<T>(val: T[], iterator: EachArrayIterator<T>, revert?: boolean): boolean; |
| 148 | +export function some<T extends {}, TValue = T[keyof T]>( |
| 149 | + val: T, |
| 150 | + iterator: EachObjectIterator, |
| 151 | + revert?: boolean, |
| 152 | +): boolean; |
| 153 | +export function some(val: any, iterator: any, revert?: boolean): boolean { |
| 154 | + let res = false; |
| 155 | + each( |
| 156 | + val, |
| 157 | + (item, key) => { |
| 158 | + if (iterator(item, key)) { |
| 159 | + res = true; |
| 160 | + return false; |
| 161 | + } |
| 162 | + }, |
| 163 | + revert, |
| 164 | + ); |
| 165 | + return res; |
| 166 | +} |
| 167 | + |
| 168 | +export function findIndex<T extends string>( |
| 169 | + val: T, |
| 170 | + iterator: EachStringIterator, |
| 171 | + revert?: boolean, |
| 172 | +): number; |
| 173 | +export function findIndex<T>(val: T[], iterator: EachArrayIterator<T>, revert?: boolean): number; |
| 174 | +export function findIndex<T extends {}, TValue = T[keyof T]>( |
| 175 | + val: T, |
| 176 | + iterator: EachObjectIterator, |
| 177 | + revert?: boolean, |
| 178 | +): keyof T; |
| 179 | +export function findIndex(val: any, iterator: any, revert?: boolean): string | number { |
| 180 | + let res: number | string = -1; |
| 181 | + each( |
| 182 | + val, |
| 183 | + (item, key) => { |
| 184 | + if (iterator(item, key)) { |
| 185 | + res = key; |
| 186 | + return false; |
| 187 | + } |
| 188 | + }, |
| 189 | + revert, |
| 190 | + ); |
| 191 | + return res; |
| 192 | +} |
| 193 | + |
| 194 | +export function find<T extends string>(val: T, iterator: EachStringIterator, revert?: boolean): any; |
| 195 | +export function find<T>(val: T[], iterator: EachArrayIterator<T>, revert?: boolean): T; |
| 196 | +export function find<T extends {}, TValue = T[keyof T]>( |
| 197 | + val: T, |
| 198 | + iterator: EachObjectIterator, |
| 199 | + revert?: boolean, |
| 200 | +): T[keyof T]; |
| 201 | +export function find(val: any, iterator: any, revert?: boolean): any { |
| 202 | + let res: any; |
| 203 | + each( |
| 204 | + val, |
| 205 | + (item, key) => { |
| 206 | + if (iterator(item, key)) { |
| 207 | + res = item; |
| 208 | + return false; |
| 209 | + } |
| 210 | + }, |
| 211 | + revert, |
| 212 | + ); |
| 213 | + return res; |
| 214 | +} |
| 215 | + |
| 216 | +export function includes<T extends string>( |
| 217 | + val: T, |
| 218 | + searchElement: string, |
| 219 | + revert?: boolean, |
| 220 | +): boolean; |
| 221 | +export function includes<T>(val: T[], searchElement: T, revert?: boolean): boolean; |
| 222 | +export function includes(val: any, searchElement: any, revert?: boolean) { |
| 223 | + if (isStr(val)) return val.includes(searchElement); |
| 224 | + return some(val, (item) => item === searchElement, revert); |
| 225 | +} |
| 226 | + |
| 227 | +export function includesWith<T extends string>(val: T, search: (item: string) => boolean): boolean; |
| 228 | +export function includesWith<T>(val: T[], search: (item: T) => boolean): boolean; |
| 229 | +export function includesWith(val: any, search: any) { |
| 230 | + if (isArr(val)) { |
| 231 | + return val.some((item) => search(item)); |
| 232 | + } |
| 233 | + return false; |
| 234 | +} |
0 commit comments