first commit

This commit is contained in:
编码猿
2024-09-27 01:15:47 +08:00
commit 8be1fcce6b
155 changed files with 9435 additions and 0 deletions

1
json-class-interface/dist/.pydio vendored Normal file
View File

@@ -0,0 +1 @@
9b3befbc-a965-48c7-ae4b-8602394b74c8

2
json-class-interface/dist/file.d.ts vendored Normal file
View File

@@ -0,0 +1,2 @@
import { PathLike } from "fs";
export declare const configFilePath: (path: PathLike) => void;

11
json-class-interface/dist/file.js vendored Normal file
View File

@@ -0,0 +1,11 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.configFilePath = void 0;
const fs_1 = require("fs");
const store_1 = require("./store");
const configFilePath = (path) => {
store_1.config.saveTypeFilePath = path;
console.log("config.saveTypeFilePath: ", store_1.config.saveTypeFilePath);
(0, fs_1.writeFileSync)(store_1.config.saveTypeFilePath, "testetststststststst");
};
exports.configFilePath = configFilePath;

3
json-class-interface/dist/index.d.ts vendored Normal file
View File

@@ -0,0 +1,3 @@
export * from './toClass';
export * from './toInterface';
export * from './file';

15
json-class-interface/dist/index.js vendored Normal file
View File

@@ -0,0 +1,15 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
__exportStar(require("./toClass"), exports);
__exportStar(require("./toInterface"), exports);
__exportStar(require("./file"), exports);

2
json-class-interface/dist/store.d.ts vendored Normal file
View File

@@ -0,0 +1,2 @@
import { Config } from "./typing";
export declare const config: Config;

6
json-class-interface/dist/store.js vendored Normal file
View File

@@ -0,0 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.config = void 0;
exports.config = {
saveTypeFilePath: ""
};

View File

@@ -0,0 +1,2 @@
import { JosnType, ToClassOptions } from "./typing";
export declare const toClass: (className: string, rawJson: JosnType | string, options?: ToClassOptions, repeat?: boolean) => string;

38
json-class-interface/dist/toClass.js vendored Normal file
View File

@@ -0,0 +1,38 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.toClass = void 0;
const utils_1 = require("./utils");
let TextResult = "";
const toClass = (className = "JsonToClass", rawJson, options = {}, repeat = false) => {
typeof (rawJson) === "string" ? rawJson = JSON.parse(rawJson) : null;
(0, utils_1.isArray)(rawJson) ? rawJson = rawJson[0] : null;
if (!repeat) {
TextResult = "";
}
let classText = (0, utils_1.stripIndent) `
${(0, utils_1.setInterfaceModel)(options)} ${className} {
${Object.keys(rawJson).map(key => {
let ArrayInsideNotObject = false;
if ((0, utils_1.isObjectOrArray)(rawJson[key])) {
if ((0, utils_1.isArray)(rawJson[key]) && (0, utils_1.isObject)(rawJson[key][0])) {
(0, exports.toClass)((0, utils_1.firstLetterCap)(key), rawJson[key], options, true);
}
if ((0, utils_1.isObject)(rawJson[key])) {
ArrayInsideNotObject = false;
(0, exports.toClass)((0, utils_1.firstLetterCap)(key), rawJson[key], options, true);
}
}
return `${(0, utils_1.setDefaultNeedPropertyValue)(options, key) == null ? '' : (0, utils_1.setDefaultNeedPropertyValue)(options, key)}
${(0, utils_1.setKeyNotNull)(options, key)}: ${ArrayInsideNotObject
? `${typeof rawJson[key][0]}[]`
: (0, utils_1.isObjectOrArray)(rawJson[key])
? `${(0, utils_1.firstLetterCap)(key)}${(0, utils_1.isArray)(rawJson[key]) ? '[]' : ''}`
: (0, utils_1.isNull)(rawJson[key]) ? (0, utils_1.setDefaultNull)(options) : typeof rawJson[key]};
`;
})}
}
`;
TextResult += `${classText} \n\n`;
return TextResult;
};
exports.toClass = toClass;

View File

@@ -0,0 +1,2 @@
import { JosnType, ToClassOptions } from "./typing";
export declare const toInterface: (className: string, rawJson: JosnType | string, options?: ToClassOptions) => string;

View File

@@ -0,0 +1,9 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.toInterface = void 0;
const toClass_1 = require("./toClass");
const toInterface = (className = "JsonToInterface", rawJson, options = {}) => {
options.interfaceModel = true;
return (0, toClass_1.toClass)(className, rawJson, options);
};
exports.toInterface = toInterface;

