JSFix
v1.2.0
A Lightweight Javascript Patches.
This paches is works for both NodeJS and Browser. This patches is created to support the Stater project.
Helpers
Public Methods
Data Type
Data type helpers is a functions to validate the variable value type.
Example
var str = 'A String'int = 'A Number';console; // trueconsole; // falseconsole; // falseconsole; // true
isDefined()
isUndefined()
Check does the function argument is defined.
isNull()
isNotNull()
Check does the variable value is null.
isString()
isNotString()
Check does the variable value is a string.
isNumber()
isNotNumber()
Check does the variable value is a number.
isFunction()
isNotFunction()
Check does the variable value is a function.
isArray()
isNotArray()
Check does the variable value is an array.
isObject()
isNotObject()
Check does the variable value is an object and not an array.
isBoolean()
isNotBoolean()
Check does the variable value is a boolean.
isDate()
isNotDate()
Check does the variable value is a date.
isURL()
isNotURL()
Check does the variable value is a URL.
isEmail()
isNotEmail()
Check does the variable value is a Email.
isArguments()
isNotArguments()
Check does the variable value is an function Arguments.
isError()
isNotError()
Check does the variable value is an error object.
isJSON()
isNotJSON()
Check does the variable value is a valid JSON object.
isJSONString()
isNotJSONString()
Check does the variable value is a valid JSON string.
isRegExp()
isNotRegExp()
Check does the variable value is a RegExp.
isEmpty()
isNotEmpty()
Check does the variable value is empty. Value can be Object, Array, and String.
Utilities
$dext()
Assign new prototypes to Javascript objects.
Example
; // Assign prototype to Object.; // Assign prototype to String.console; // ['S', 't', 'r', 'i', 'n', 'g']
$dget()
Define getter to Javascript objects.
Example
var x = a: 1 b: 2 ;;console; // Value of c is: 3
$dset()
Define setter to Javascript objects.
Example
var x = a: 1 b: 2 ;;xc = 10;console; // 20
Iterator
A simple Object and Array iterator that wait until the this.next()
function is executed to proceed the next items.
Usage
;
Example
// Iterating object.;// Iterating array.
Patches
Object Patches
Adding some methods to the Object.prototype
.
Direct Iterator
obj.$each()
, arr.$each()
, str.$each()
, and num.$each()
A direct object iterator, with reversed support for iterating array, number and string.
Usage
object;
Example
var obj = a: 1 b: 2 c: 3arr = 1 2 3str = 'String'num = 10;// Iterating object.obj;// Iterating array.arr;// Iterating stringstr;// Iterating number.num;// Reversed iterating array.arr$each {console;} true);
Path Helpers
An array and object path helpers.
obj.$get()
[browser: obj.$gets()
]
Get an object and array value using path.
Usage
obj;
- path -
Required
String path (e.g:a.b.c
) - default -
Optional
Default value when the path value is undefined.
Example
var obj =a: 1b: 2c:ca: 1cb:cba: 1cbb: 2var arr = 'a' 'b' a: 1 b: 2console; // undefinedconsole; // 1console; // 10 since the value is undefined.console; // undefinedconsole; // 1
obj.$set()
[browser: obj.$sets()
]
Set an object and array value using path.
Usage
obj;
- path -
Required
String path (e.g:a.b.c
) - value -
Required
Value to set to.
Example
var obj = {}arr = ;obj; // { a: { b: { c: 100 } } }arr; // [ { a: { b: 100 } } ]
obj.$add()
[browser: obj.$adds()
]
Push an item into array in path.
Usage
obj;
- path -
Required
String path. - value -
Required
Value to set to.
Example
var obj = a: b: c: ;obj; // { a: { b: { c: [ 'item' ] } } }
obj.$del()
[browser: obj.$dels()
]
Array and Object path remover.
Usage
obj;
- path -
Required
String path. - length -
Optional
The length of item to be removed. Only for array.
Example
var obj = a: b: c: 123 d: 1 ;obj; // Remove property "d"obj; // Remove 1 and 2 from c.
obj.$dir()
[browser: obj.$dirs()
]
Extract the paths from array or object.
Usage
obj;
- exclude_root -
Optional
Does the root object or array is included.
Example
var obj = a: b: c: 3 d: 4 ;obj; // { 'a.b.c': { type: 'number', body: 3 }, 'a.b.d': { type: 'number', body: 4 }
obj.$diff()
[browser: obj.$diffs()
]
Compare two object (object to object, array to array).
Usage
obj;
- target -
Required
Array or object to compare from.
Example
var a = a: 1 b: 2b = a: 2 b: 1 c: 3 ;a;/*{ a: { old: 1, new: 2 },b: { old: 2, new: 1 },c: { old: undefined, new: 3 } }*/b;/*{ a: { old: 2, new: 1 },b: { old: 1, new: 2 },c: { old: 3, new: undefined } }*/
obj.$join()
Recursively merge two objects or arrays (object to object, array to array).
Usage
obj;
- target -
Required
Array or object to merge with. - ignore -
Optional
* Array contains paths to ignore.
Example
var a = a: 1 b: 2b = b: 1 c: 3 ;a; a: 1 b: 1 c: 3
obj.$sort()
[browser: obj.$sorts()
]
Recursively sort object or array.
Usage
obj;
- handler -
Optional
* - Function to handler the sorting.
Example
var obj = d: 4 b: 2 c: 3 a: 1 ;obj; // { a: 1, b: 2, c: 3, d: 4 };
obj.$keys()
Direct Object.keys()
Example
var obj = a: 1 b: 2 ;obj; // [ 'a', 'b' ]
obj.$group()
[browser: obj.$groups()
]
Group an array items with specific column per row.
Usage
obj;
- column -
Required
The number of columns per row. - mode -
Optional
The grouping mode (split or chunk). Default is chunk.
Example
var arr = 1 2 3 4 5 6 7 8 ;arr; // [ [ 1, 4, 7 ], [ 2, 5, 8 ], [ 3, 6 ] ]arr; // [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8 ] ]
obj.$extend()
Extend the already defined object and array prototypes.
Usage
obj;
name
- String prototype name, or object containskey
as name andvalue
as handler.handler
- Function to handle the prototype call.
Example
var x = a: 1 b: 2x;console; // ['a', 'b']
Number.random()
Generate random number.
Example
Number;Number;
Changelog
v1.3.2 - May 11, 2016
- Prevent reinitializing patches if already initialized.
v1.3.1 - Apr 23, 2016
- Fixing error when using
readable-stream
v1.3.0 - Mar 17, 2016
- Object methods changes:
$gets
,$sets
,$adds
,$dels
,$diffs
,$sorts
, and$groups
only for browser. NodeJS users will use.$get()
,.$set()
, and so on. - Changed
.$ext()
to.$extend()
- Changed
.$dif()
to.$diff()
v1.2.1 - Feb 20, 2016
- Fixing wrong dependency.
v1.2.0 - Feb 17, 2016
- Changed
$get
to$gets
due to conflict with angular. - Chagned
$set
to$sets
due to conflict with angular. - Changed
$add
to$adds
due to conflict with angular. - Changed
$del
to$dels
due to conflict with angular. - Changed
$dirs
to$dirs
due to conflict with angular. - Changed
$dif
to$difs
due to conflict with angular. - Changed
$sort
to$sorts
due to conflict with angular. - Changed
$group
to$groups
due to conflict with angular.