@ditojs/utils 2.75.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.75.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": "cfc4ee21dc3f5808b5a78a3b1fab83500c05907a"
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
  })
@@ -6,11 +6,13 @@ export function decamelize(str, sep = ' ') {
6
6
  // switch to better parsing that matches non-ASCII uppercase letters:
7
7
  // /([\p{Ll}\d])(\p{Lu})/gu
8
8
  // See: https://caniuse.com/?search=Unicode%20property%20escape
9
- /([a-z\d])([A-Z])|(\S)(?:\s+)(\S)/g,
10
- (all, lower, upper, beforeSpace, afterSpace) =>
11
- upper
9
+ /(^\s+)|(\s+$)|([a-z\d])([A-Z])|(?<=\S)\s+(?=\S)/g,
10
+ (all, leading, trailing, lower, upper) =>
11
+ lower
12
12
  ? `${lower}${sep}${upper}`
13
- : `${beforeSpace}${sep}${afterSpace}`
13
+ : leading || trailing
14
+ ? ''
15
+ : sep
14
16
  )
15
17
  .toLowerCase()
16
18
  : ''
@@ -32,6 +32,26 @@ describe('hyphenate()', () => {
32
32
  })
33
33
  }
34
34
  )
35
+
36
+ it('should hyphenate single space between words', () => {
37
+ expect(hyphenate('Foo Bar')).toBe('foo-bar')
38
+ })
39
+
40
+ it('should hyphenate multiple spaces', () => {
41
+ expect(hyphenate('Foo Bar')).toBe('foo-bar')
42
+ })
43
+
44
+ it('should trim leading whitespace', () => {
45
+ expect(hyphenate(' Foo Bar')).toBe('foo-bar')
46
+ })
47
+
48
+ it('should trim trailing whitespace', () => {
49
+ expect(hyphenate('Foo Bar ')).toBe('foo-bar')
50
+ })
51
+
52
+ it('should hyphenate special characters correctly', () => {
53
+ expect(hyphenate('Foo & Bar')).toBe('foo-&-bar')
54
+ })
35
55
  })
36
56
 
37
57
  describe('underscore()', () => {
@@ -44,4 +64,20 @@ describe('underscore()', () => {
44
64
  })
45
65
  }
46
66
  )
67
+
68
+ it('should underscore multiple spaces', () => {
69
+ expect(underscore('Foo Bar')).toBe('foo_bar')
70
+ })
71
+
72
+ it('should trim leading whitespace', () => {
73
+ expect(underscore(' Foo Bar')).toBe('foo_bar')
74
+ })
75
+
76
+ it('should trim trailing whitespace', () => {
77
+ expect(underscore('Foo Bar ')).toBe('foo_bar')
78
+ })
79
+
80
+ it('should underscore special characters correctly', () => {
81
+ expect(underscore('Foo & Bar')).toBe('foo_&_bar')
82
+ })
47
83
  })
@@ -3,14 +3,15 @@ export function labelize(str) {
3
3
  // expand them to title cased labels.
4
4
  return str
5
5
  ? str.replace(
6
- /([-_ ]|^)(\w)|([a-z])(?=[A-Z0-9])|(\d)([a-zA-Z])/g,
7
- function (all, hyphen, hyphenated, camel, decimal, decimalNext) {
8
- return hyphenated
9
- ? `${hyphen ? ' ' : ''}${hyphenated.toUpperCase()}`
10
- : camel
11
- ? `${camel} `
12
- : `${decimal} ${decimalNext.toUpperCase()}`
13
- }
6
+ /[-_](?=\W)|([-_ ]|^)(\w)|([a-z])(?=[A-Z0-9])|(\d)([a-zA-Z])/g,
7
+ (all, hyphen, hyphenated, camel, decimal, decimalNext) =>
8
+ all === '-' || all === '_'
9
+ ? ' '
10
+ : hyphenated
11
+ ? `${hyphen ? ' ' : ''}${hyphenated.toUpperCase()}`
12
+ : camel
13
+ ? `${camel} `
14
+ : `${decimal} ${decimalNext.toUpperCase()}`
14
15
  )
15
16
  : ''
16
17
  }
@@ -3,6 +3,8 @@ import { labelize } from './labelize.js'
3
3
  describe('labelize()', () => {
4
4
  it('should labelize hyphenated text', () => {
5
5
  expect(labelize('some-hyphenated-text')).toBe('Some Hyphenated Text')
6
+ expect(labelize('one-&-two', true)).toBe('One & Two')
7
+ expect(labelize('one-2-three', true)).toBe('One 2 Three')
6
8
  })
7
9
 
8
10
  it('should labelize underscored text', () => {