Skip to content
Eugene Lazutkin edited this page Aug 15, 2024 · 6 revisions

This module contains a collection of utilities used to transform names.

names.js

Utilities are usually prefixed with to and from. from utilities take a name in some format (defined by the utility name) and return an array of names. to utilities take an array of names and return a name in some format (defined by the utility name).

Legend for tables
  • API
    • name — a name as a string.
    • names — an array of name components as strings

The following utilities are available:

Function Return value Description
toCamelCase(names) name Converts an array of names to camelCase.
fromCamelCase(name) names Converts a camelCase name to an array of names.
toPascalCase(names) name Converts an array of names to PascalCase.
fromPascalCase(name) names Converts a PascalCase name to an array of names.
toAllCapsSnakeCase(names) name Converts an array of names to SNAKE_CASE.
toSnakeCase(names) name Converts an array of names to snake_case.
fromSnakeCase(name) names Converts a snake_case name to an array of names.
toKebabCase(names) name Converts an array of names to kebab-case.
fromKebabCase(name) names Converts a kebab-case name to an array of names.
capitalize(name) name Capitalizes a name: makes the first letter uppercase.

Examples

import {toPascalCase, fromSnakeCase} from 'meta-toolkit/names.js';

const defaultColors = {RED: 0xff0000, DARK_RED: 0x800000, LIGHT_RED: 0xff8080};

class Color {
  constructor(hex = 0) { this.hex = hex; }
}

for (const [name, value] of Object.entries(defaultColors)) {
  Color.prototype['set' + toPascalCase(fromSnakeCase(name)] = function () {
    this.hex = value;
    return this;
  };
}

const color = new Color().setDarkRed();

Exports

All functions are exported by names. There is no default export.

Clone this wiki locally