README
Veritas
The extension library for pragmatists with elegance.
Installation
npm install --save veritas
Basic usage with examples
First of all,
import 'veritas';
Simply,
class Something {
// blah blah blah ...
}
Something.enhance({
name: 'morrighan'
});
const instance = new Something();
for(const key of Object.keys(instance)) {
// the key 'name' must not found.
}
Furthermore,
String.enhance({
/**
* {Function} String.prototype.replaceAll
* Search and replace as far as possible.
*
* @param {RegExp|String} search
* @param {String|Function} replacement
* @return {String}
**/
replaceAll(searchValue, replaceValue) {
let [string, replaced] = [this, this.replace(searchValue, replaceValue)];
while(string !== replaced) {
[string, replaced] = [replaced, replaced.replace(searchValue, replaceValue)];
}
return string;
}
});
const string = 'Green Glass Grows Globes Greenly.';
console.log(string.replaceAll('G', 'C')); // Creen Class Crows Clobes Creenly.
Finally,
Array.enhance({
/**
* {Getter} Array.prototype.odded
* Return odded sequence.
*
* @return {Array}
**/
get odded() {
return this.map(element => element * 2 - 1);
},
/**
* {Getter} Array.prototype.zigzagged
* Return zigzagged sequence.
*
* @return {Array}
**/
get zigzagged() {
return this.map(element => Math.pow(-1, element + 1));
}
});
const array = [1, 2, 3, 4, 5];
console.log(array.odded); // [1, 3, 5, 7, 9];
console.log(array.zigzagged); // [1, -1, 1, -1, 1];
// `hasOwnProperty` checking is unnecessary by Veritas.
for(const index in array) {
if(array.hasOwnProperty(index)) {
console.log(array[index]);
}
}
// Do not worry. just do like this.
for(const element of array) {
console.log(element);
}
Ta-da! We can extend standard built-in objects and user defined objects in elegantly without worrying about unexpected exceptions within Veritas we believe.
License
Veritas is licensed under LGPL-3.0.