@lionweb/textgen-utils 0.7.0-beta.8 → 0.7.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/dist/general.d.ts CHANGED
@@ -16,5 +16,5 @@ export declare const withFirstUpper: (str: string) => string;
16
16
  * @param left thunk returning a string
17
17
  * @param right string
18
18
  */
19
- export declare const wrapInIf: (condition: boolean, left: () => string, right: string) => (text: string) => string;
19
+ export declare const wrapInIf: (condition: boolean, left: () => string, right: string) => ((text: string) => string);
20
20
  //# sourceMappingURL=general.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"general.d.ts","sourceRoot":"","sources":["../src/general.ts"],"names":[],"mappings":"AAmBA;;GAEG;AACH,eAAO,MAAM,MAAM,qEAAwB,CAAA;AAY3C;;GAEG;AACH,eAAO,MAAM,cAAc,QAXjB,MAAM,WAWsD,CAAA;AAEtE;;GAEG;AACH,eAAO,MAAM,cAAc,QAhBjB,MAAM,WAgBsD,CAAA;AAGtE;;;;;GAKG;AACH,eAAO,MAAM,QAAQ,cAAe,OAAO,QAAQ,MAAM,MAAM,SAAS,MAAM,YAAW,MAAM,KAAK,MAG5E,CAAA"}
1
+ {"version":3,"file":"general.d.ts","sourceRoot":"","sources":["../src/general.ts"],"names":[],"mappings":"AAmBA;;GAEG;AACH,eAAO,MAAM,MAAM,qEAAwB,CAAA;AAY3C;;GAEG;AACH,eAAO,MAAM,cAAc,QAXjB,MAAM,WAWsD,CAAA;AAEtE;;GAEG;AACH,eAAO,MAAM,cAAc,QAhBjB,MAAM,WAgBsD,CAAA;AAGtE;;;;;GAKG;AACH,eAAO,MAAM,QAAQ,GAAI,WAAW,OAAO,EAAE,MAAM,MAAM,MAAM,EAAE,OAAO,MAAM,KAAG,CAAC,CAAC,IAAI,EAAE,MAAM,KAAK,MAAM,CAGlF,CAAA"}
@@ -1 +1 @@
1
- {"version":3,"file":"javascript.d.ts","sourceRoot":"","sources":["../src/javascript.ts"],"names":[],"mappings":"AAiBA,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAA;AAG7C;;GAEG;AACH,eAAO,MAAM,cAAc,QAAS,MAAM,KAAG,MAGhB,CAAA;AAG7B;;GAEG;AACH,MAAM,MAAM,SAAS,GAAG,CAAC,cAAc,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,CAAC,CAAA;AAErE;;;;;GAKG;AACH,eAAO,MAAM,UAAU,eAAgB,MAAM,SAAS,SAAS,EAAE,gBAAgB,QAAQ,eAuBhF,CAAA;AAGT;;GAEG;AACH,eAAO,MAAM,YAAY,SAAU,OAAO,WACwB,CAAA"}
1
+ {"version":3,"file":"javascript.d.ts","sourceRoot":"","sources":["../src/javascript.ts"],"names":[],"mappings":"AAkBA,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAA;AAG7C;;GAEG;AACH,eAAO,MAAM,cAAc,GAAI,KAAK,MAAM,KAAG,MAGhB,CAAA;AAG7B;;GAEG;AACH,MAAM,MAAM,SAAS,GAAG,CAAC,cAAc,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,CAAC,CAAA;AAErE;;;;;GAKG;AACH,eAAO,MAAM,UAAU,GAAI,YAAY,MAAM,EAAE,OAAO,SAAS,EAAE,EAAE,cAAc,QAAQ,eAuBhF,CAAA;AAGT;;GAEG;AACH,eAAO,MAAM,YAAY,GAAI,MAAM,OAAO,WACmB,CAAA"}
@@ -14,6 +14,7 @@
14
14
  //
15
15
  // SPDX-FileCopyrightText: 2025 TRUMPF Laser SE and other contributors
16
16
  // SPDX-License-Identifier: Apache-2.0
17
+ import { asPrettyJsonString } from "@lionweb/ts-utils";
17
18
  import { indent } from "./general.js";
18
19
  /**
19
20
  * @return a sanitized version of the given string that should be suitable as a valid JavaScript identifier.
@@ -53,5 +54,5 @@ export const switchOrIf = (expression, cases, defaultBlock) => cases.length > 1
53
54
  /**
54
55
  * @return the given JSON as valid TypeScript source, meaning: without quotation marks around object keys.
55
56
  */
