Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/uhop/meta-toolkit
Toolkit to help with meta programming and OOP
https://github.com/uhop/meta-toolkit
Last synced: 1 day ago
JSON representation
Toolkit to help with meta programming and OOP
- Host: GitHub
- URL: https://github.com/uhop/meta-toolkit
- Owner: uhop
- License: bsd-3-clause
- Created: 2024-08-11T22:15:30.000Z (about 2 months ago)
- Default Branch: main
- Last Pushed: 2024-09-26T14:02:06.000Z (7 days ago)
- Last Synced: 2024-09-26T14:09:42.280Z (7 days ago)
- Language: JavaScript
- Size: 29.3 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# Meta toolkit [![NPM version][npm-img]][npm-url]
[npm-img]: https://img.shields.io/npm/v/meta-toolkit.svg
[npm-url]: https://npmjs.org/package/meta-toolkitMeta toolkit is a toolkit to help with meta programming and OOP. It is a no-dependency,
no-nonsense collection of small bits my projects accumulated over the years. They deal with
helping to generate classes, objects, prototypes, iterators, and more.* Name mangling (`meta-toolkit/names.js`) provides a conversion from compound names
such as `foo-bar` to `['foo', 'bar']` and from `['foo', 'bar']` to `fooBar` and vice versa.
* Used to generated 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()`.
* Descriptor manipulation (`meta-toolkit/descriptors.js`) provides a set of helpers used to generate
various accessors dynamically and share them between different objects/prototypes:
* Create descriptors — `makeGetter()`, `makeSetter()`, `makeAccessors()`.
* Add descriptors — `addDescriptor()`, `addDescriptors()`, `addAccessor()`, `addGetters()`.
* Copy descriptors — `copyDescriptors()`.
* Aliases (`meta-toolkit/aliases.js`) provides a set of helpers to alias existing properties:
* Alias properties — `addAlias()`, `addAliases()`.
* Prototypes (`meta-toolkit/prototypes.js`) provides a set of helpers to inspect prototypes:
* Iterate over prototypes — `prototypes()`.
* `getPropertyDescriptor()` — similar to `getOwnPropertyDescriptor()`, but for all prototypes
not just the current object.
* Iterators (`meta-toolkit/iterators.js`) provides a set of helpers simplify creating custom iterators:
* Augment iterable with an iterator interface — `augmentIterator()`, `normalizeIterator()`.
* Add array-like methods if not present — `mapIterator()`, `filterIterator()`.
* Path (`meta-toolkit/path.js`) provides a set of helpers to work with embedded objects in a simple way:
* Get value from a nested object by path — `get()`.
* Set value in a nested object by path — `set()`.
* Options (`meta-toolkit/options.js`) provides a set of helpers to organize options for constructors:
* Copy options according to some defaults — `copyOptions()`.
* Comparators (`meta-toolkit/comparators.js`) provides a set of helpers to convert between different
types of comparator functions:
* Create a compare function from a less function and vice versa — `compareFromLess()`,
`lessFromCompare()`.
* Create an equality function from a less function — `equalFromLess()`.
* Reverse comparators — `reverseCompare()`, `reverseLess()`.See the full documentation in the [wiki](https://github.com/uhop/meta-toolkit/wiki).
## Examples
Generating names:
```js
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_BAZ
```Aliasing properties:
```js
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(2);
console.log(f.double); // 4
console.log(f.x2); // 4
console.log(f.linear(1, 2)); // 4
```Object manipulation with paths:
```js
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}}}
set(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: {}}
```## License
BSD 3-Clause "New" or "Revised" License. See the LICENSE file for details.
## Release History
* 1.1.1 *Updated deps.*
* 1.1.0 *Added comparator utilities.*
* 1.0.0 *Initial release.*