Skip to content

Commit

Permalink
package
Browse files Browse the repository at this point in the history
  • Loading branch information
pyramation committed May 25, 2024
1 parent c90d2c1 commit 6d3aa5c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 206 deletions.
7 changes: 6 additions & 1 deletion packages/schema-typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,13 @@
"dependencies": {
"@babel/generator": "^7.24.4",
"@babel/types": "^7.24.0",
"@interweb-utils/casing": "^0.2.0",
"deepmerge": "^4.3.1",
"minimatch": "^9.0.4"
},
"keywords": ["jsonschema", "schema", "typescript"]
"keywords": [
"jsonschema",
"schema",
"typescript"
]
}
3 changes: 2 additions & 1 deletion packages/schema-typescript/src/schema.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import generate from '@babel/generator';
import * as t from '@babel/types';
import { isValidIdentifier, isValidIdentifierCamelized, toCamelCase } from '@interweb-utils/casing';

import { SchemaTSContext, type SchemaTSOptions } from './context';
import type { JSONSchema } from './types';
import { getTypeNameSafe, isValidIdentifier, isValidIdentifierCamelized, makeCommentLine, shouldInclude, toCamelCase } from './utils';
import { getTypeNameSafe, makeCommentLine, shouldInclude } from './utils';

const getSchemaTypeNameSafe = (ctx: SchemaTSContext, str: string): string => {
return getTypeNameSafe(ctx.options.namingStrategy, str);
Expand Down
27 changes: 1 addition & 26 deletions packages/schema-typescript/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,9 @@
import * as t from '@babel/types';
import { toPascalCase } from '@interweb-utils/casing';
import { minimatch } from 'minimatch';

import { SchemaNamingStrategy } from './context';

export function toPascalCase(str: string) {
return str.replace(/(^|_|\s|-)(\w)/g, (_: any, __: any, letter: string) => letter.toUpperCase()).replace(/[_\s-]/g, '');
}

export function toCamelCase(key: string) {
return key
// First, remove all leading non-alphabet characters
.replace(/^[^a-zA-Z]+/, '')
// Convert what follows a separator into upper case
.replace(/[-_\s]+(.)?/g, (_, c) => c ? c.toUpperCase() : '')
// Ensure the first character of the result is always lowercase
.replace(/^./, (c) => c.toLowerCase());
}

// // Determine if the key is a valid JavaScript identifier
export function isValidIdentifier(key: string) {
return /^[$A-Z_][0-9A-Z_$]*$/i.test(key) && !/^[0-9]+$/.test(key);
}


// Determine if the key is a valid JavaScript-like identifier, allowing internal hyphens
export function isValidIdentifierCamelized(key: string) {
return /^[$A-Z_][0-9A-Z_$\-]*$/i.test(key) && !/^[0-9]+$/.test(key) && !/^-/.test(key);
}

export const getTypeNameSafe = (strategy: SchemaNamingStrategy, str: string): string => {
if (Object.prototype.hasOwnProperty.call(strategy.renameMap ?? {}, str)) {
return toPascalCase(strategy.renameMap[str]);
Expand All @@ -46,7 +22,6 @@ export const getTypeNameSafe = (strategy: SchemaNamingStrategy, str: string): st
return toPascalCase(str);
};


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

interface ShouldIncludeOptions {
Expand Down
Loading

0 comments on commit 6d3aa5c

Please sign in to comment.