@ditojs/utils 2.64.0 → 2.66.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.64.0",
3
+ "version": "2.66.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": "2981f6cd6e7d28291835eacab14d2c91938d9f82"
42
+ "gitHead": "f4c1f6cf632c396e160a701d90fbcafe0cdf8478"
43
43
  }
@@ -1,14 +1,35 @@
1
- export function getCommonPrefix(str1, str2) {
2
- return str1.slice(0, getCommonOffset(str1, str2))
1
+ /**
2
+ * Computes the index of the end of the longest common prefix from an array of
3
+ * strings.
4
+ * @param {string[]} strings
5
+ * @returns {number}
6
+ */
7
+ export function getCommonOffset(...strings) {
8
+ if (strings.length === 0) return 0
9
+ let offset = 0
10
+ const string1 = strings[0]
11
+ const length1 = string1.length
12
+ for (let i = 0; i < length1; i++) {
13
+ const char = string1.charCodeAt(i)
14
+ for (let j = 1; j < strings.length; j++) {
15
+ const string2 = strings[j]
16
+ if (i >= string2.length || char !== string2.charCodeAt(i)) {
17
+ return offset
18
+ }
19
+ }
20
+ offset++
21
+ }
22
+ return offset
3
23
  }
4
24
 
5
- export function getCommonOffset(str1, str2) {
6
- const length1 = str1.length
7
- const length2 = str2.length
8
- const max = length2 < length1 ? length2 : length1
9
- let pos = 0
10
- while (pos < max && str2.charCodeAt(pos) === str1.charCodeAt(pos)) {
11
- pos++
12
- }
13
- return pos
25
+ /**
26
+ * Computes the longest common prefix from an array of strings.
27
+ *
28
+ * @param {string[]} strings
29
+ * @returns {string}
30
+ */
31
+ export function getCommonPrefix(...strings) {
32
+ return strings.length === 0
33
+ ? ''
34
+ : strings[0].slice(0, getCommonOffset(...strings))
14
35
  }
@@ -1,4 +1,4 @@
1
- import { getCommonPrefix } from './getCommonPrefix.js'
1
+ import { getCommonPrefix, getCommonOffset } from './getCommonPrefix.js'
2
2
 
3
3
  describe('getCommonPrefix()', () => {
4
4
  it('should return the longest common prefix', () => {
@@ -10,3 +10,14 @@ describe('getCommonPrefix()', () => {
10
10
  expect(getCommonPrefix('InterState', 'Intersection')).toBe('Inter')
11
11
  })
12
12
  })
13
+
14
+ describe('getCommonOffset()', () => {
15
+ it('should return the longest common prefix', () => {
16
+ expect(getCommonOffset('interstate', 'intersection')).toBe(6)
17
+ expect(getCommonOffset('intersection', 'interstate')).toBe(6)
18
+ })
19
+ it('should compare case-sensitively', () => {
20
+ expect(getCommonOffset('interstate', 'Intersection')).toBe(0)
21
+ expect(getCommonOffset('InterState', 'Intersection')).toBe(5)
22
+ })
23
+ })
package/types/index.d.ts CHANGED
@@ -293,14 +293,14 @@ export function deindent(
293
293
  ...values: Array<string>
294
294
  ): string
295
295
  /**
296
- * Returns the longest prefix string that is common to both supplied strings.
296
+ * Returns the longest prefix string that is common to the supplied strings.
297
297
  */
298
- export function getCommonPrefix(str1: string, str2: string): string
298
+ export function getCommonPrefix(...strings: Array<string>): string
299
299
  /**
300
- * Returns the offset of the longest prefix string that is common to both
300
+ * Returns the offset of the longest prefix string that is common to the
301
301
  * supplied strings.
302
302
  */
303
- export function getCommonOffset(str1: string, str2: string): number
303
+ export function getCommonOffset(...strings: Array<string>): number
304
304
  /**
305
305
  * Determines whether the supplied string is an absolute url.
306
306
  *