protos.js

1.5.0 • Public • Published

Javascript easy string manipulation, file access, ascii conversions, statistics, json, and more ..

by @sha0coder

 
require('protos.js');
 
 
 
var users = [];
 
users.load('users.txt');  // arrays can load data
 
if (!users.has('Peter')) { // arrays can check content
    users.push('Peter')
    users.save('users.txt') // arrays can save data
}
 
if ('Peter'.in(users)) {   // strings can also check if exists in arrays
    'Peter is here'.log(); // strings can console.log
    'some error'.err();   // same as console.error()
    'John'.appendFile('users.txt') // strings can append to file
}
 
var str = 'test';
str.log(); // same as console.log(str);
str.save('file.txt');
str.save('file.txt', function() {
   'async saved!'.log();
});
 
var str = ''.load('data.txt');
 
'myfolder'.mkdir();
 
 
> ['a','b','c','d'].cut(1,3);
[ 'b', 'c' ]
 
> ['a','b','c','d'].cut(0,-1); // -1 means last item
[ 'a', 'b', 'c', 'd' ]
 
 
> ['a','b','c','d'].cut(1,-1);
[ 'b', 'c', 'd' ]
 
> ['a','b','c','d'].cut(-2);
'c'
 
// walking objects, but only (hasOwnProperty)
> obj.each(function(name, obj) { ...
 
 
 
// STRING MANIPULATION MADE EASY (python style)
> 'hello'.cut(0)
'h'
 
> 'hello'.cut(-1)
'o'
 
> 'hello'.cut(0,-1)
'hello'
 
> 'hello'.cut(-5,-1)
'hello'
 
> 'hello'.cut(-2,-1)
'lo'
 
> '<td>prize:</td><td>$100</td>'.between('<td>','</td>')
['prize:','$100']
 
> 'this is a test'.getChr('s');
[3,6,12]
 
> 'test is a test'.getStr('test');
[0,10]
 
 
'I have {0} years old'.fmt(age).log();
 
 
> 'hello'.cut(0,4).log();
'hell'
 
> 'A'.repeat(3);
AAA
 
> '3'.toInt();
3
> '3.3'.toFloat();
3.3
> 'a'.toInt().isNum();
false
 
> 'john'.capitalize()
'John'
 
 
 
// JSON FILES
 
var struct = {}.load('file.json');  // load json files into js object
struct.save('file.json');           // save js object into json file
struct.save('file.json', function() { // save async
   'saved'.log();
});
struct.dir()  // same as console.dir(struct);
 
 
// FUZZY HASHING thanks to ctph.js
 
'hello'.compare('hell')     // 50%
'hello'.compare('hello')    // 100%
'hello'.compare('abcd')     // 0%
 
var hash = 'hello'.fuzzyHash();
 
// calling a function on all items
objects.call('method',[params]);
 
 
 
// ARRAY USEFUL METHODS
 
> ['cats','dogs','birds'].choice()
'dogs'
 
> [3, 1, 2, 3].unique()
[ 3, 1, 2 ]
 
> [1,2,3,4].common([5,6,1,2]);
[ 1, 2 ]
 
> [1,2,3,4].notIn([9,3,5,1,2]);
[ 4 ]
 
> [1,2,3].has(4)
false
 
 
> [ 'hola', 'cara', 'cola' ].grep('la');
[ 'hola', 'cola' ]
 
> [ 'hola', 'cara', 'cola' ].vgrep('la');
[ 'cara' ]
 
> var n = [1,2,3,4,5];
> n.smap(function(n) { return n*2; });
[ 2, 4, 6, 8, 10 ]
 
 
// SHELL EXECUTION
 
'ls'.exec(function(stdout,stderr) {
    stdout.log();
    stderr.err();
});
 
// STATISTICS
 
 
> [1,3,2].max()
3
 
> [1,3,2].min()
1
 
> [1,3,2].average()
2
 
> var n = [];
> n.fillNums(1,10);
> n.cut(0,1)
[ 1 ]
 
> n.cut(-3,-1) // -1 means last item
[ 9, 10 ]
 
> var n = 0/0;
> n.isNum();
false
 
>var n=1;
>n.in([1,2,3]);
true
 
>(1).in([1,2,3]);
true
 
 
 
// INTROSPECT
 
> var a = {b:1};
> a.dir();
{ b: 1 }
 
> var a = [1,2,3];
> a.dir();
[ 1, 2, 3 ]
 
 
// ASCII  chr()
 
> (0x41).chr();
'A'
 
> 'a'.chr()
97
 
> 'HELLO'.chr();
[ 72, 69, 76, 76, 79 ]
 
> [72,69,76,76,79].chr()
'HELLO'
 
 
 
// ITERATION knowing the index
 
['dog','cat','bird'].each(function(i,pet) {
   pet.log();
});
 
 
// TYPES
 
x.isNumber();
x.isObject();
x.isArray();
x.isString();
x.isBoolean();
 
 
 
 
// BOOLEANS callback based conditions
 
 
(cmd == 'install').call(doInstall);    // boleans can call if true
 
(1 == 2).call(ghostbusters);
 
true.call(function() {
    'always here'.log();
});
 
false.call(ghostbusters)
 
 
'help'.in(process.args).call(doHelp);
 
 
 
// SCHEDULER
 
> function sayHello() { 'hello'.log(); }
> sayHello.sched(1000) // function will be launched in a setTimeout until return true;
 
hello
hello
hello
hello
...
 
 
// ALL THE PROTOTYPES:
 
Object.prototype.load = function(filename)
Object.prototype.save = function(filename, cb)
Object.prototype.each = function(cb)
Object.prototype.dir = function()
Object.prototype.has = function(name)
Object.prototype.isNumeric = function()
Object.prototype.isBoolean = function()
Object.prototype.isArray = function()
Object.prototype.isString = function()
Object.prototype.isObject = function()
Object.prototype.isDate = function()
Object.prototype.clone = function()
 
Array.prototype.dir = function()
Array.prototype.log = function()
Array.prototype.choice = function()
Array.prototype.removeId = function(id)
Array.prototype.grep = function(kw)
Array.prototype.vgrep = function(kw)
Array.prototype.hasSimilar = function(str, min_ratio)
Array.prototype.unique = function()
Array.prototype.load = function(filename)
Array.prototype.save = function(filename)
Array.prototype.clear = function()
Array.prototype.sched = function(millis, cb)
Array.prototype.max = function()
Array.prototype.min = function()
Array.prototype.average = function()
Array.prototype.smap = function(fn)
Array.prototype.has = function(item)
Array.prototype.cut = function(b,e)
Array.prototype.call = function(params)
Array.prototype.fillNums = function(b,e)
Array.prototype.chr = function()
Array.prototype.each = function(cb)
Array.prototype.common = function(arr)
Array.prototype.notIn = function(arr)
Array.prototype.isNumeric = function()
Array.prototype.isBoolean = function()
Array.prototype.isArray = function()
Array.prototype.isString = function()
Array.prototype.isObject = function()
Array.prototype.isDate = function()
Array.prototype.clone = function()
 
String.prototype.exec = function(cb)
String.prototype.strip = function()
String.prototype.fuzzyHash = function()
String.prototype.log = function()
String.prototype.err = function()
String.prototype.compare = function(str)
String.prototype.capitalize = function()
String.prototype.fmt = function()
String.prototype.load = function(filename, cb)
String.prototype.save = function(filename,cb)
String.prototype.mkdir = function()
String.prototype.appendFile = function(filename)
String.prototype.toInt = function()
String.prototype.toFloat = function()
String.prototype.in = function(arr)
String.prototype.cut = function(b,e)
String.prototype.contains = function(str)
String.prototype.getChr = function()
String.prototype.getStr = function()
String.prototype.between = function()
 
String.prototype.findChar = function(c) {
String.prototype.isNumeric = function()
String.prototype.isBoolean = function()
String.prototype.isArray = function()
String.prototype.isString = function()
String.prototype.isObject = function()
String.prototype.isDate = function()
 
Number.prototype.in = function(arr)
Number.prototype.isNaN = function()
Number.prototype.isNum = function()
Number.prototype.log = function()
Number.prototype.chr = function()
 
Boolean.prototype.call = function(cb)
Boolean.prototype.isNumeric = function()
Boolean.prototype.isBoolean = function()
Boolean.prototype.isArray = function()
Boolean.prototype.isString = function()
Boolean.prototype.isObject = function()
Boolean.prototype.isDate = function()
 
Number.prototype.isNumber = function()
Number.prototype.isBoolean = function()
Number.prototype.isArray = function()
Number.prototype.isString = function()
Number.prototype.isObject = function()
Number.prototype.isDate = function()
 
Date.prototype.isNumeric = function()
Date.prototype.isBoolean = function()
Date.prototype.isArray = function()
Date.prototype.isString = function()
Date.prototype.isObject = function()
Date.prototype.isDate = function()
 
Function.prototype.sched = function(millis)
Function.prototype.clone = function()
 

Package Sidebar

Install

npm i protos.js

Weekly Downloads

1

Version

1.5.0

License

GPL-3.0

Unpacked Size

29.8 kB

Total Files

5

Last publish

Collaborators

  • sha0