Skip to content

Instantly share code, notes, and snippets.

@vsemozhetbyt
Last active January 22, 2019 18:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vsemozhetbyt/2893a240dd4d86fd871cecd46a577c4e to your computer and use it in GitHub Desktop.
Save vsemozhetbyt/2893a240dd4d86fd871cecd46a577c4e to your computer and use it in GitHub Desktop.
JavaScript Nomenclature
/******************************************************************************/
'use strict';
/******************************************************************************/
const nomenclatureTerms = new Set();
const nomenclatureChains = new Set();
const globs = new Map();
const processedObjects = new Set();
/******************************************************************************/
if (typeof window !== 'undefined') {
globs.set('window', window);
} else {
globs.set('global', global);
globs.set('module', module);
globs.set('require', require);
require('repl')._builtinLibs.concat('timers', 'module').forEach(libName => {
globs.set(`require('${libName}')`, require(libName));
});
['__dirname', '__filename'].forEach(name => {
nomenclatureTerms.add(name);
nomenclatureChains.add(name);
});
}
/******************************************************************************/
globs.forEach((value, key) => { processKeys(key, value, false); });
ouput(nomenclatureTerms, 'terms');
ouput(nomenclatureChains, 'chains');
console.log(`
Terms: ${nomenclatureTerms.size.toLocaleString()}
Chains: ${nomenclatureChains.size.toLocaleString()}
`);
/******************************************************************************/
function processKeys(name, obj, hasParent) {
if (!hasParent) {
nomenclatureTerms.add(name);
nomenclatureChains.add(name);
}
processedObjects.add(obj);
Reflect.ownKeys(obj).forEach(key => {
let depthName;
if (typeof key === 'symbol') {
nomenclatureTerms.add(key.toString());
depthName = `${name}[${JSON.stringify(key.toString())}]`;
} else {
nomenclatureTerms.add(key);
depthName = `${name}[${JSON.stringify(key)}]`;
}
nomenclatureChains.add(depthName);
try {
const value = obj[key];
if ((value instanceof Object ||
typeof value === 'object' && value !== null
) &&
!processedObjects.has(value)
) {
processKeys(depthName, value, true);
}
} catch (err) {
console.error(err.message);
}
});
}
/******************************************************************************/
function ouput(data, type) {
const dataOutput = `${
[...data]
.sort((a, b) => a.localeCompare(b, 'en', { numeric: true, caseFirst: 'upper' }))
.join('\n')
}\n`;
if (typeof window !== 'undefined') {
const textarea = document.body.appendChild(document.createElement('textarea'));
textarea.cols = 80;
textarea.rows = 10;
textarea.value = `js.nomenclature.web.${type}.txt\n${dataOutput}`;
document.body.appendChild(document.createElement('br'));
} else {
require('fs').writeFileSync(
`js.nomenclature.node.${type}.txt`, dataOutput, 'utf8'
);
}
}
/******************************************************************************/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment