Skip to content

Commit 6d3aa5c

Browse files
committed
package
1 parent c90d2c1 commit 6d3aa5c

File tree

4 files changed

+30
-206
lines changed

4 files changed

+30
-206
lines changed

packages/schema-typescript/package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,13 @@
3131
"dependencies": {
3232
"@babel/generator": "^7.24.4",
3333
"@babel/types": "^7.24.0",
34+
"@interweb-utils/casing": "^0.2.0",
3435
"deepmerge": "^4.3.1",
3536
"minimatch": "^9.0.4"
3637
},
37-
"keywords": ["jsonschema", "schema", "typescript"]
38+
"keywords": [
39+
"jsonschema",
40+
"schema",
41+
"typescript"
42+
]
3843
}

packages/schema-typescript/src/schema.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import generate from '@babel/generator';
22
import * as t from '@babel/types';
3+
import { isValidIdentifier, isValidIdentifierCamelized, toCamelCase } from '@interweb-utils/casing';
34

45
import { SchemaTSContext, type SchemaTSOptions } from './context';
56
import type { JSONSchema } from './types';
6-
import { getTypeNameSafe, isValidIdentifier, isValidIdentifierCamelized, makeCommentLine, shouldInclude, toCamelCase } from './utils';
7+
import { getTypeNameSafe, makeCommentLine, shouldInclude } from './utils';
78

89
const getSchemaTypeNameSafe = (ctx: SchemaTSContext, str: string): string => {
910
return getTypeNameSafe(ctx.options.namingStrategy, str);

packages/schema-typescript/src/utils.ts

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,9 @@
11
import * as t from '@babel/types';
2+
import { toPascalCase } from '@interweb-utils/casing';
23
import { minimatch } from 'minimatch';
34

45
import { SchemaNamingStrategy } from './context';
56

6-
export function toPascalCase(str: string) {
7-
return str.replace(/(^|_|\s|-)(\w)/g, (_: any, __: any, letter: string) => letter.toUpperCase()).replace(/[_\s-]/g, '');
8-
}
9-
10-
export function toCamelCase(key: string) {
11-
return key
12-
// First, remove all leading non-alphabet characters
13-
.replace(/^[^a-zA-Z]+/, '')
14-
// Convert what follows a separator into upper case
15-
.replace(/[-_\s]+(.)?/g, (_, c) => c ? c.toUpperCase() : '')
16-
// Ensure the first character of the result is always lowercase
17-
.replace(/^./, (c) => c.toLowerCase());
18-
}
19-
20-
// // Determine if the key is a valid JavaScript identifier
21-
export function isValidIdentifier(key: string) {
22-
return /^[$A-Z_][0-9A-Z_$]*$/i.test(key) && !/^[0-9]+$/.test(key);
23-
}
24-
25-
26-
// Determine if the key is a valid JavaScript-like identifier, allowing internal hyphens
27-
export function isValidIdentifierCamelized(key: string) {
28-
return /^[$A-Z_][0-9A-Z_$\-]*$/i.test(key) && !/^[0-9]+$/.test(key) && !/^-/.test(key);
29-
}
30-
317
export const getTypeNameSafe = (strategy: SchemaNamingStrategy, str: string): string => {
328
if (Object.prototype.hasOwnProperty.call(strategy.renameMap ?? {}, str)) {
339
return toPascalCase(strategy.renameMap[str]);
@@ -46,7 +22,6 @@ export const getTypeNameSafe = (strategy: SchemaNamingStrategy, str: string): st
4622
return toPascalCase(str);
4723
};
4824

49-
5025
const globPattern = /\*+([^+@!?\*\[\(]*)/;
5126

5227
interface ShouldIncludeOptions {

0 commit comments

Comments
 (0)