@slimio/validate-addon-name

SlimIO Addon name validator/sanitizer

Usage no npm install needed!

<script type="module">
  import slimioValidateAddonName from 'https://cdn.skypack.dev/@slimio/validate-addon-name';
</script>

README

Validate-addon-name

version Maintenance MIT dep size Known Vulnerabilities Build Status Greenkeeper badge

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.

Requirements

Getting Started

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

Usage example

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");

API

validate(addonName: string): boolean

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

sanitize(addonName: string): string

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(str: string): string

Decamelize a string and transform it to snake_case.

decamelize("sayHello"); // say_hello

CONSTANTS

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}`)
}

Dependencies

This project have no dependencies.

License

MIT