@ditojs/utils 2.74.0 → 2.77.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.74.0",
3
+ "version": "2.77.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": "26c5b4bd33d1bc469c8b621a214ae3c8f0927c11"
42
+ "gitHead": "d2cff77924d989aad5ab601cf08433a4e683ac6d"
43
43
  }
@@ -4,8 +4,10 @@ export function stripHtml(html) {
4
4
  return html != null
5
5
  ? html
6
6
  .toString()
7
- .replace(/<br\/?>/gi, '\n')
7
+ .replace(/<br\s*\/?>/gi, '\n')
8
+ .replace(/<\/p>/gi, '\n')
8
9
  .replace(/<[^>]+>/g, '')
10
+ .trim()
9
11
  : ''
10
12
  }
11
13
 
@@ -20,5 +20,18 @@ describe('stripHtml()', () => {
20
20
  it('should convert <br> tags to line-breaks', () => {
21
21
  expect(stripHtml('hello<br>world')).toBe('hello\nworld')
22
22
  expect(stripHtml('hello<br/>world')).toBe('hello\nworld')
23
+ expect(stripHtml('hello<br />world')).toBe('hello\nworld')
24
+ })
25
+
26
+ it('should convert <p> tags to line-breaks', () => {
27
+ expect(stripHtml('<p>First paragraph</p><p>Second paragraph</p>'))
28
+ .toBe('First paragraph\nSecond paragraph')
29
+ expect(stripHtml('<p>Only paragraph</p>'))
30
+ .toBe('Only paragraph')
31
+ })
32
+
33
+ it('should trim leading and trailing whitespace', () => {
34
+ expect(stripHtml(' <p>text</p> ')).toBe('text')
35
+ expect(stripHtml('<p> text </p>')).toBe('text')
23
36
  })
24
37
  })
@@ -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', () => {