appolo-utils
Advanced tools
Comparing version 0.0.26 to 0.0.27
@@ -48,6 +48,9 @@ "use strict"; | ||
static keyBy(arr, key) { | ||
let output = {}; | ||
if (!key) { | ||
key = (item, index) => item.toString(); | ||
} | ||
let output = {}, isFn = classes_1.Classes.isFunction(key); | ||
for (let i = 0, len = (arr || []).length; i < len; i++) { | ||
let item = arr[i]; | ||
let outputKey = classes_1.Classes.isFunction(key) ? key(item, i) : item[key]; | ||
let outputKey = isFn ? key(item, i) : item[key]; | ||
output[outputKey] = item; | ||
@@ -54,0 +57,0 @@ } |
@@ -68,6 +68,10 @@ import {Classes} from "./classes"; | ||
public static keyBy<T extends object>(arr: T[], key: string | ((item: T, index: number) => string)) { | ||
public static keyBy<T extends any>(arr: T[], key?: string | ((item: T, index: number) => string)): { [index: string]: T } { | ||
let output: { [index: string]: T } = {}; | ||
if (!key) { | ||
key = (item: T, index: number) => item.toString(); | ||
} | ||
let output: { [index: string]: T } = {}, isFn = Classes.isFunction(key); | ||
for (let i = 0, len = (arr || []).length; i < len; i++) { | ||
@@ -77,3 +81,3 @@ | ||
let outputKey = Classes.isFunction(key) ? (key as Function)(item, i) : item[key as string]; | ||
let outputKey = isFn ? (key as Function)(item, i) : item[key as string]; | ||
@@ -86,7 +90,7 @@ output[outputKey] = item; | ||
public static flat<T>(arr:any[]):T[]{ | ||
public static flat<T>(arr: any[]): T[] { | ||
return arr.reduce((acc, val) => acc.concat(val), []); | ||
} | ||
public static flatDeep<T>(arr:any[],depth:number = 1):T[]{ | ||
public static flatDeep<T>(arr: any[], depth: number = 1): T[] { | ||
return depth > 0 | ||
@@ -97,14 +101,14 @@ ? arr.reduce((acc, val) => acc.concat(Array.isArray(val) ? Arrays.flatDeep(val, depth - 1) : val), []) | ||
public static partition<T>(arr:T[],criteria:(value:T)=>boolean):[T[],T[]]{ | ||
let arr1 = [],arr2=[]; | ||
public static partition<T>(arr: T[], criteria: (value: T) => boolean): [T[], T[]] { | ||
let arr1 = [], arr2 = []; | ||
for(let i=0,len=(arr||[]).length;i<len;i++){ | ||
for (let i = 0, len = (arr || []).length; i < len; i++) { | ||
let value= arr[i]; | ||
let value = arr[i]; | ||
criteria(value) ? arr1.push(value):arr2.push(value); | ||
criteria(value) ? arr1.push(value) : arr2.push(value); | ||
} | ||
return [arr1,arr2] | ||
return [arr1, arr2] | ||
} | ||
} |
@@ -17,3 +17,3 @@ { | ||
"main": "./index.js", | ||
"version": "0.0.26", | ||
"version": "0.0.27", | ||
"license": "MIT", | ||
@@ -20,0 +20,0 @@ "repository": { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
63094
1166