diff --git a/nanoid.hjson b/nanoid.hjson new file mode 100644 index 0000000..3d8987f --- /dev/null +++ b/nanoid.hjson @@ -0,0 +1,62 @@ +[ + { + type: 'nanoid', + module : 'nanoid', + action : 'nanoid', + groupTitle : 'Nanoid', + groupIcon : 'fal fa-lg fal fa-barcode comp-data', + title : 'Nano ID', + icon : 'fal fa-lg fa-barcode comp-data', + dataPickObject: true, + properties : [ + { + group: 'Nano ID options', + variables: [ + { name: 'name', optionName: 'name', title: 'Name', type: 'text', required: true, defaultValue: 'NanoID', help: 'This will be the name for the output key and the tag used in the expression builder'}, + { name: 'api', optionName: 'api', title: 'Method', + type: 'droplist', + values: [ + {title: 'Secure', value: 'secure', show:[], hide:['alphabet'] }, + {title: 'Non-Secure', value: 'nonsecure', show:[], hide:['alphabet'] }, + {title: 'Custom Alphabet Secure', value: 'custom_secure', show:['alphabet'], hide:[]}, + {title: 'Custom Alphabet Non-Secure', value: 'custom_nonsecure', show:['alphabet'], hide:[]} + ], defaultValue: '', + help: 'Choose the method to generate Nano ID. Non-Secure ones prioritize performance over security and low collision probability' + ,} + { name: 'size', optionName:'size', title: 'ID Size', type: 'number', required: true, defaultValue :'21', help: 'The size of the NanoID generated. The smaller it is the increased probability of collision. Default: 21'}, + { name: 'alphabet', optionName:'alphabet', title: 'Alphabet', type: 'text', required: false, defaultValue :'', help: 'Add the string of characters you want to use as your custom alphabet. The smaller it is the increased probability of collision.'}, + { name: 'nanoidOutput', optionName: 'output', title: 'Output', type: 'boolean', defaultValue: false } + ] + } + ] + }, + { + type: 'ananoid', + module : 'nanoid', + action : 'ananoid', + groupTitle : 'Nanoid', + groupIcon : 'fal fa-lg fal fa-barcode comp-data', + title : 'Nano ID Asynchronous', + icon : 'fal fa-lg fa-barcode-scan comp-data', + dataPickObject: true, + properties : [ + { + group: 'Nano ID Async options', + variables: [ + { name: 'name', optionName: 'name', title: 'Name', type: 'text', required: true, defaultValue: 'NanoID', help: 'This will be the name for the output key and the tag used in the expression builder'}, + { name: 'api', optionName: 'api', title: 'Method', + type: 'droplist', + values: [ + {title: 'Secure', value: 'secure', show:[], hide:['alphabet'] }, + {title: 'Custom Alphabet Secure', value: 'custom_secure', show:['alphabet'], hide:[]}, + ], defaultValue: '', + help: 'Choose the method to generate Nano ID.' + ,} + { name: 'size', optionName:'size', title: 'ID Size', type: 'number', required: true, defaultValue :'21', help: 'The size of the NanoID generated. The smaller it is the increased probability of collision. Default: 21'}, + { name: 'alphabet', optionName:'alphabet', title: 'Alphabet', type: 'text', required: false, defaultValue :'', help: 'Add the string of characters you want to use as your custom alphabet. The smaller it is the increased probability of collision.'}, + { name: 'nanoidOutput', optionName: 'output', title: 'Output', type: 'boolean', defaultValue: false } + ] + } + ] + } +] \ No newline at end of file diff --git a/nanoid.js b/nanoid.js new file mode 100644 index 0000000..bd11526 --- /dev/null +++ b/nanoid.js @@ -0,0 +1,28 @@ +const { nanoid: s, customAlphabet: custom_secure } = require('nanoid') +const { nanoid: ns, customAlphabet: custom_nonsecure } = require('nanoid/non-secure'); +const { nanoid: as, customAlphabet: acustom_secure } = require('nanoid/async') + +exports.nanoid = function (options) { + switch (options.api) { + case 'secure': + return s(Number(options.size) || 21); + case 'nonsecure': + return ns(Number(options.size) || 21); + case 'custom_secure': + cs = custom_secure(this.parseRequired(options.alphabet ? String(options.alphabet) : null, 'string', 'parameter alphabet is required.'), Number(options.size) || 21) + return cs(); + case 'custom_nonsecure': + cns = custom_nonsecure(this.parseRequired(options.alphabet ? String(options.alphabet) : null, 'string', 'parameter alphabet is required.'), Number(options.size) || 21) + return cns(); + } +}; + +exports.ananoid = async function (options) { + switch (options.api) { + case 'secure': + return await as(Number(options.size) || 21); + case 'custom_secure': + acs = acustom_secure(this.parseRequired(options.alphabet ? String(options.alphabet) : null, 'string', 'parameter alphabet is required.'), Number(options.size) || 21) + return await acs(); + } +}; \ No newline at end of file