@ditojs/utils
Advanced tools
| import { isString } from '../base/index.js' | ||
| export function asCallback(callback) { | ||
| if (isString(callback)) { | ||
| const key = callback | ||
| return value => value[key] | ||
| } | ||
| return callback | ||
| } |
+2
-2
| { | ||
| "name": "@ditojs/utils", | ||
| "version": "2.77.0", | ||
| "version": "2.78.0", | ||
| "type": "module", | ||
@@ -42,3 +42,3 @@ "description": "Dito.js Utility Functions – Dito.js is a declarative and modern web framework, based on Objection.js, Koa.js and Vue.js", | ||
| }, | ||
| "gitHead": "d2cff77924d989aad5ab601cf08433a4e683ac6d" | ||
| "gitHead": "ede4c836e6b2bb5dfcc80e1614a3d85eb80b0efa" | ||
| } |
| import { isArray } from '../base/index.js' | ||
| import { asCallback } from './asCallback.js' | ||
@@ -7,2 +8,3 @@ export function groupBy(collection, callback) { | ||
| : Object.values(collection) | ||
| callback = asCallback(callback) | ||
| return array.reduce((groups, item) => { | ||
@@ -9,0 +11,0 @@ const key = callback(item) |
@@ -34,2 +34,38 @@ import { groupBy } from './groupBy.js' | ||
| }) | ||
| it('should support string property accessor', () => { | ||
| const array = [ | ||
| { category: 'fruit', name: 'apple' }, | ||
| { category: 'vegetable', name: 'carrot' }, | ||
| { category: 'fruit', name: 'banana' } | ||
| ] | ||
| const actual = groupBy(array, 'category') | ||
| expect(actual).toStrictEqual({ | ||
| fruit: [ | ||
| { category: 'fruit', name: 'apple' }, | ||
| { category: 'fruit', name: 'banana' } | ||
| ], | ||
| vegetable: [ | ||
| { category: 'vegetable', name: 'carrot' } | ||
| ] | ||
| }) | ||
| }) | ||
| it('should group by string property with numbers', () => { | ||
| const array = [ | ||
| { id: 1, value: 'a' }, | ||
| { id: 2, value: 'b' }, | ||
| { id: 1, value: 'c' } | ||
| ] | ||
| const actual = groupBy(array, 'id') | ||
| expect(actual).toStrictEqual({ | ||
| 1: [ | ||
| { id: 1, value: 'a' }, | ||
| { id: 1, value: 'c' } | ||
| ], | ||
| 2: [ | ||
| { id: 2, value: 'b' } | ||
| ] | ||
| }) | ||
| }) | ||
| }) |
@@ -0,1 +1,2 @@ | ||
| export * from './asCallback.js' | ||
| export * from './clone.js' | ||
@@ -2,0 +3,0 @@ export * from './equals.js' |
@@ -0,2 +1,5 @@ | ||
| import { asCallback } from './asCallback.js' | ||
| export function mapValues(object, callback) { | ||
| callback = asCallback(callback) | ||
| return Object.keys(object).reduce((mapped, key) => { | ||
@@ -3,0 +6,0 @@ mapped[key] = callback(object[key], key, object) |
@@ -16,2 +16,24 @@ import { mapValues } from './mapValues.js' | ||
| }) | ||
| it('should support string property accessor', () => { | ||
| const users = { | ||
| user1: { id: 1, name: 'Alice', email: 'alice@example.com' }, | ||
| user2: { id: 2, name: 'Bob', email: 'bob@example.com' } | ||
| } | ||
| const actual = mapValues(users, 'email') | ||
| expect(actual).toStrictEqual({ | ||
| user1: 'alice@example.com', | ||
| user2: 'bob@example.com' | ||
| }) | ||
| }) | ||
| it('should extract nested properties with string accessor', () => { | ||
| const data = { | ||
| a: { value: 10 }, | ||
| b: { value: 20 }, | ||
| c: { value: 30 } | ||
| } | ||
| const actual = mapValues(data, 'value') | ||
| expect(actual).toStrictEqual({ a: 10, b: 20, c: 30 }) | ||
| }) | ||
| }) |
@@ -0,2 +1,5 @@ | ||
| import { asCallback } from './asCallback.js' | ||
| export function pickBy(object, callback) { | ||
| callback = asCallback(callback) | ||
| return Object.entries(object).reduce((result, [key, value]) => { | ||
@@ -3,0 +6,0 @@ if (callback(value, key, object)) { |
@@ -9,2 +9,41 @@ import { pickBy } from './pickBy.js' | ||
| }) | ||
| it('should support string property accessor for truthy values', () => { | ||
| const users = { | ||
| user1: { name: 'Alice', active: true }, | ||
| user2: { name: 'Bob', active: false }, | ||
| user3: { name: 'Charlie', active: true } | ||
| } | ||
| const actual = pickBy(users, 'active') | ||
| expect(actual).toStrictEqual({ | ||
| user1: { name: 'Alice', active: true }, | ||
| user3: { name: 'Charlie', active: true } | ||
| }) | ||
| }) | ||
| it('should filter by numeric property with string accessor', () => { | ||
| const items = { | ||
| a: { score: 0 }, | ||
| b: { score: 5 }, | ||
| c: { score: 10 } | ||
| } | ||
| const actual = pickBy(items, 'score') | ||
| expect(actual).toStrictEqual({ | ||
| b: { score: 5 }, | ||
| c: { score: 10 } | ||
| }) | ||
| }) | ||
| it('should filter by string property with string accessor', () => { | ||
| const data = { | ||
| a: { label: '' }, | ||
| b: { label: 'test' }, | ||
| c: { label: 'value' } | ||
| } | ||
| const actual = pickBy(data, 'label') | ||
| expect(actual).toStrictEqual({ | ||
| b: { label: 'test' }, | ||
| c: { label: 'value' } | ||
| }) | ||
| }) | ||
| }) |
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
164882
1.88%100
1.01%6152
1.74%