meta-toolkit
v1.3.0
Published
Zero-dependency toolkit for meta programming and OOP: name-casing conversions, property descriptors, aliases, prototype traversal, iterators, deep path access, option merging, and comparator adapters
Maintainers
Readme
Meta toolkit 
Meta toolkit is a no-dependency, no-nonsense collection of small utilities for meta programming and OOP that my projects accumulated over the years: generating classes, objects, prototypes, iterators, and more.
- Name mangling (
meta-toolkit/names.js) provides a conversion from compound names such asfoo-barto['foo', 'bar']and from['foo', 'bar']tofooBarand vice versa.- Used to generate file names, method names, and so on.
- The following naming schemas are supported out of box:
camelCase—toCamelCase(),fromCamelCase().PascalCase—toPascalCase(),fromPascalCase().snake_case—toSnakeCase(),fromSnakeCase().SNAKE_CASE—toAllCapsSnakeCase().
kebab-case—toKebabCase(),fromKebabCase().- whitespace-separated words —
toWords(),toTitleCase(),fromWords().
- Descriptor manipulation (
meta-toolkit/descriptors.js) — generate accessors dynamically and share them between objects/prototypes:- Create descriptors —
makeGetter(),makeSetter(),makeAccessors(). - Add descriptors —
addDescriptor(),addDescriptors(),addAccessor(),addAccessors(),addGetter(),addGetters(),addSetter(),addSetters(). - Class-prototype sugar —
addProtoDescriptor(),addProtoDescriptors(),addProtoAccessor(),addProtoAccessors(),addProtoGetter(),addProtoGetters(),addProtoSetter(),addProtoSetters(). defaultDescriptor— the default descriptor template.- Copy descriptors —
copyDescriptors().
- Create descriptors —
- Aliases (
meta-toolkit/aliases.js) — alias existing properties:- Alias properties —
addAlias(),addAliases(),addProtoAlias(),addProtoAliases().
- Alias properties —
- Prototypes (
meta-toolkit/prototypes.js) — inspect prototypes:- Iterate over prototypes —
prototypes(). getPropertyDescriptor()— similar togetOwnPropertyDescriptor(), but for all prototypes not just the current object.
- Iterate over prototypes —
- Iterators (
meta-toolkit/iterators.js) — simplify working with iterators:- Augment an iterator with an iterable interface —
augmentIterator(),normalizeIterator()(the gateway to the native iterator helpers when the runtime has them). - Lazy transformation helpers —
mapIterator(),filterIterator().
- Augment an iterator with an iterable interface —
- Path (
meta-toolkit/path.js) — work with nested objects using paths:- Get a value from a nested object by path —
get(). - Check that a path exists —
has(). - Set a value in a nested object by path —
set(),forceSet(). - Remove a value from a nested object by path —
remove().
- Get a value from a nested object by path —
- Options (
meta-toolkit/options.js) — organize options for constructors:- Copy options according to some defaults —
copyOptions().
- Copy options according to some defaults —
- Comparators (
meta-toolkit/comparators.js) — convert between different comparator function styles:- Create a compare function from a less function and vice versa —
compareFromLess(),lessFromCompare(). - Create an equality function from a less or compare function —
equalFromLess(),equalFromCompare(). - Reverse comparators —
reverseCompare(),reverseLess().
- Create a compare function from a less function and vice versa —
Full documentation is in the wiki — browse the index, or search it by name.
Examples
Generating names:
import {
toCamelCase,
toPascalCase,
toSnakeCase,
toAllCapsSnakeCase,
fromKebabCase
} from 'meta-toolkit/names.js';
const names = fromKebabCase('foo-bar-baz');
console.log(toCamelCase(names)); // fooBarBaz
console.log(toPascalCase(names)); // FooBarBaz
console.log(toSnakeCase(names)); // foo_bar_baz
console.log(toAllCapsSnakeCase(names)); // FOO_BAR_BAZAliasing properties:
import {addAliases} from 'meta-toolkit/aliases.js';
class Foo {
constructor() {
this.value = 0;
}
get double() {
return this.value * 2;
}
line(a, b) {
return a * this.value + b;
}
}
addAliases(Foo.prototype, {
double: 'x2, duplicate',
line: 'linear'
});
const f = new Foo();
f.value = 2;
console.log(f.double); // 4
console.log(f.x2); // 4
console.log(f.linear(1, 2)); // 4Object manipulation with paths:
import {set, get, remove, forceSet} from 'meta-toolkit/path.js';
const object = {};
forceSet(object, 'a.b.c', 1); // object = {a: {b: {c: 1}}}
get(object, 'a.b.c'); // 1
get(object, 'a'); // {b: {c: 1}}
set(object, 'a.b.c', 2); // object = {a: {b: {c: 2}}}
forceSet(object, 'a.b.d', 3); // object = {a: {b: {c: 2, d: 3}}}
remove(object, 'a.b.c'); // object = {a: {b: {d: 3}}}
remove(object, 'a.b'); // object = {a: {}}Note on path safety.
get(),set(),forceSet(), andremove()walk user-supplied keys without sanitizing magic property names (__proto__,constructor,prototype). If you pass externally-sourced paths (e.g., from HTTP requests) directly to these functions, validate them at your application boundary first — the library does not.
License
BSD 3-Clause "New" or "Revised" License. See the LICENSE file for details.
Release History
- 1.3.0 Completed the descriptor family: setters, accessor dictionaries, and
addProto*sugar for every installer. Addedhas()for paths,equalFromCompare(), and word-based name conversions.mapIterator()/filterIterator()are now always lazy, single-use, and close the source on early exit. - 1.2.0 Added
addProtoAlias/addProtoAliases/addProtoGetter/addProtoGetters/addGetter. Updated dev dependencies. - 1.1.9 Updated dev dependencies.
- 1.1.8 Added TS typing tests, CJS test. Fixed
.d.tstypings for strict mode. Improved docs and workflows. Updated dev dependencies. - 1.1.7 Added JSDoc to all source files. Fixed
addGetters()bug with symbol keys. Improved docs. Added AI-friendly project files. - 1.1.6 Updated dev dependencies.
- 1.1.5 Updated dev dependencies.
- 1.1.4 Updated dev dependencies.
- 1.1.3 Technical release: added
typestopackage.json. - 1.1.2 Added TS types.
- 1.1.1 Updated deps.
- 1.1.0 Added comparator utilities.
- 1.0.0 Initial release.
The full release notes are in the wiki: Release notes.
