node-mst
A general-purpose JavaScript library inspired by LINQ and Matlab, and named after MST3K.
prototype
Array and Object Traversal
Non-The library provides in-order traversal functions like each
, select
, and where
that work on both Array
s and Object
s, without altering their prototype
s:
For arrays, the kernel function accepts arguments of (element, index, array)
, since having an index is often un-necessary:
var $$ = ; $$; // 1 // 2 // 3 $$; // 0: -1 // 1: -2 // 2: -3
each
each
will visit all items in order, will break on a return true;
, and return the last value of the iterator variable. It works for objects, too, and the kernel function is optional, so a fast way to find out the number of key–value pairs in an object is this:
var $$ = ; var obj = foo: 1 bar: 4 moo: 9 ; console; // 3 console; // 1
select
select
will visit each element in the array/object and return a new array/object composed of the return values from the kernel. undefined
returns are ignored:
var $$ = ; var arr = 1 2 3 4; console; // [-1, -4, -9, -16]; var obj = one: 1 two: 2 three: 3 four: 4 ; console; // { '-one*one': -1, // '-two*two': -4, // '-three*three': -9, // '-four*four': -16 }
where
where
will return a new array/object where only the elements that returned true
from the kernel will be retained:
var $$ = ; var arr = 1 -1 2 -2 3 -3; console; // [-1, -2, -3]
Easy Utility Functions
Test whether any two objects are deeply equal with equal
:
var $$ = ; var obj1 = foo: bar: 1 moo: 1 2 3 zoo: 16 obj2 = zoo: 16 foo: bar: 1 moo: 1 2 3 ; console; // true
Perform a deep copy of any object using clone
:
var $$ = ; var obj = foo: bar: 1 moo: 1 2 3 zoo: 16 ; var clone = $$; console; // { foo: { bar: 1, moo: [ 1, 2, 3 ] }, zoo: 16 } console; // false console; // true console; // false console; // true
Recursively examine any object, with any components, using the kernel
function:
var $$ = ; var obj = foo: bar: 1 moo: 1 2 3 zoo: 16 ; $$; // foo: [object Object] // bar: 1 // moo: 1,2,3 // 0: 1 // 1: 2 // 2: 3 // zoo: 16 // Find any '3's in any value position, anywhere var anyThrees = false; $$; console; // true