metafoo
A collection of APIs that let you write very DRY and meta code. Inspired by projects like Ruby on Rails and MobX.
Installation
# npm npm install metafoo # yarn yarn add metafoo
Usage
numbers
The numbers
module lets you create numbers from conveniently named variables.
Basic:
;// orconst numbers = ; const one two three = numbers; console; // 1 2 3
Supports camel, pascal and snake case:
const twoHundredAndSeven TwoHundredAndSeven two_hundred_and_seven = numbers; console; // 207console; // 207console; // 207
Also supports kebab case and dot notation:
console; // 207console; // 207
Don't worry about any spelling errors:
const tooHundretNadSvenen = numbers; console; // 207
You can go wild here:
console; // 207
Use it to generate random numbers:
console; // 9603console; // 14417
Or loop from 1 to Infinity
for let num of numbers console; // 1 ↩︎ 2 ↩︎ 3 ... Infinity
sentence
The clean way of writing sentences.
Basic:
;// orconst sentence = ; console; // Hello world.
The sentence is only converted to a string when it is called so you can extend it:
let s = sentencethisis;const isNice = true; if isNice s = snice; else s = snotnice; console; // This is nice.
Loop over all the words of a sentence:
for let word of sentencehelloworld console; // hello ↩ world
Convert the sentence to an array using the spread syntax
console; // [ 'hello', 'world' ]
Generate random sentences:
console; // Est atque vel doloribus animi nihil.console; // Delectus soluta sit ipsam placeat quas sed.
Or even an endless loop of random sentences:
for let s of sentence console; // Quae quaerat dolorem ut. ↩ Est id aut non atque. ...
safeguard
Gettin tired of TypeError: bla is not a function
and TypeError: Cannot read property bla of undefined
? Well with safeguard
that's no more!
Basic:
;// orconst safeguard = ; const obj = ; console; // bazconsole; // [Function: Safeguard]console; // [Function: Safeguard]console; // [Function: Safeguard]console; // [Function: Safeguard]
It will also work for arrays:
const arr = ;console; // 1console; // [Function: Safeguard]console; // [Function: Safeguard]
fuzzy
Sometimes you can have small spelling mistakes in your code and unlike safeguard
, where nothing will happen, fuzzy
knows what property you actually meant!
;// orconst fuzzy = ; const obj = ; console; // bar
Also works for nested objects:
const obj = ; console; // baz // setting a value is also fuzzyobjfopbat = 'bang'; console; // { foo: { bar: 'bang' } } // if you want to set a new value you'll have to use Object.defineProperty Object; console; // foo-bar
For arrays it takes the closest index instead:
const arr = ; console; // 1console; // 3console; // 1console; // 2console; // [ 2, 3 ] // here setting a value is NOT fuzzyarr3 = 4; console; // [ 1, 2, 3, 4 ]
Objects with number keys will work similar to arrays:
const nums = ; console; // Oneconsole; // Threeconsole; // PIconsole; // Four
monkey
Adds useful functions to the base data types by monkey patching their prototypes. See a list of all mokey patches here.
Basic:
;// orconst monkey = ; monkeypatch;// you can also call it if you want, doesn't make a differencemonkey; console; // falseconsole; // hello_worldconsole; // [2, 3]
You can also configure what prototypes to monkey patch:
patch; console; // falseconsole; // hello_worldconsole; // Uncaught TypeError: [1,2,3].reject is not a function ...
Instead of the constructor you can also pass in something that represents it:
patch;// orpatch;
But be careful, arrays containing items will be flattened instead
patch; console; // falseconsole; // hello_worldconsole; // Uncaught TypeError: [1,2,3].reject is not a function ...
Monkey patches
Number
isEven()
Determines whether the calling number is even
console; // false
isOdd()
Determines whether the calling number is odd
console; // true
String
inquiry()
Converts the calling string into an inquirer making equality checks prettier.
const str = 'foo';console; // trueconsole; // false
It also supports camel, pascal and snake case.
const str = 'hello world';console; // trueconsole; // trueconsole; // true
camelcase()
Converts the calling string into camel case
console; // hello-worldconsole; // hello-worldconsole; // hello-worldconsole; // hello-world
kebabcase()
Converts the calling string into kebab case
console; // hello-worldconsole; // hello-worldconsole; // hello-worldconsole; // hello-world
pascalcase()
Converts the calling string into pascal case
console; // hello-worldconsole; // hello-worldconsole; // hello-worldconsole; // hello-world
snakecase()
Converts the calling string into snake case
console; // hello-worldconsole; // hello-worldconsole; // hello-worldconsole; // hello-world
Array
each()
Alias for Array.prototype.forEach
select()
Alias for Array.prototype.map
reject()
The opposite of Array.prototype.filter
console; // [2, 3]
Limitations
Due to the limitations of ES6 proxies safeguard
and fuzzy
will not work with primitive types like Number
or String
:
const num = ;console; // 1e+0console; // Uncaught TypeError: num.toExpoventiak is not a function ... const str = ;console; // aconsole; // Uncaught TypeError: str.tuLowerVase is not a function ...