appolo-utils
Advanced tools
Comparing version 0.0.1 to 0.0.2
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const benchmark = require("benchmark"); | ||
const strings_1 = require("../lib/strings"); | ||
const _ = require("lodash"); | ||
let suite = new benchmark.Suite(); | ||
let test = { | ||
test: 1, | ||
test3: 3, | ||
test4: "afsdjdshgkjhsdkjghdkshfgkdsjkfhjdkshfjkdsjkf", | ||
test2: 2 | ||
let a = [{ "a": 1, b: 2, c: 3 }, { "a": 2, b: 2, c: 3 }, { "a": 1, b: 2, c: 3 }, { "a": 3, b: 2, c: 3 }, { | ||
"a": 3, | ||
b: 2, | ||
c: 3 | ||
}, { "a": 5, b: 2, c: 3 }, { "a": 2, b: 2, c: 3 }]; | ||
const groupBy = function (xs, key) { | ||
return xs.reduce(function (rv, x) { | ||
(rv[x[key]] = rv[x[key]] || []).push(x); | ||
return rv; | ||
}, {}); | ||
}; | ||
const groupBy2 = (items, key) => items.reduce((result, item) => (Object.assign({}, result, { [item[key]]: [ | ||
...(result[item[key]] || []), | ||
item, | ||
] })), {}); | ||
const groupBy3 = function (items, key) { | ||
let output = {}; | ||
for (let i = 0, len = items.length; i < len; i++) { | ||
let item = items[i], value = (typeof key === "function") ? key(item) : item[key], arr = output[value] || (output[value] = []); | ||
arr.push(item); | ||
} | ||
return output; | ||
}; | ||
suite.add('set', function () { | ||
strings_1.Strings.stringifyObjectValues(test); | ||
_.groupBy(a, "a"); | ||
}); | ||
suite.add('set2', function () { | ||
JSON.stringify(test); | ||
groupBy(a, "a"); | ||
}); | ||
suite.add('set3', function () { | ||
Object.values(test).join(""); | ||
groupBy2(a, "a"); | ||
}); | ||
suite.add('set4', function () { | ||
groupBy3(a, "a"); | ||
}); | ||
suite | ||
@@ -22,0 +42,0 @@ .on('cycle', (event) => { |
'use strict'; | ||
import benchmark = require('benchmark'); | ||
import {Strings} from '../lib/strings'; | ||
import * as _ from 'lodash'; | ||
@@ -9,24 +9,59 @@ let suite = new benchmark.Suite(); | ||
let test = { | ||
test: 1, | ||
test3: 3, | ||
test4: "afsdjdshgkjhsdkjghdkshfgkdsjkfhjdkshfjkdsjkf", | ||
test2: 2 | ||
let a = [{"a": 1, b: 2, c: 3}, {"a": 2, b: 2, c: 3}, {"a": 1, b: 2, c: 3}, {"a": 3, b: 2, c: 3}, { | ||
"a": 3, | ||
b: 2, | ||
c: 3 | ||
}, {"a": 5, b: 2, c: 3}, {"a": 2, b: 2, c: 3}] | ||
const groupBy = function (xs, key) { | ||
return xs.reduce(function (rv, x) { | ||
(rv[x[key]] = rv[x[key]] || []).push(x); | ||
return rv; | ||
}, {}); | ||
}; | ||
const groupBy2 = (items, key) => items.reduce( | ||
(result, item) => ({ | ||
...result, | ||
[item[key]]: [ | ||
...(result[item[key]] || []), | ||
item, | ||
], | ||
}), | ||
{}, | ||
); | ||
const groupBy3 = function (items, key) { | ||
let output = {}; | ||
for (let i = 0, len = items.length; i < len; i++) { | ||
let item = items[i], value = (typeof key === "function") ? key(item) : item[key], arr = output[value] || (output[value] = []); | ||
arr.push(item); | ||
} | ||
return output; | ||
} | ||
suite.add('set', function () { | ||
Strings.stringifyObjectValues(test) | ||
_.groupBy(a, "a") | ||
}); | ||
suite.add('set2', function () { | ||
JSON.stringify(test) | ||
groupBy(a, "a") | ||
}); | ||
suite.add('set3', function () { | ||
Object.values(test).join("") | ||
groupBy2(a, "a") | ||
}); | ||
suite.add('set4', function () { | ||
groupBy3(a, "a"); | ||
}); | ||
suite | ||
@@ -33,0 +68,0 @@ .on('cycle', (event) => { |
@@ -33,4 +33,12 @@ "use strict"; | ||
} | ||
static groupBy(arr, key) { | ||
let output = {}; | ||
for (let i = 0, len = arr.length; i < len; i++) { | ||
let item = arr[i], value = (typeof key === "function") ? key(item) : item[key], dto = output[value] || (output[value] = []); | ||
dto.push(item); | ||
} | ||
return output; | ||
} | ||
} | ||
exports.Arrays = Arrays; | ||
//# sourceMappingURL=arrays.js.map |
@@ -41,2 +41,18 @@ export class Arrays { | ||
} | ||
public static groupBy<T>(arr: T[], key: string | number | ((item: T) => string | number)) { | ||
let output = {}; | ||
for (let i = 0, len = arr.length; i < len; i++) { | ||
let item = arr[i], | ||
value = (typeof key === "function") ? key(item) : item[key], | ||
dto = output[value] || (output[value] = []); | ||
dto.push(item); | ||
} | ||
return output; | ||
} | ||
} |
@@ -12,3 +12,3 @@ import {Arrays} from "./arrays"; | ||
public static functionArgsNames(func: (...args: any[]) => any) { | ||
public static functionArgsNames(func: ((...args: any[]) => any) | (new(...args: any[])=> any)) { | ||
@@ -15,0 +15,0 @@ const STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg; |
@@ -17,3 +17,3 @@ { | ||
"main": "./index.js", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"license": "MIT", | ||
@@ -37,2 +37,3 @@ "repository": { | ||
"chai": "^4.2.0", | ||
"lodash": "^4.17.11", | ||
"mocha": "^5.2.0", | ||
@@ -39,0 +40,0 @@ "tslib": "^1.9.3", |
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
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
34522
658
13