littoral-templates 0.5.0 → 0.5.1
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/CHANGELOG.md +5 -0
- package/dist/primitives.d.ts.map +1 -1
- package/dist/primitives.js +3 -0
- package/dist/primitives.js.map +1 -1
- package/dist/types.d.ts +1 -1
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/eol.ts +37 -0
- package/src/functions.ts +50 -0
- package/src/indents.ts +47 -0
- package/src/index-internal.ts +16 -0
- package/src/index.ts +15 -0
- package/src/primitives.ts +31 -0
- package/src/strings.ts +32 -0
- package/src/types.ts +18 -0
package/CHANGELOG.md
CHANGED
package/dist/primitives.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"primitives.d.ts","sourceRoot":"","sources":["../src/primitives.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAM,QAAQ,EAAC,MAAM,qBAAqB,CAAA;AAIjD;;;GAGG;AACH,eAAO,MAAM,SAAS,aAAc,QAAQ,KAAG,MAAM,
|
|
1
|
+
{"version":3,"file":"primitives.d.ts","sourceRoot":"","sources":["../src/primitives.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAM,QAAQ,EAAC,MAAM,qBAAqB,CAAA;AAIjD;;;GAGG;AACH,eAAO,MAAM,SAAS,aAAc,QAAQ,KAAG,MAAM,EAUpD,CAAA;AAGD;;GAEG;AACH,eAAO,MAAM,QAAQ,aAAc,QAAQ,KAAG,MACY,CAAA"}
|
package/dist/primitives.js
CHANGED
|
@@ -17,6 +17,9 @@ var eolRegex = new RegExp(/\r*\n/);
|
|
|
17
17
|
* Note: doesn't need to be exposed.
|
|
18
18
|
*/
|
|
19
19
|
export var flattened = function (template) {
|
|
20
|
+
if (template === undefined || template === null) {
|
|
21
|
+
return [];
|
|
22
|
+
}
|
|
20
23
|
if (typeof template === "function") {
|
|
21
24
|
return flattened(template());
|
|
22
25
|
}
|
package/dist/primitives.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"primitives.js","sourceRoot":"","sources":["../src/primitives.ts"],"names":[],"mappings":"AAAA;;GAEG;;;;;;;;;;AAEH,OAAO,EAAC,GAAG,EAAW,MAAM,qBAAqB,CAAA;AAEjD,IAAM,QAAQ,GAAG,IAAI,MAAM,CAAC,OAAO,CAAC,CAAA;AAEpC;;;GAGG;AACH,MAAM,CAAC,IAAM,SAAS,GAAG,UAAC,QAAkB;IACxC,IAAI,OAAO,QAAQ,KAAK,UAAU,EAAE,CAAC;
|
|
1
|
+
{"version":3,"file":"primitives.js","sourceRoot":"","sources":["../src/primitives.ts"],"names":[],"mappings":"AAAA;;GAEG;;;;;;;;;;AAEH,OAAO,EAAC,GAAG,EAAW,MAAM,qBAAqB,CAAA;AAEjD,IAAM,QAAQ,GAAG,IAAI,MAAM,CAAC,OAAO,CAAC,CAAA;AAEpC;;;GAGG;AACH,MAAM,CAAC,IAAM,SAAS,GAAG,UAAC,QAAkB;IACxC,IAAI,QAAQ,KAAK,SAAS,IAAI,QAAQ,KAAK,IAAI,EAAE,CAAC;QAC9C,OAAO,EAAE,CAAA;IACb,CAAC;IAAI,IAAI,OAAO,QAAQ,KAAK,UAAU,EAAE,CAAC;QACtC,OAAO,SAAS,CAAC,QAAQ,EAAE,CAAC,CAAA;IAChC,CAAC;IACD,IAAI,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAC;QAC1B,OAAO,QAAQ,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC,MAAM,CAAC,UAAC,IAAI,EAAE,IAAI,IAAK,uCAAI,IAAI,SAAK,IAAI,SAAjB,CAAkB,EAAE,EAAE,CAAC,CAAA;IACjF,CAAC;IACD,OAAO,QAAQ,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAA;AACnC,CAAC,CAAA;AAGD;;GAEG;AACH,MAAM,CAAC,IAAM,QAAQ,GAAG,UAAC,QAAkB;IACvC,OAAA,SAAS,CAAC,QAAQ,CAAC,CAAC,GAAG,CAAC,UAAC,IAAI,IAAK,OAAA,IAAI,GAAG,GAAG,EAAV,CAAU,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC;AAAtD,CAAsD,CAAA"}
|
package/dist/types.d.ts
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
*
|
|
8
8
|
* @since version 0.3.0
|
|
9
9
|
*/
|
|
10
|
-
export type Template = string | (() => Template)
|
|
10
|
+
export type Template = string | undefined | null | Array<Template> | (() => Template);
|
|
11
11
|
/**
|
|
12
12
|
* Legacy alias for the {@link Template} type definition.
|
|
13
13
|
* Note: this type might be deprecated and removed (per a major version) in the future.
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;;;;GAKG;AACH,MAAM,MAAM,QAAQ,GAAG,MAAM,GAAG,CAAC,
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;;;;GAKG;AACH,MAAM,MAAM,QAAQ,GAAG,MAAM,GAAG,SAAS,GAAG,IAAI,GAAG,KAAK,CAAC,QAAQ,CAAC,GAAG,CAAC,MAAM,QAAQ,CAAC,CAAA;AAErF;;;GAGG;AACH,MAAM,MAAM,YAAY,GAAG,QAAQ,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "littoral-templates",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.1",
|
|
4
4
|
"description": "A small JavaScript/TypeScript framework to do templating comfortably using the template literal syntax in either JavaScript or TypeScript",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Meinte Boersma",
|
package/src/eol.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* EOL-related constants and functions.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Common EOL styles.
|
|
7
|
+
*/
|
|
8
|
+
export const eolStyles = {
|
|
9
|
+
lf: "\n",
|
|
10
|
+
crlf: "\r\n"
|
|
11
|
+
} as const
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The value of the EOL string: typically, either "\n" (=LF-style, compatible with POSIX/Linux/macOS), or "\r\n" (=CRLF-style, compatible with Windows).
|
|
16
|
+
*/
|
|
17
|
+
export let eol: string = eolStyles.lf
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Sets the EOL explicitly to the given value.
|
|
22
|
+
* (This is especially useful for testing.)
|
|
23
|
+
*/
|
|
24
|
+
export const setEOLExplicitly = (newEol: string) => {
|
|
25
|
+
eol = newEol
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
const splitRegex = new RegExp(/\r*\n/)
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Splits the given string on EOLs (of any style) into an array of strings,
|
|
33
|
+
* each corresponding to one line.
|
|
34
|
+
*/
|
|
35
|
+
export const splitOnEOL = (str: string): string[] =>
|
|
36
|
+
str.split(splitRegex)
|
|
37
|
+
|
package/src/functions.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Non-primitive functions on the {@link Template} type.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import {flattened, Template} from "./index-internal.js"
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Allows for the following syntax:
|
|
9
|
+
* <pre>
|
|
10
|
+
* [
|
|
11
|
+
* when(<some boolean condition>)(
|
|
12
|
+
* <stuff to include when boolean condition is true>
|
|
13
|
+
* )
|
|
14
|
+
* ]
|
|
15
|
+
* </pre>
|
|
16
|
+
*
|
|
17
|
+
* and
|
|
18
|
+
*
|
|
19
|
+
* <pre>
|
|
20
|
+
* [
|
|
21
|
+
* when(<some boolean condition>)(
|
|
22
|
+
* () => <stuff to include when boolean condition is true>
|
|
23
|
+
* )
|
|
24
|
+
* ]
|
|
25
|
+
* </pre>
|
|
26
|
+
*
|
|
27
|
+
* In the latter case, it's guaranteed that the thunk ({@see https://en.wikipedia.org/wiki/Thunk})
|
|
28
|
+
* is only evaluated (/run) when the boolean condition is true.
|
|
29
|
+
* That is useful in case the stuff to include produces side effects.
|
|
30
|
+
*
|
|
31
|
+
* Note that the second Curried argument is variadic, so it doesn't require array brackets (`[]`).
|
|
32
|
+
*/
|
|
33
|
+
export const when = (bool: boolean): ((...whenResults: Template[]) => Template) =>
|
|
34
|
+
bool
|
|
35
|
+
? (...whenResults: Template[]) => flattened(whenResults) // (need to be explicit here, or it doesn't work – despite having specified the return type...)
|
|
36
|
+
: (_) => []
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* @return a function that composes the given template function with the action of "adding an empty line after".
|
|
41
|
+
*/
|
|
42
|
+
export const withEmptyLineAppended = <T>(templateFunc: (t: T) => Template) =>
|
|
43
|
+
(t: T) => [templateFunc(t), ``]
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* A legacy alias of {@link withEmptyLineAppended}.
|
|
47
|
+
* Note: this function might be deprecated and removed (per a major version) in the future.
|
|
48
|
+
*/
|
|
49
|
+
export const withNewlineAppended = withEmptyLineAppended
|
|
50
|
+
|
package/src/indents.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Indentation-related constants and functions.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import {flattened, repeat, Template} from "./index-internal.js"
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @returns a function to instantiate a function to indent a sub-template.
|
|
9
|
+
* The function always returns an array of strings.
|
|
10
|
+
*
|
|
11
|
+
* Its usage looks as follows:
|
|
12
|
+
* <pre>
|
|
13
|
+
* indentWith(" ")(2)([
|
|
14
|
+
* `this is indented 2 levels`
|
|
15
|
+
* ])
|
|
16
|
+
* </pre>
|
|
17
|
+
* Note that the third Curried argument is variadic, so the array brackets (`[]`) can be elided.
|
|
18
|
+
*
|
|
19
|
+
* Usually, one sets up the following function constant before:
|
|
20
|
+
* <pre>
|
|
21
|
+
* const indent = indentWith(" ")
|
|
22
|
+
* </pre>
|
|
23
|
+
*/
|
|
24
|
+
export const indentWith = (singleIndentation: string) =>
|
|
25
|
+
(indentLevel: number = 1) => {
|
|
26
|
+
const prefix = repeat(singleIndentation, indentLevel)
|
|
27
|
+
const indenter = (line: string) => line.length > 0 ? (prefix + line) : line
|
|
28
|
+
return (...templates: Template[]) =>
|
|
29
|
+
flattened(templates).map(indenter) // Note: Template[] has type Template as well!
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* An enumeration of common indentation styles.
|
|
34
|
+
* Use these in a type-safe way as follows, e.g. for “1 tab”:
|
|
35
|
+
*
|
|
36
|
+
* ```
|
|
37
|
+
* const indent = indentWith(commonIndentations["1 tab"])
|
|
38
|
+
*
|
|
39
|
+
* indent([`foo`])
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
export const commonIndentations = {
|
|
43
|
+
"2 spaces": " ",
|
|
44
|
+
"4 spaces": " ",
|
|
45
|
+
"1 tab": "\t"
|
|
46
|
+
} as const
|
|
47
|
+
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export * from "./eol.js"
|
|
2
|
+
export * from "./functions.js"
|
|
3
|
+
export * from "./indents.js"
|
|
4
|
+
export * from "./primitives.js"
|
|
5
|
+
export * from "./strings.js"
|
|
6
|
+
export * from "./types.js"
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* To avoid problems with circular dependencies between JavaScript – i.e.: transpiled TypeScript – files,
|
|
10
|
+
* members in any .ts file *must* be imported through this index-internal.ts, e.g.:
|
|
11
|
+
*
|
|
12
|
+
* `import {asString} from "./index-internal.js"`
|
|
13
|
+
*
|
|
14
|
+
* (Mind the .js extension!)
|
|
15
|
+
*/
|
|
16
|
+
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export {
|
|
2
|
+
asString,
|
|
3
|
+
commaSeparated,
|
|
4
|
+
commonIndentations,
|
|
5
|
+
indentWith,
|
|
6
|
+
setEOLExplicitly,
|
|
7
|
+
when,
|
|
8
|
+
withEmptyLineAppended,
|
|
9
|
+
withNewlineAppended // (legacy alias)
|
|
10
|
+
} from "./index-internal.js"
|
|
11
|
+
export type {
|
|
12
|
+
Template,
|
|
13
|
+
NestedString // (legacy alias)
|
|
14
|
+
} from "./index-internal.js"
|
|
15
|
+
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Primitive functions on the {@link Template} type.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import {eol, Template} from "./index-internal.js"
|
|
6
|
+
|
|
7
|
+
const eolRegex = new RegExp(/\r*\n/)
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* “Flattens” the given template to an array of strings, each of which represent exactly one line.
|
|
11
|
+
* Note: doesn't need to be exposed.
|
|
12
|
+
*/
|
|
13
|
+
export const flattened = (template: Template): string[] => {
|
|
14
|
+
if (template === undefined || template === null) {
|
|
15
|
+
return []
|
|
16
|
+
} if (typeof template === "function") {
|
|
17
|
+
return flattened(template())
|
|
18
|
+
}
|
|
19
|
+
if (Array.isArray(template)) {
|
|
20
|
+
return template.map(flattened).reduce((arrL, arrR) => [...arrL, ...arrR], [])
|
|
21
|
+
}
|
|
22
|
+
return template.split(eolRegex)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @returns {string} - the given template joined as one string, taking care of proper EOLs.
|
|
28
|
+
*/
|
|
29
|
+
export const asString = (template: Template): string =>
|
|
30
|
+
flattened(template).map((line) => line + eol).join("")
|
|
31
|
+
|
package/src/strings.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Purely string-related functions.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @return the given array of strings but with commas added after each string except the last one.
|
|
7
|
+
*/
|
|
8
|
+
export const commaSeparated = (strings: string[])=>
|
|
9
|
+
strings.map((str, index) => `${str}${index + 1 < strings.length ? `,` : ``}`)
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Polyfill/shim for ES2015's String.prototype.repeat which doesn't work for some reason in the test...
|
|
13
|
+
* (Implementation uses the binary representation of n.)
|
|
14
|
+
* Note: doesn't need to be exposed.
|
|
15
|
+
*/
|
|
16
|
+
export const repeat = (str: string, n: number): string => {
|
|
17
|
+
let result = ""
|
|
18
|
+
while (n > 0) {
|
|
19
|
+
if (n % 2 === 1) {
|
|
20
|
+
result += str
|
|
21
|
+
}
|
|
22
|
+
if (n > 1) {
|
|
23
|
+
str += str
|
|
24
|
+
}
|
|
25
|
+
n >>= 1
|
|
26
|
+
}
|
|
27
|
+
return result
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
// TODO more common string utils, e.g. withFirst{Upper|Lower}, etc.
|
|
32
|
+
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Types for templates.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* A type definition for “templates” that consist of a string, or an array of templates, or a thunk returning a template.
|
|
7
|
+
* Note that nesting can occur to arbitrary (finite) depth.
|
|
8
|
+
*
|
|
9
|
+
* @since version 0.3.0
|
|
10
|
+
*/
|
|
11
|
+
export type Template = string | undefined | null | Array<Template> | (() => Template)
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Legacy alias for the {@link Template} type definition.
|
|
15
|
+
* Note: this type might be deprecated and removed (per a major version) in the future.
|
|
16
|
+
*/
|
|
17
|
+
export type NestedString = Template
|
|
18
|
+
|