z-builtins
A plugin for Z, the utility library for JavaScript promises.
The z-builtins plugin exposes all native JavaScript methods directly on Z promises.
Installation
The easiest way to get started is to use a ready-made package of Z, like z-std-pack. It includes this plugin.
If you want to set it up yourself, you have a number of options:
When installed, you apply the plugin by calling Z.mixin(zBuiltins)
. Note that you have to install Z first, if you're not using a ready-made package.
Crash course
Go to the Z main page to learn what Z does.
The idea behind this particular plugin, z-builtins, is to expose all the standard JavaScript methods available on strings, arrays and other types of objects directly on promises wrapping them. It allows you to do things like:
var people = getJSON('/people');
var listOfToddlers = people.filter(function(person) {
return person.age <= 3;
}).map(function(person) {
return person.name;
}).join('\n');
listOfToddlers.log();
All the usual methods are available directly on promises. Instead of returning their results directly (which would be impossible, since the promise might not have resolved) they in turn return new promises. Those new promises also have all the expected methods. And so on. Down to turtles.
The only time you have to treat these objects diferently then you have treated regular objects is when you call global functions like console.log
. If the list of toddlers from above was used like this, then console.log(listOfToddlers)
would just print "[object Object]"
since listOfToddlers
is not actually as string; it's a promise. Instead, either do listOfToddlers.then(function(list) { console.log(list)); };
or simply listOfToddlers.log()
.
Methods included in this plugin
Except then explicitly noted, all methods below will invoke their native counterparts directly. All quirks and bugs will still get to you. As will all fixes and performance improvements in the future.
String methods
charAt
charCodeAt
concat
indexOf
lastIndexOf
localeCompare
replace
search
slice
split
substr
substring
toLocaleLowerCase
toLocaleUpperCase
toLowerCase
toString *AND* toStr
toUpperCase
trim
Note that toString
has not been overridden to return a promise. It will return a string as always. That is to follow the principle of least surprise. If you want to call toString
on the value that the promise resolves to, then you can use toStr
.
Example:
var html = getHTML('http://www.google.com');
var str1 = html.toString();
var str2 = html.toStr();
console.log(str1);
str2.then(function(s) {
console.log(s);
});
str2.slice(0, 5).log();
Array methods
pop *
push *
reverse *
shift *
sort *
splice *
unshift *
concat
join
slice
toString *AND* toStr
toLocaleString
indexOf
lastIndexOf
forEach
every
some
filter
map
reduce
reduceRight
The methods followed by a star are mutator methods in the JavaScript standard library. They change the object they act upon and return something else.
Mutating behaviour doesn't work well with promises. Therefore, these methods do not mutate when used in Z. Their usual return values are ignored and instead they return the new array produced.
Example:
var numbers = getJSON('/primeNumbers?n=5');
numbers.log();
var newNumbers = numbers.push(9000);
numbers.log();
newNumbers.log();
JSON methods
parseJSON (alias for JSON.parse, since parse is ambiguous)
stringify
Math methods
abs
acos
asin
atan
atan2
ceil
cos
exp
floor
logarithm (alias for log, since log is for console.log)
max
min
pow
round
sin
sqrt
tan
Number methods
toExponential
toFixed
toLocaleString
toPrecision
Regexp methods
exec
test
Function methods
apply
bind
call
Operators
get - the dot and indexing operators (. and [])
inc - increment (++)
dec - decrement (--)
neg - negate (-)
typeof
del - delete
in
instanceof
mult - multiplication (*)
div - division (/)
mod - modulo (%)
add - addition (+)
sub - subtraction (-)
bitNot - bitwise not (~)
bitAnd - bitwise and (&)
bitOr - bitwise or (|)
bitLeft - bitwise left shift (<<)
bitRight - bitwise right shift (>>)
bitRightFill - bitwise right shift zero fill (>>>)
gt - greater than (>)
gte - greather than or equal to (>=)
lt - less than (<)
lte - less than or equal to (<=)
and - logical and (&&)
or - logical or (||)
not - logical not (!)
if - the conditional operator (:?)
Note: The operators yield
, void
, new
, ,
and the assignment operators are not available.
Global functions
eval
isFinite
isNaN
parseFloat
parseInt
decodeURI
decodeURIComponent
encodeURI
encodeURIComponent
escape
unescape
These functions use the promised value as their first argument. Remaining arguments can be given as usual.
Example
Z("1+5").eval().log();
Z("1111").parseInt(2);
Other methods
length
isArray
log
print
The length
method returns the value of the length property.
The isArray
method invokes Array.iArray
.
The log
method invoked console.log
.
The print
method invokes JSON.stringify
followed by console.log
.
Example:
var people = getJSON('/people');
people.isArray().log();
people.length().log();