diff --git a/assets/vendor/json-to-jsdoc.js b/assets/vendor/json-to-jsdoc.js new file mode 100644 index 0000000..453dc50 --- /dev/null +++ b/assets/vendor/json-to-jsdoc.js @@ -0,0 +1,202 @@ +function checkIfNeededToAddUndefinedType(obj, objectKey) { + const temporaryObject = {}; + let maxLenghtOfObject = 0; + + Object.keys(obj).forEach(currentKey => { + // check if it's our Object, + // like awards.editions in awards.editions.outcome + if (~currentKey.indexOf(objectKey) && currentKey !== objectKey) { + temporaryObject[currentKey] = obj[currentKey]; + } + }); + + // We need to check the local maximum + Object.keys(temporaryObject).forEach(key => { + const currentKeyLength = temporaryObject[key].length; + + if (currentKeyLength > maxLenghtOfObject) + maxLenghtOfObject = currentKeyLength; + }); + + // If the maximum is bigger than the current value, + // but only one child in, we can also have an undefined. + Object.keys(temporaryObject).forEach(key => { + if (key.split(".").length < 2) { + if (maxLenghtOfObject > temporaryObject[key].length) { + this[key].push("undefined"); + } + } + }); +} + +function keepUniqueTypes(obj, jsonObjects) { + const temporaryObject = obj; + const remainingObject = {}; + const numberOfJSONDefinitions = jsonObjects.length; + + Object.keys(obj).forEach(objectKey => { + if (!~objectKey.indexOf(".")) { + if (obj[objectKey].length < numberOfJSONDefinitions) { + temporaryObject[objectKey].push("undefined"); + } + } + + obj[objectKey].forEach(anyValue => { + if (anyValue === "object" || anyValue === "object[]") { + checkIfNeededToAddUndefinedType.bind(temporaryObject, obj, objectKey)(); + } + }); + }); + + Object.keys(temporaryObject).forEach(key => { + remainingObject[key] = getUnique(temporaryObject[key]); + }); + + return remainingObject; +} + +/** + * @param {Array} array - The array that needs to be ded-duplicated + * @returns {Array} + */ +function getUnique(array) { + const u = {}; + const a = []; + for (let i = 0, l = array.length; i < l; ++i) { + if (u.hasOwnProperty(array[i])) continue; + a.push(array[i]); + u[array[i]] = 1; + } + return a; +} + +/** + * @param {*} value + * @returns {string} currentType - lowerCased type of value + */ +function getTypeOfValue(value) { + let currentType = Object.prototype.toString + .call(value) + .split(" ")[1] + .slice(0, -1) + .toLowerCase(); + + if (!currentType) currentType = "*"; + + return currentType[0].toUpperCase() + currentType.substr(1); +} +/** + * @param {Array} array - The array that we want to parse + * @param {string} objectName - Usually the Prefix - Root do no have ObjectName + */ +function parseArray(array, objectName) { + const currentArrayTypes = []; + const prefix = objectName ? `${objectName}` : ""; + + array.forEach(currentValue => { + currentArrayTypes.push(`${getTypeOfValue(currentValue)}[]`); + }); + + if (!(objectName in this)) this[objectName] = []; + this[objectName].push(getUnique(currentArrayTypes).join("|")); + + array.forEach(currentValue => { + const currentValueType = getTypeOfValue(currentValue); + + if (currentValueType === "array") { + parseArray.bind(this, currentValue, prefix)(); + } else if (currentValueType === "object") { + parseObject.bind(this, currentValue, prefix, true)(); + } + }); +} +/** + * @param {object} obj - The object that we want to parse + * @param {string} objectName - Usually the Prefix - Root do no have ObjectName + * @param {boolean} doNotReinsert - If we want to reinsert the type, usually when we parse + * an array of objects, we do not want to reinsert. + */ +function parseObject(obj, objectName, doNotReinsert = false) { + const prefix = objectName ? `${objectName}.` : ""; + + Object.keys(obj).forEach(propertyName => { + const currentValue = obj[propertyName]; + const propertyType = getTypeOfValue(currentValue); + const currentPrefix = `${prefix}${propertyName}`; + let result = null; + + // If it's Array, we need the values inside. + if (propertyType === "array") + parseArray.bind(this, currentValue, currentPrefix)(); + else if (propertyType === "object") + parseObject.bind(this, currentValue, currentPrefix)(); + else { + result = propertyType; + if (!(currentPrefix in this)) this[currentPrefix] = []; + this[currentPrefix].push(result); + } + }); + // Root Object don't have a objectName + if (!doNotReinsert && objectName) { + if (!(objectName in this)) this[objectName] = []; + this[objectName].push("object"); + } +} + +function ParseRootDefinition(obj) { + // 1. What is the default type for our Definition? + const jsonType = typeof obj; + + if (jsonType === "array") parseArray.bind(this, obj)(); + else if (jsonType === "object") parseObject.bind(this, obj)(); +} + +/** + * @param {Object} obj + * @param {Object[]} jsonObjects + */ +function printTheTypeDef(obj, jsonObjects) { + let output = ""; + // 0. A default name for our Definition + const jsonDef = "json"; + const jsonTypes = []; + + Object.keys(jsonObjects).forEach(key => { + jsonTypes.push(getTypeOfValue(jsonObjects[key])); + }); + + output = `/** @typedef {${getUnique(jsonTypes)}} ${jsonDef} \n`; + + Object.keys(obj) + .sort() + .forEach(key => { + output += `* @property {${obj[key].join("|")}} ${key} \n`; + }); + + output += "*/"; + + return output; +} + +/** + * @param {Array} jsonObjects + */ +function goThroughAndParse(jsonObjects) { + const theObjectDefinition = {}; + + jsonObjects.forEach(obj => + ParseRootDefinition.bind(theObjectDefinition, obj)() + ); + + return printTheTypeDef( + keepUniqueTypes(theObjectDefinition, jsonObjects), + jsonObjects + ); +} + +export function convert(json) { + const jsonObjects = []; + + jsonObjects.push(JSON.parse(json)); + return goThroughAndParse(jsonObjects); +} diff --git a/pages/json-to-jsdoc.tsx b/pages/json-to-jsdoc.tsx new file mode 100644 index 0000000..dd9f328 --- /dev/null +++ b/pages/json-to-jsdoc.tsx @@ -0,0 +1,20 @@ +import ConversionPanel, { Transformer } from "@components/ConversionPanel"; +import * as React from "react"; +import { useCallback } from "react"; +import { convert } from "@assets/vendor/json-to-jsdoc"; + +export default function() { + const transformer = useCallback(async ({ value }) => { + return convert(value); + }, []); + + return ( + + ); +} diff --git a/utils/routes.tsx b/utils/routes.tsx index 6f08a7b..4110b34 100644 --- a/utils/routes.tsx +++ b/utils/routes.tsx @@ -107,6 +107,10 @@ export const categorizedRoutes = [ path: "/json-to-yaml", packageName: "json2yaml", packageUrl: "https://github.com/jeffsu/json2yaml" + }, + { + label: "to JSDoc", + path: "/json-to-jsdoc" } ] },