@jsonhero/parser
Advanced tools
+119
| import { JSONHeroPath } from '@jsonhero/path'; | ||
| import { inferType } from '@jsonhero/json-infer-types'; | ||
| import { WildcardPathComponent } from '@jsonhero/path/lib/path/wildcard-path-component'; | ||
| function friendlyName(name) { | ||
| name = snakeCase(name); | ||
| name = dashCase(name); | ||
| name = camelCase(name); | ||
| name = toTitleCase(name); | ||
| return name; | ||
| } | ||
| function camelCase(name) { | ||
| return (name | ||
| // insert a space before all caps | ||
| .replace(/(\w)([A-Z])/g, '$1 $2')); | ||
| } | ||
| function snakeCase(name) { | ||
| return (name | ||
| // insert a space before all caps | ||
| .replace(/(_)/g, ' ')); | ||
| } | ||
| function dashCase(name) { | ||
| return (name | ||
| // insert a space before all caps | ||
| .replace(/(-)/g, ' ')); | ||
| } | ||
| function toTitleCase(name) { | ||
| return (name | ||
| //after every space | ||
| .replace(/ ([a-z])/g, function (str) { | ||
| return str.toUpperCase(); | ||
| }) | ||
| //first character | ||
| .replace(/^./, function (str) { | ||
| return str.toUpperCase(); | ||
| })); | ||
| } | ||
| function parse(object) { | ||
| var rootPath = '$'; | ||
| var parsedObject = { | ||
| values: { | ||
| rootPath: rootPath, | ||
| values: {}, | ||
| }, | ||
| structure: { | ||
| rootPath: rootPath, | ||
| values: {}, | ||
| }, | ||
| }; | ||
| buildValueTree(object, rootPath, 'Root', parsedObject.values.values); | ||
| buildStructureTree(object, rootPath, 'Root', parsedObject.structure.values); | ||
| return parsedObject; | ||
| } | ||
| function buildValueTree(object, path, name, valueCollection) { | ||
| var valueInfo = { | ||
| path: path, | ||
| name: name, | ||
| displayName: friendlyName(name), | ||
| value: object, | ||
| type: inferType(object), | ||
| children: null, | ||
| }; | ||
| valueCollection[path] = valueInfo; | ||
| //for any children add to children and then recursively run this | ||
| if (isCollection(valueInfo.type)) { | ||
| var parentPath = new JSONHeroPath(path); | ||
| valueInfo.children = []; | ||
| for (var key in object) { | ||
| var child = object[key]; | ||
| var childPath = parentPath.child(key).toString(); | ||
| valueInfo.children.push(childPath); | ||
| var childName = key; | ||
| if (valueInfo.type.name === 'array') { | ||
| childName = "".concat(name, " ").concat(key); | ||
| } | ||
| buildValueTree(child, childPath, childName, valueCollection); | ||
| } | ||
| } | ||
| } | ||
| function buildStructureTree(rootObject, path, name, structureCollection) { | ||
| var heroPath = new JSONHeroPath(path); | ||
| var results = heroPath.all(rootObject); | ||
| heroPath.components[heroPath.components.length - 1] instanceof WildcardPathComponent; | ||
| var structureInfo = { | ||
| path: path, | ||
| name: name, | ||
| displayName: friendlyName(name), | ||
| type: inferType(results[0]), | ||
| children: null, | ||
| }; | ||
| structureCollection[path] = structureInfo; | ||
| results.forEach(function (result) { | ||
| if (isCollection(structureInfo.type)) { | ||
| var parentPath = new JSONHeroPath(path); | ||
| structureInfo.children = []; | ||
| if (structureInfo.type.name === 'array') { | ||
| var arrayChildPath = parentPath.child('*').toString(); | ||
| if (!structureInfo.children.includes(arrayChildPath)) { | ||
| structureInfo.children.push(arrayChildPath); | ||
| } | ||
| buildStructureTree(rootObject, arrayChildPath, name, structureCollection); | ||
| } | ||
| else { | ||
| for (var key in result) { | ||
| result[key]; | ||
| var childPath = parentPath.child(key).toString(); | ||
| structureInfo.children.push(childPath); | ||
| buildStructureTree(rootObject, childPath, key, structureCollection); | ||
| } | ||
| } | ||
| } | ||
| }); | ||
| } | ||
| function isCollection(type) { | ||
| return type.name === 'array' || type.name === 'object'; | ||
| } | ||
| export { parse }; |
+0
-0
@@ -0,0 +0,0 @@ import { JSONValueType } from '@jsonhero/json-infer-types'; |
+59
-23
@@ -1,8 +0,43 @@ | ||
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.parse = void 0; | ||
| var path_1 = require("@jsonhero/path"); | ||
| var json_infer_types_1 = require("@jsonhero/json-infer-types"); | ||
| var wildcard_path_component_1 = require("@jsonhero/path/lib/path/wildcard-path-component"); | ||
| var naming_1 = require("./naming/naming"); | ||
| 'use strict'; | ||
| Object.defineProperty(exports, '__esModule', { value: true }); | ||
| var path = require('@jsonhero/path'); | ||
| var jsonInferTypes = require('@jsonhero/json-infer-types'); | ||
| var wildcardPathComponent = require('@jsonhero/path/lib/path/wildcard-path-component'); | ||
| function friendlyName(name) { | ||
| name = snakeCase(name); | ||
| name = dashCase(name); | ||
| name = camelCase(name); | ||
| name = toTitleCase(name); | ||
| return name; | ||
| } | ||
| function camelCase(name) { | ||
| return (name | ||
| // insert a space before all caps | ||
| .replace(/(\w)([A-Z])/g, '$1 $2')); | ||
| } | ||
| function snakeCase(name) { | ||
| return (name | ||
| // insert a space before all caps | ||
| .replace(/(_)/g, ' ')); | ||
| } | ||
| function dashCase(name) { | ||
| return (name | ||
| // insert a space before all caps | ||
| .replace(/(-)/g, ' ')); | ||
| } | ||
| function toTitleCase(name) { | ||
| return (name | ||
| //after every space | ||
| .replace(/ ([a-z])/g, function (str) { | ||
| return str.toUpperCase(); | ||
| }) | ||
| //first character | ||
| .replace(/^./, function (str) { | ||
| return str.toUpperCase(); | ||
| })); | ||
| } | ||
| function parse(object) { | ||
@@ -24,16 +59,15 @@ var rootPath = '$'; | ||
| } | ||
| exports.parse = parse; | ||
| function buildValueTree(object, path, name, valueCollection) { | ||
| function buildValueTree(object, path$1, name, valueCollection) { | ||
| var valueInfo = { | ||
| path: path, | ||
| path: path$1, | ||
| name: name, | ||
| displayName: (0, naming_1.friendlyName)(name), | ||
| displayName: friendlyName(name), | ||
| value: object, | ||
| type: (0, json_infer_types_1.inferType)(object), | ||
| type: jsonInferTypes.inferType(object), | ||
| children: null, | ||
| }; | ||
| valueCollection[path] = valueInfo; | ||
| valueCollection[path$1] = valueInfo; | ||
| //for any children add to children and then recursively run this | ||
| if (isCollection(valueInfo.type)) { | ||
| var parentPath = new path_1.JSONHeroPath(path); | ||
| var parentPath = new path.JSONHeroPath(path$1); | ||
| valueInfo.children = []; | ||
@@ -52,17 +86,17 @@ for (var key in object) { | ||
| } | ||
| function buildStructureTree(rootObject, path, name, structureCollection) { | ||
| var heroPath = new path_1.JSONHeroPath(path); | ||
| function buildStructureTree(rootObject, path$1, name, structureCollection) { | ||
| var heroPath = new path.JSONHeroPath(path$1); | ||
| var results = heroPath.all(rootObject); | ||
| var isWildcard = heroPath.components[heroPath.components.length - 1] instanceof wildcard_path_component_1.WildcardPathComponent; | ||
| heroPath.components[heroPath.components.length - 1] instanceof wildcardPathComponent.WildcardPathComponent; | ||
| var structureInfo = { | ||
| path: path, | ||
| path: path$1, | ||
| name: name, | ||
| displayName: (0, naming_1.friendlyName)(name), | ||
| type: (0, json_infer_types_1.inferType)(results[0]), | ||
| displayName: friendlyName(name), | ||
| type: jsonInferTypes.inferType(results[0]), | ||
| children: null, | ||
| }; | ||
| structureCollection[path] = structureInfo; | ||
| structureCollection[path$1] = structureInfo; | ||
| results.forEach(function (result) { | ||
| if (isCollection(structureInfo.type)) { | ||
| var parentPath = new path_1.JSONHeroPath(path); | ||
| var parentPath = new path.JSONHeroPath(path$1); | ||
| structureInfo.children = []; | ||
@@ -78,3 +112,3 @@ if (structureInfo.type.name === 'array') { | ||
| for (var key in result) { | ||
| var child = result[key]; | ||
| result[key]; | ||
| var childPath = parentPath.child(key).toString(); | ||
@@ -91,1 +125,3 @@ structureInfo.children.push(childPath); | ||
| } | ||
| exports.parse = parse; |
| export declare function friendlyName(name: string): string; |
@@ -0,0 +0,0 @@ import { JSONValueType } from '@jsonhero/json-infer-types/lib/@types'; |
+23
-9
| { | ||
| "name": "@jsonhero/parser", | ||
| "version": "1.0.7", | ||
| "version": "1.0.8", | ||
| "description": "A parser that walks through a JSON file and for each value determines the path to it, the type and paths to any children. It also builds a model of the structure of the data.", | ||
| "main": "lib/index.js", | ||
| "types": "lib/index.d.ts", | ||
| "module": "./lib/index.mjs", | ||
| "types": "./lib/index.d.ts", | ||
| "type": "commonjs", | ||
| "scripts": { | ||
| "test": "jest", | ||
| "build": "tsc", | ||
| "format": "prettier --write \"src/**/*.ts\"", | ||
| "lint": "tslint -p tsconfig.json", | ||
| "clean": "rimraf lib", | ||
| "check-types": "tsc --noEmit", | ||
| "test": "jest --runInBand --coverage", | ||
| "build": "rollup -c", | ||
| "build:watch": "tsc --watch", | ||
| "prepare": "npm run build", | ||
| "prepublishOnly": "npm test && npm run lint", | ||
| "prepublishOnly": "npm run clean && npm run check-types && npm run format:check && npm run lint && npm test && npm run build", | ||
| "preversion": "npm run lint", | ||
| "version": "npm run format && git add -A src", | ||
| "postversion": "git push && git push --tags" | ||
| "postversion": "git push && git push --tags", | ||
| "lint": "eslint . --ext .ts", | ||
| "lint-and-fix": "eslint . --ext .ts --fix", | ||
| "format": "prettier --config .prettierrc 'src/**/*.ts' --write && prettier --config .prettierrc 'tests/**/*.ts' --write", | ||
| "format:check": "prettier --config .prettierrc --list-different 'src/**/*.ts'" | ||
| }, | ||
@@ -38,5 +45,12 @@ "repository": { | ||
| "devDependencies": { | ||
| "@rollup/plugin-node-resolve": "^13.1.2", | ||
| "@types/jest": "^27.0.3", | ||
| "@typescript-eslint/eslint-plugin": "^5.9.0", | ||
| "eslint": "^8.6.0", | ||
| "eslint-plugin-prettier": "^4.0.0", | ||
| "jest": "^27.3.1", | ||
| "prettier": "^2.4.1", | ||
| "prettier": "^2.5.1", | ||
| "rimraf": "^3.0.2", | ||
| "rollup": "^2.63.0", | ||
| "rollup-plugin-typescript2": "^0.31.1", | ||
| "ts-jest": "^27.0.7", | ||
@@ -43,0 +57,0 @@ "tslint": "^6.1.3", |
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.friendlyName = void 0; | ||
| function friendlyName(name) { | ||
| name = snakeCase(name); | ||
| name = dashCase(name); | ||
| name = camelCase(name); | ||
| name = toTitleCase(name); | ||
| return name; | ||
| } | ||
| exports.friendlyName = friendlyName; | ||
| function camelCase(name) { | ||
| return (name | ||
| // insert a space before all caps | ||
| .replace(/(\w)([A-Z])/g, '$1 $2')); | ||
| } | ||
| function snakeCase(name) { | ||
| return (name | ||
| // insert a space before all caps | ||
| .replace(/(_)/g, ' ')); | ||
| } | ||
| function dashCase(name) { | ||
| return (name | ||
| // insert a space before all caps | ||
| .replace(/(-)/g, ' ')); | ||
| } | ||
| function toTitleCase(name) { | ||
| return (name | ||
| //after every space | ||
| .replace(/ ([a-z])/g, function (str) { | ||
| return str.toUpperCase(); | ||
| }) | ||
| //first character | ||
| .replace(/^./, function (str) { | ||
| return str.toUpperCase(); | ||
| })); | ||
| } |
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
18625
31.73%274
65.06%14
100%8
-11.11%1
Infinity%