56
- export const asTypeScript = (json) => JSON.stringify(json, null, 4).replaceAll(/"(.+?)": /g, `$1: `);
57
+ export const asTypeScript = (json) => asPrettyJsonString(json).replaceAll(/"(.+?)": /g, `$1: `);
57
58
  //# sourceMappingURL=javascript.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"javascript.js","sourceRoot":"","sources":["../src/javascript.ts"],"names":[],"mappings":"AAAA,wDAAwD;AACxD,EAAE;AACF,iEAAiE;AACjE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,iDAAiD;AACjD,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;AACjC,EAAE;AACF,sEAAsE;AACtE,sCAAsC;AAGtC,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAA;AAErC;;GAEG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,GAAW,EAAU,EAAE,CAClD,GAAG;KACE,UAAU,CAAC,GAAG,EAAE,GAAG,CAAC;KACpB,UAAU,CAAC,GAAG,EAAE,GAAG,CAAC,CAAA;AAQ7B;;;;;GAKG;AACH,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,UAAkB,EAAE,KAAkB,EAAE,YAAsB,EAAE,EAAE,CACzF,KAAK,CAAC,MAAM,GAAG,CAAC;IACZ,CAAC,CAAC;QACE,WAAW,UAAU,KAAK;QAC1B,MAAM,CAAC;YACH,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,cAAc,EAAE,WAAW,CAAC,EAAE,EAAE,CAAC,QAAQ,cAAc,YAAY,WAAW,GAAG,CAAC;YAC9F,OAAO,YAAY,KAAK,QAAQ;gBAC5B,CAAC,CAAC,YAAY,YAAY,EAAE;gBAC5B,CAAC,CAAC;oBACE,YAAY;oBACZ,MAAM,CAAC,YAAY,CAAC;oBACpB,GAAG;iBACN;SACR,CAAC;QACF,GAAG;KACN;IACD,CAAC,CAAC;QACE,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,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AG,CAAC;YAC9F,OAAO,YAAY,KAAK,QAAQ;gBAC5B,CAAC,CAAC,YAAY,YAAY,EAAE;gBAC5B,CAAC,CAAC;oBACE,YAAY;oBACZ,MAAM,CAAC,YAAY,CAAC;oBACpB,GAAG;iBACN;SACR,CAAC;QACF,GAAG;KACN;IACD,CAAC,CAAC;QACE,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,cAAc,EAAE,WAAW,CAAC,EAAE,EAAE,CAAC;YACzC,OAAO,UAAU,QAAQ,cAAc,KAAK;YAC5C,MAAM,CAAC,UAAU,WAAW,GAAG,CAAC;YAChC,GAAG;SACN,CAAC;QACF,YAAY;KACf,CAAA;AAGT;;GAEG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,IAAa,EAAE,EAAE,CAC1C,kBAAkB,CAAC,IAAI,CAAC,CAAC,UAAU,CAAC,YAAY,EAAE,MAAM,CAAC,CAAA"}
package/package.json CHANGED
@@ -1,32 +1,33 @@
1
1
  {
2
- "name": "@lionweb/textgen-utils",
3
- "version": "0.7.0-beta.8",
4
- "description": "Utilities for text generation",
5
- "main": "dist/index.js",
6
- "types": "dist/index.d.ts",
7
- "typings": "dist/index.d.ts",
8
- "type": "module",
9
- "license": "Apache-2.0",
10
- "repository": {
11
- "type": "git",
12
- "url": "git+https://github.com/LionWeb-io/lionweb-typescript.git"
13
- },
14
- "bugs": {
15
- "url": "https://github.com/LionWeb-io/lionweb-typescript/issues"
16
- },
17
- "scripts": {
18
- "clean": "npx rimraf dist node_modules -g lionweb-textgen-utils-*.tgz",
19
- "build": "tsc",
20
- "lint": "eslint src",
21
- "prep:pre-release": "npm run clean && npm install && npm run build",
22
- "prerelease-alpha": "npm run prep:pre-release",
23
- "release-alpha": "npm publish --tag alpha",
24
- "prerelease-beta": "npm run prep:pre-release",
25
- "release-beta": "npm publish --tag beta",
26
- "prerelease": "npm run prep:pre-release",
27
- "release": "npm publish"
28
- },
29
- "dependencies": {
30
- "littoral-templates": "0.5.0"
31
- }
2
+ "name": "@lionweb/textgen-utils",
3
+ "version": "0.7.0",
4
+ "description": "Utilities for text generation",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "typings": "dist/index.d.ts",
8
+ "type": "module",
9
+ "license": "Apache-2.0",
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/LionWeb-io/lionweb-typescript.git"
13
+ },
14
+ "bugs": {
15
+ "url": "https://github.com/LionWeb-io/lionweb-typescript/issues"
16
+ },
17
+ "scripts": {
18
+ "clean": "npx rimraf dist node_modules -g lionweb-textgen-utils-*.tgz",
19
+ "build": "tsc",
20
+ "lint": "eslint src",
21
+ "prep:pre-release": "npm run clean && npm install && npm run build",
22
+ "prerelease-alpha": "npm run prep:pre-release",
23
+ "release-alpha": "npm publish --tag alpha",
24
+ "prerelease-beta": "npm run prep:pre-release",
25
+ "release-beta": "npm publish --tag beta",
26
+ "prerelease": "npm run prep:pre-release",
27
+ "release": "npm publish"
28
+ },
29
+ "dependencies": {
30
+ "@lionweb/ts-utils": "0.7.0",
31
+ "littoral-templates": "0.5.1"
32
+ }
32
33
  }
