SlimIO Addon name validator/sanitizer. This package has been created to centralize validation and sanitization of addons names. So if tomorrow we decide to change the way addon name are constructed we only need to update the package everywhere.
- Node.js v12 or higher
This package is available in the Node Package Repository and can be easily installed with npm or yarn.
$ npm i @slimio/validate-addon-name
# or
$ yarn add @slimio/validate-addon-name
const assert = require("assert").strict;
const { validate, sanitize } = require("@slimio/validate-addon-name");
// Validate addon name
assert.equal(validate("Addon"), false);
assert.equal(validate("myaddon5"), true);
assert.equal(validate("1"), false);
// Sanitize addon name
assert.equal(sanitize("Addon-Name"), "addonname");
assert.equal(sanitize("??CPU-DB>>"), "cpudb");
Validate and return a boolean to tell if the name is a valid and acceptable name for a SlimIO Addon. This method does not throw if addonName is not a string (it will return false instead).
validate(10); // false
Remove non-valid (wide) characters from a given string. This method will throw a TypeError if addonName is not a string primitive.
sanitize(10); // Throw
Decamelize a string and transform it to snake_case.
decamelize("sayHello"); // say_hello
The package export a freezed CONSTANTS object that contain a VALIDATE_REGEX key.
const { validate, CONSTANTS } = require("@slimio/validate-addon-name");
const addonName = "Addon";
if (!validate(addonName)) {
console.log(`addon '${addonName}' not matching expected regex ${CONSTANTS.VALIDATE_REGEX}`)
}
This project have no dependencies.
MIT