14
json-class-interface/dist/typing.d.ts vendored Normal file
View File

@@ -0,0 +1,14 @@
/// <reference types="node" />
import { PathLike } from "fs";
export declare type JosnType = {
[key: string]: any;
};
export declare type Config = {
saveTypeFilePath: PathLike;
};
export interface ToClassOptions {
nullAlias?: string;
needProperty?: boolean;
needPropertyValue?: boolean;
interfaceModel?: boolean;
}

2
json-class-interface/dist/typing.js vendored Normal file
View File

@@ -0,0 +1,2 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });

13
json-class-interface/dist/utils.d.ts vendored Normal file
View File

@@ -0,0 +1,13 @@
import { ToClassOptions } from "./typing";
export declare const isNull: (val: any) => boolean;
export declare const isUndefined: (val: any) => boolean;
export declare const isNullOrUndefined: (val: any) => boolean;
export declare const isArray: (val: any) => boolean;
export declare const isObject: (val: any) => boolean;
export declare const isObjectOrArray: (val: any) => boolean;
export declare const firstLetterCap: (val: string) => string;
export declare const setDefaultNull: (options: ToClassOptions) => string;
export declare const setDefaultNeedPropertyValue: (options: ToClassOptions, key: string) => string;
export declare const setInterfaceModel: (options: ToClassOptions) => "export interface" | "export class";
export declare const setKeyNotNull: (options: ToClassOptions, key: string) => string;
export declare const stripIndent: (template: TemplateStringsArray, ...expressions: any[]) => string;

56
json-class-interface/dist/utils.js vendored Normal file
View File

@@ -0,0 +1,56 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.stripIndent = exports.setKeyNotNull = exports.setInterfaceModel = exports.setDefaultNeedPropertyValue = exports.setDefaultNull = exports.firstLetterCap = exports.isObjectOrArray = exports.isObject = exports.isArray = exports.isNullOrUndefined = exports.isUndefined = exports.isNull = void 0;
const isNull = (val) => val === null;
exports.isNull = isNull;
const isUndefined = (val) => val === undefined;
exports.isUndefined = isUndefined;
const isNullOrUndefined = (val) => (0, exports.isNull)(val) || (0, exports.isUndefined)(val);
exports.isNullOrUndefined = isNullOrUndefined;
const isArray = (val) => Array.isArray(val);
exports.isArray = isArray;
const isObject = (val) => (0, exports.isNull)(val) ? false : val.constructor === Object;
exports.isObject = isObject;
const isObjectOrArray = (val) => (0, exports.isObject)(val) || (0, exports.isArray)(val);
exports.isObjectOrArray = isObjectOrArray;
const firstLetterCap = (val) => {
let splitArr = val.split("_");
return splitArr.map(v => v.charAt(0).toUpperCase() + v.slice(1)).join("");
};
exports.firstLetterCap = firstLetterCap;
const setDefaultNull = (options) => options?.nullAlias ? options?.nullAlias : "string";
exports.setDefaultNull = setDefaultNull;
const setDefaultNeedPropertyValue = (options, key) => {
options.needProperty = options.needProperty || false;
return options?.needProperty
? options?.interfaceModel
? null
: options?.needPropertyValue
? `\t\n@property("${key}")`
: `@property()`
: null;
};
exports.setDefaultNeedPropertyValue = setDefaultNeedPropertyValue;
const setInterfaceModel = (options) => options?.interfaceModel ? 'export interface' : `export class`;
exports.setInterfaceModel = setInterfaceModel;
const setKeyNotNull = (options, key) => `${key}${options?.interfaceModel ? '' : '!'}`;
exports.setKeyNotNull = setKeyNotNull;
const stripIndent = (template, ...expressions) => {
let result = template.reduce((prev, next, i) => {
let expression = expressions[i - 1];
if ((0, exports.isArray)(expression)) {
expression = expression.join('');
}
return prev + expression + next;
});
const match = result.match(/^[^\S\n]*(?=\S)/gm);
const indent = match && Math.min(...match.map(el => el.length));
if (indent) {
const regexp = new RegExp(`^.{${indent}}`, 'gm');
result = result.replace(regexp, '');
}
result = result.replace(/^[^\S\n]+/gm, ' ');
result = result.trim();
return result;
};
exports.stripIndent = stripIndent;