package/src/general.ts ADDED
@@ -0,0 +1,57 @@
1
+ // Copyright 2025 TRUMPF Laser SE and other contributors
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License")
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ //
15
+ // SPDX-FileCopyrightText: 2025 TRUMPF Laser SE and other contributors
16
+ // SPDX-License-Identifier: Apache-2.0
17
+
18
+ import { indentWith } from "littoral-templates"
19
+
20
+ /**
21
+ * @return an indenter function that indents by 4 spaces.
22
+ */
23
+ export const indent = indentWith(" ")(1)
24
+
25
+
26
+ const withFirstCased = (charFunc: (ch: string) => string) =>
27
+ (str: string) => {
28
+ if (str.length === 0) {
29
+ return str
30
+ }
31
+ return charFunc(str.charAt(0))
32
+ + (str.length > 1 ? str.substring(1) : "")
33
+ }
34
+
35
+ /**
36
+ * @return the given string but with its first character lower-cased (when possible).
37
+ */
38
+ export const withFirstLower = withFirstCased((ch) => ch.toLowerCase())
39
+
40
+ /**
41
+ * @return the given string but with its first character upper-cased (when possible).
42
+ */
43
+ export const withFirstUpper = withFirstCased((ch) => ch.toUpperCase())
44
+
45
+
46
+ /**
47
+ * @return a function that takes one string argument, and returns that argument wrapped between `left` and `right`, provided `condition` is true.
48
+ * @param condition boolean
49
+ * @param left thunk returning a string
50
+ * @param right string
51
+ */
52
+ export const wrapInIf = (condition: boolean, left: () => string, right: string): ((text: string) => string) =>
53
+ condition
54
+ ? (text) => `${left()}${text}${right}` // (don't pre-eval left, as it might have side-effects)
55
+ : (text) => text
56
+
57
+
package/src/index.ts ADDED
@@ -0,0 +1,20 @@
1
+ // Copyright 2025 TRUMPF Laser SE and other contributors
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License")
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ //
15
+ // SPDX-FileCopyrightText: 2025 TRUMPF Laser SE and other contributors
16
+ // SPDX-License-Identifier: Apache-2.0
17
+
18
+ export * from "./javascript.js"
19
+ export * from "./general.js"
20
+
@@ -0,0 +1,73 @@
1
+ // Copyright 2025 TRUMPF Laser SE and other contributors
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License")
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ //
15
+ // SPDX-FileCopyrightText: 2025 TRUMPF Laser SE and other contributors
16
+ // SPDX-License-Identifier: Apache-2.0
17
+
18
+ import { asPrettyJsonString } from "@lionweb/ts-utils"
19
+ import { Template } from "littoral-templates"
20
+ import { indent } from "./general.js"
21
+
22
+ /**
23
+ * @return a sanitized version of the given string that should be suitable as a valid JavaScript identifier.
24
+ */
25
+ export const asJSIdentifier = (str: string): string =>
26
+ str
27
+ .replaceAll(".", "_")
28
+ .replaceAll("-", "_")
29
+
30
+
31
+ /**
32
+ * A type def. that captures one `case` of a TypeScript `switch` statement.
33
+ */
34
+ export type MatchCase = [caseExpression: string, returnValue: string]
35
+
36
+ /**
37
+ * @return a JavaScript `switch` statement (as a {@link Template template}).
38
+ * @param expression string with a valid JS expression
39
+ * @param cases `cases` of the `switch` statement, encoded using {@link MatchCase}
40
+ * @param defaultBlock template with a valid `default` block – assumed to be a single-line string if it's a string
41
+ */
42
+ export const switchOrIf = (expression: string, cases: MatchCase[], defaultBlock: Template) =>
43
+ cases.length > 1
44
+ ? [
45
+ `switch (${expression}) {`,
46
+ indent([
47
+ cases.map(([caseExpression, returnValue]) => `case ${caseExpression}: return ${returnValue};`),
48
+ typeof defaultBlock === "string"
49
+ ? `default: ${defaultBlock}`
50
+ : [
51
+ `default: {`,
52
+ indent(defaultBlock),
53
+ `}`
54
+ ]
55
+ ]),
56
+ `}`
57
+ ]
58
+ : [
59
+ cases.map(([caseExpression, returnValue]) => [
60
+ `if (${expression} === ${caseExpression}) {`,
61
+ indent(`return ${returnValue};`),
62
+ `}`
63
+ ]),
64
+ defaultBlock
65
+ ]
66
+
67
+
68
+ /**
69
+ * @return the given JSON as valid TypeScript source, meaning: without quotation marks around object keys.
70
+ */
71
+ export const asTypeScript = (json: unknown) =>
72
+ asPrettyJsonString(json).replaceAll(/"(.+?)": /g, `$1: `)
73
+