Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

protos.js

Package Overview
Dependencies
Maintainers
1
Versions
47
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

protos.js

String and Array prototypes for a more easy life

  • 1.1.7
  • npm
  • Socket score

Version published
Weekly downloads
4
increased by33.33%
Maintainers
1
Weekly downloads
 
Created
Source

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');

['a','b','c','d'].cut(1,3);
['a','b','c','d'].cut(1,-1); // -1 means last item (like python)

// STRING MANIPULATION MADE EASY (python style)
var el = 'hello'.cut(1,3); 
var lo = 'hello'.cut(3,-1); 

> 'hello'.cut(0,4).log();
'hell'

> '3'.toInt();
3
> 'a'.toInt().isNum();
false


// 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 ]


// 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



// INTROSPECT

> var a = {b:1};
> a.dir();
{ b: 1 }

> var a = [1,2,3];
> a.dir();
[ 1, 2, 3 ]


// ASCII  chr() 

> (0x41).chr();
'A'

> '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
...


Keywords

FAQs

Package last updated on 10 Nov 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc