@ditojs/utils 2.77.0 → 2.78.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ditojs/utils",
3
- "version": "2.77.0",
3
+ "version": "2.78.0",
4
4
  "type": "module",
5
5
  "description": "Dito.js Utility Functions – Dito.js is a declarative and modern web framework, based on Objection.js, Koa.js and Vue.js",
6
6
  "repository": "https://github.com/ditojs/dito/tree/master/packages/utils",
@@ -39,5 +39,5 @@
39
39
  "devDependencies": {
40
40
  "typescript": "^5.9.3"
41
41
  },
42
- "gitHead": "d2cff77924d989aad5ab601cf08433a4e683ac6d"
42
+ "gitHead": "ede4c836e6b2bb5dfcc80e1614a3d85eb80b0efa"
43
43
  }
@@ -0,0 +1,9 @@
1
+ import { isString } from '../base/index.js'
2
+
3
+ export function asCallback(callback) {
4
+ if (isString(callback)) {
5
+ const key = callback
6
+ return value => value[key]
7
+ }
8
+ return callback
9
+ }
@@ -1,9 +1,11 @@
1
1
  import { isArray } from '../base/index.js'
2
+ import { asCallback } from './asCallback.js'
2
3
 
3
4
  export function groupBy(collection, callback) {
4
5
  const array = isArray(collection)
5
6
  ? collection
6
7
  : Object.values(collection)
8
+ callback = asCallback(callback)
7
9
  return array.reduce((groups, item) => {
8
10
  const key = callback(item)
9
11
  const group = groups[key] || (groups[key] = [])
@@ -31,4 +31,40 @@ describe('groupBy()', () => {
31
31
  const actual = groupBy({ a: 6.1, b: 4.2, c: 6.3 }, Math.floor)
32
32
  expect(actual).toStrictEqual({ 4: [4.2], 6: [6.1, 6.3] })
33
33
  })
34
+
35
+ it('should support string property accessor', () => {
36
+ const array = [
37
+ { category: 'fruit', name: 'apple' },
38
+ { category: 'vegetable', name: 'carrot' },
39
+ { category: 'fruit', name: 'banana' }
40
+ ]
41
+ const actual = groupBy(array, 'category')
42
+ expect(actual).toStrictEqual({
43
+ fruit: [
44
+ { category: 'fruit', name: 'apple' },
45
+ { category: 'fruit', name: 'banana' }
46
+ ],
47
+ vegetable: [
48
+ { category: 'vegetable', name: 'carrot' }
49
+ ]
50
+ })
51
+ })
52
+
53
+ it('should group by string property with numbers', () => {
54
+ const array = [
55
+ { id: 1, value: 'a' },
56
+ { id: 2, value: 'b' },
57
+ { id: 1, value: 'c' }
58
+ ]
59
+ const actual = groupBy(array, 'id')
60
+ expect(actual).toStrictEqual({
61
+ 1: [
62
+ { id: 1, value: 'a' },
63
+ { id: 1, value: 'c' }
64
+ ],
65
+ 2: [
66
+ { id: 2, value: 'b' }
67
+ ]
68
+ })
69
+ })
34
70
  })
@@ -1,3 +1,4 @@
1
+ export * from './asCallback.js'
1
2
  export * from './clone.js'
2
3
  export * from './equals.js'
3
4
  export * from './groupBy.js'
@@ -1,4 +1,7 @@
1
+ import { asCallback } from './asCallback.js'
2
+
1
3
  export function mapValues(object, callback) {
4
+ callback = asCallback(callback)
2
5
  return Object.keys(object).reduce((mapped, key) => {
3
6
  mapped[key] = callback(object[key], key, object)
4
7
  return mapped
@@ -13,4 +13,26 @@ describe('mapValues()', () => {
13
13
  const actual = mapValues(array, String)
14
14
  expect(actual).toStrictEqual({ 0: '1', 1: '2' })
15
15
  })
16
+
17
+ it('should support string property accessor', () => {
18
+ const users = {
19
+ user1: { id: 1, name: 'Alice', email: 'alice@example.com' },
20
+ user2: { id: 2, name: 'Bob', email: 'bob@example.com' }
21
+ }
22
+ const actual = mapValues(users, 'email')
23
+ expect(actual).toStrictEqual({
24
+ user1: 'alice@example.com',
25
+ user2: 'bob@example.com'
26
+ })
27
+ })
28
+
29
+ it('should extract nested properties with string accessor', () => {
30
+ const data = {
31
+ a: { value: 10 },
32
+ b: { value: 20 },
33
+ c: { value: 30 }
34
+ }
35
+ const actual = mapValues(data, 'value')
36
+ expect(actual).toStrictEqual({ a: 10, b: 20, c: 30 })
37
+ })
16
38
  })
@@ -1,4 +1,7 @@
1
+ import { asCallback } from './asCallback.js'
2
+
1
3
  export function pickBy(object, callback) {
4
+ callback = asCallback(callback)
2
5
  return Object.entries(object).reduce((result, [key, value]) => {
3
6
  if (callback(value, key, object)) {
4
7
  result[key] = value
@@ -6,4 +6,43 @@ describe('pickBy()', () => {
6
6
  const actual = pickBy(object, n => n === 1 || n === 3)
7
7
  expect(actual).toStrictEqual({ a: 1, c: 3 })
8
8
  })
9
+
10
+ it('should support string property accessor for truthy values', () => {
11
+ const users = {
12
+ user1: { name: 'Alice', active: true },
13
+ user2: { name: 'Bob', active: false },
14
+ user3: { name: 'Charlie', active: true }
15
+ }
16
+ const actual = pickBy(users, 'active')
17
+ expect(actual).toStrictEqual({
18
+ user1: { name: 'Alice', active: true },
19
+ user3: { name: 'Charlie', active: true }
20
+ })
21
+ })
22
+
23
+ it('should filter by numeric property with string accessor', () => {
24
+ const items = {
25
+ a: { score: 0 },
26
+ b: { score: 5 },
27
+ c: { score: 10 }
28
+ }
29
+ const actual = pickBy(items, 'score')
30
+ expect(actual).toStrictEqual({
31
+ b: { score: 5 },
32
+ c: { score: 10 }
33
+ })
34
+ })
35
+
36
+ it('should filter by string property with string accessor', () => {
37
+ const data = {
38
+ a: { label: '' },
39
+ b: { label: 'test' },
40
+ c: { label: 'value' }
41
+ }
42
+ const actual = pickBy(data, 'label')
43
+ expect(actual).toStrictEqual({
44
+ b: { label: 'test' },
45
+ c: { label: 'value' }
46
+ })
47
+ })
9
48
  })