New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

derive-ts

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

derive-ts - npm Package Compare versions

Comparing version 1.1.1 to 1.1.2

63

helpers.js

@@ -6,2 +6,48 @@ // @ts-check

/**
* Derive a TypeScript interface from a JavaScript object
* @param {Record<string, unknown>} object
* @param {string} interfaceName Name for the generated interface
* @param {boolean} formatCode If the code should be prettified or not
* @returns {string}
*/
const deriveInterfaceFromObject = (
object,
interfaceName = 'MyInterface',
formatCode = true,
subInterfaces = false,
) => {
const interfaces = {};
const interfaceCode = identifyType(object, null, subInterfaces ? interfaces : null);
const fullInterface = `export interface ${interfaceName} ${interfaceCode}`;
let allCode = null;
if (subInterfaces) {
allCode =
`
${Object.keys(interfaces)
.map(
(key) => `
export interface ${key} {
${interfaces[key]}
}
`,
)
.join('\n')}
` + fullInterface;
} else {
allCode = fullInterface;
}
if (!formatCode) {
return allCode;
}
try {
return prettifyCode(allCode);
} catch (error) {
console.error('Error formatting code. Trying to save anyways', error);
}
};
/**
* Convert a string to Pascal Case (TitleCase)

@@ -18,3 +64,3 @@ * @param {string} str String to convert to Pascal case

* @param {string | number | boolean | unknown[] | Record<string, unknown> | unknown} value
* @param {*} key
* @param {string} key
* @param {*} interfaces

@@ -35,3 +81,13 @@ * @returns

return `${identifyType(value[0], key, interfaces)}[]`;
const types = {};
value.forEach((v) => {
const type = identifyType(v, key, interfaces);
types[type] = true;
});
const typesArr = Object.keys(types);
const type = typesArr.length > 1 ? `(${typesArr.join('|')})` : typesArr[0];
return `${type}[]`;
} else if (value === undefined || value === null) {

@@ -44,3 +100,3 @@ return 'unknown';

.map(([key, subValue]) => `${key}: ${identifyType(subValue, key, interfaces)}`)
.join(';\n');
.join(';');

@@ -70,2 +126,3 @@ if (interfaces && key) {

module.exports = {
deriveInterfaceFromObject,
pascalCase,

@@ -72,0 +129,0 @@ identifyType,

55

index.js

@@ -6,54 +6,4 @@ #!/usr/bin/env node

const packageJson = require('./package.json');
const { identifyType, prettifyCode } = require('./helpers');
const { deriveInterfaceFromObject } = require('./helpers');
/**
* Derive a TypeScript interface from a JavaScript object
* @param {Record<string, unknown>} object
* @param {string} interfaceName Name for the generated interface
* @param {boolean} formatCode If the code should be prettified or not
* @returns {Promise<string>}
*/
const deriveInterfaceFromObject = async (
object,
interfaceName = 'MyInterface',
formatCode = true,
subInterfaces = false,
) => {
const interfaces = {};
const interfaceCode = await identifyType(object, null, subInterfaces ? interfaces : null);
const fullInterface = `export interface ${interfaceName} ${interfaceCode}`;
let allCode = null;
if (subInterfaces) {
allCode =
`
${Object.keys(interfaces)
.map(
(key) => `
export interface ${key} {
${interfaces[key]}
}
`,
)
.join('\n')}
` + fullInterface;
} else {
allCode = fullInterface;
}
if (!formatCode) {
return allCode;
}
let formattedCode = allCode;
try {
formattedCode = prettifyCode(allCode);
} catch (error) {
console.error('Error formatting code. Trying to save anyways', error);
}
return formattedCode;
};
if (require.main === module) {

@@ -90,3 +40,3 @@ const program = new Command();

const formattedCode = await deriveInterfaceFromObject(
const formattedCode = deriveInterfaceFromObject(
objectToDeriveFrom,

@@ -112,3 +62,2 @@ interfaceName,

deriveInterfaceFromObject,
prettifyCode,
};
{
"name": "derive-ts",
"version": "1.1.1",
"version": "1.1.2",
"author": "Jake Cyr",

@@ -10,13 +10,14 @@ "license": "ISC",

},
"description": "Simple library to derive TypeScript interfaces from JS object examples",
"description": "Simple library to derive TypeScript interfaces from JS object / JSON examples",
"main": "index.js",
"bin": "index.js",
"dependencies": {
"commander": "^8.1.0",
"fs-extra": "^10.0.0",
"commander": "^9.3.0",
"fs-extra": "^10.1.0",
"lodash": "^4.17.21",
"prettier": "^2.3.2"
"prettier": "^2.7.0"
},
"scripts": {
"start": "node index.js"
"start": "node index.js",
"test": "jest"
},

@@ -27,4 +28,13 @@ "keywords": [

"type",
"derive"
]
"derive",
"json",
"javascript"
],
"devDependencies": {
"@types/fs-extra": "^9.0.13",
"@types/jest": "^28.1.1",
"@types/lodash": "^4.14.182",
"@types/prettier": "^2.6.3",
"jest": "^28.1.1"
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc