@ditojs/utils 1.22.0 → 1.23.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 +8 -7
- package/src/string/deindent.js +22 -11
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ditojs/utils",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.23.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",
|
|
@@ -14,9 +14,6 @@
|
|
|
14
14
|
"src/",
|
|
15
15
|
"types/"
|
|
16
16
|
],
|
|
17
|
-
"scripts": {
|
|
18
|
-
"types": "tsc --noEmit types/index.d.ts"
|
|
19
|
-
},
|
|
20
17
|
"engines": {
|
|
21
18
|
"node": ">= 18.0.0",
|
|
22
19
|
"yarn": ">= 1.0.0"
|
|
@@ -32,8 +29,12 @@
|
|
|
32
29
|
"punycode": "^2.3.0"
|
|
33
30
|
},
|
|
34
31
|
"devDependencies": {
|
|
35
|
-
"typescript": "^4.9.
|
|
32
|
+
"typescript": "^4.9.5"
|
|
36
33
|
},
|
|
37
34
|
"types": "types",
|
|
38
|
-
"gitHead": "
|
|
39
|
-
|
|
35
|
+
"gitHead": "3258fd030226062efa6837bfc5e5a761e80cead5",
|
|
36
|
+
"scripts": {
|
|
37
|
+
"types": "tsc --noEmit types/index.d.ts"
|
|
38
|
+
},
|
|
39
|
+
"readme": "# Dito.js Utility Functions\n\nDito.js is a declarative and modern web framework with a focus on API driven\ndevelopment, based on Koa.js, Objection.js and Vue.js\n\nReleased in 2018 under the MIT license, with support by https://lineto.com/\n"
|
|
40
|
+
}
|
package/src/string/deindent.js
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
|
-
import os from 'os'
|
|
2
1
|
import { asArray } from '../base/index.js'
|
|
3
2
|
|
|
4
3
|
// ES6 string tag that strips indentation from multi-line strings
|
|
5
4
|
// Based on, and further improved from https://github.com/dmnd/dedent
|
|
6
5
|
export function deindent(strings, ...values) {
|
|
6
|
+
// Small helper to determine the actual line-break used in the strings, based
|
|
7
|
+
// on the line-breaks encountered.
|
|
8
|
+
const getLineBreak = line => line.match(/^(\n|\r\n|\r)/)?.[1] || '\n'
|
|
9
|
+
|
|
7
10
|
strings = asArray(strings)
|
|
8
11
|
// First, perform interpolation
|
|
9
12
|
let parts = []
|
|
@@ -21,16 +24,18 @@ export function deindent(strings, ...values) {
|
|
|
21
24
|
if (lines.length > 1) {
|
|
22
25
|
// Determine the indent by finding the immediately preceding white-space
|
|
23
26
|
// up to the previous line-break or the beginning of the string.
|
|
24
|
-
// (
|
|
25
|
-
//
|
|
26
|
-
// (\
|
|
27
|
-
// (
|
|
28
|
-
//
|
|
27
|
+
// (?:^[\n\r]*|[\n\r]) # Start at either the beginning or the previous
|
|
28
|
+
// # line break.
|
|
29
|
+
// (?:[\n\r]*) # The line break
|
|
30
|
+
// (\s+) # Collect the indenting white-space...
|
|
31
|
+
// (?:[^\n\r]*)$ # ...up to the end or the next word, but in last
|
|
32
|
+
// # line, by making sure no line breaks follow
|
|
33
|
+
// # until the end.
|
|
29
34
|
const str = parts.join('')
|
|
30
|
-
const match = str.match(/(
|
|
35
|
+
const match = str.match(/((?:^[\n\r]*|[\n\r])(?:[\n\r]*))(\s+)(?:[^\n\r]*)$/)
|
|
36
|
+
const [, lineBreaks = '', indent = ''] = match || []
|
|
31
37
|
parts = [str]
|
|
32
|
-
|
|
33
|
-
parts.push(lines.join(`${os.EOL}${indent}`))
|
|
38
|
+
parts.push(lines.join(`${getLineBreak(lineBreaks)}${indent}`))
|
|
34
39
|
} else {
|
|
35
40
|
parts.push(value)
|
|
36
41
|
}
|
|
@@ -42,7 +47,11 @@ export function deindent(strings, ...values) {
|
|
|
42
47
|
// const value = `
|
|
43
48
|
// content...
|
|
44
49
|
// ` // line break at the end remains
|
|
45
|
-
|
|
50
|
+
let offset = 0
|
|
51
|
+
if (!lines[0]) {
|
|
52
|
+
lines.shift()
|
|
53
|
+
offset++
|
|
54
|
+
}
|
|
46
55
|
let indent = Infinity
|
|
47
56
|
for (const line of lines) {
|
|
48
57
|
const match = line.match(/^(\s*)\S+/)
|
|
@@ -50,5 +59,7 @@ export function deindent(strings, ...values) {
|
|
|
50
59
|
indent = Math.min(indent, match[1].length)
|
|
51
60
|
}
|
|
52
61
|
}
|
|
53
|
-
|
|
62
|
+
const index = lines[0].length + offset
|
|
63
|
+
const lineBreak = getLineBreak(str.slice(index, index + 2))
|
|
64
|
+
return lines.map(line => line.slice(indent)).join(lineBreak)
|
|
54
65
|
}
|