Comparing version 0.1.2 to 0.2.1
// Generated by CoffeeScript 1.7.1 | ||
(function() { | ||
var exportMethod, func, isArray, logging, method, methods, name, _i, _len, | ||
var exportArbitraryMethod, exportMethod, exportMutatorMethod, func, global, isArray, logging, method, methods, name, _i, _j, _k, _l, _len, _len1, _len2, _len3, _ref, _ref1, _ref2, _ref3, _ref4, | ||
__slice = [].slice; | ||
@@ -14,2 +14,6 @@ | ||
global = (function() { | ||
return this; | ||
})(); | ||
exportMethod = function(method) { | ||
@@ -23,2 +27,23 @@ return exports[method] = function() { | ||
exportMutatorMethod = function(method) { | ||
return exports[method] = function() { | ||
var args, newValue; | ||
args = 1 <= arguments.length ? __slice.call(arguments, 0) : []; | ||
if (!Array.isArray(this.value)) { | ||
throw new Error("Must be an array"); | ||
} | ||
newValue = this.value.slice(0); | ||
newValue[method].apply(newValue, args); | ||
return newValue; | ||
}; | ||
}; | ||
exportArbitraryMethod = function(name, method) { | ||
return exports[name] = function() { | ||
var args; | ||
args = 1 <= arguments.length ? __slice.call(arguments, 0) : []; | ||
return method.apply(null, [this.value].concat(__slice.call(args))); | ||
}; | ||
}; | ||
for (name in logging) { | ||
@@ -29,11 +54,32 @@ func = logging[name]; | ||
for (_i = 0, _len = methods.length; _i < _len; _i++) { | ||
method = methods[_i]; | ||
_ref = methods.plain; | ||
for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
method = _ref[_i]; | ||
exportMethod(method); | ||
} | ||
exports.isArray = function() { | ||
return isArray(this.value); | ||
}; | ||
_ref1 = methods.mutators; | ||
for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { | ||
method = _ref1[_j]; | ||
exportMutatorMethod(method); | ||
} | ||
_ref2 = methods.globals; | ||
for (_k = 0, _len2 = _ref2.length; _k < _len2; _k++) { | ||
method = _ref2[_k]; | ||
exportArbitraryMethod(method, global[method]); | ||
} | ||
_ref3 = methods.methods; | ||
for (name in _ref3) { | ||
method = _ref3[name]; | ||
exportArbitraryMethod(name, method); | ||
} | ||
_ref4 = methods.maths; | ||
for (_l = 0, _len3 = _ref4.length; _l < _len3; _l++) { | ||
method = _ref4[_l]; | ||
exportArbitraryMethod(method, Math[method]); | ||
} | ||
exports.length = function() { | ||
@@ -50,2 +96,14 @@ if (typeof this.value !== 'string' && !isArray(this.value)) { | ||
exports.toStr = function() { | ||
return this.value.toString(); | ||
}; | ||
exports["if"] = function(f1, f2) { | ||
if (this.value) { | ||
return f1(); | ||
} else if (f2) { | ||
return f2(); | ||
} | ||
}; | ||
}).call(this); |
@@ -21,5 +21,5 @@ // Generated by CoffeeScript 1.7.1 | ||
}); | ||
return log(v); | ||
return log(JSON.stringify(this.value)); | ||
}; | ||
}).call(this); |
// Generated by CoffeeScript 1.7.1 | ||
(function() { | ||
module.exports = ["chatAt", "chatCodeAt", "concat", "indexOf", "lastIndexOf", "localeCompare", "match", "replace", "search", "slice", "split", "substr", "substring", "toLocaleLowerCase", "toLocaleUpperCase", "toLowerCase", "toUpperCase", "trim", "pop", "push", "reverse", "shift", "sort", "splice", "unshift", "concat", "join", "slice", "indexOf", "lastIndexOf", "forEach", "every", "some", "filter", "map", "reduce", "reduceRight"]; | ||
exports.plain = ["charAt", "charCodeAt", "concat", "indexOf", "lastIndexOf", "localeCompare", "match", "replace", "search", "slice", "split", "substr", "substring", "toLocaleLowerCase", "toLocaleUpperCase", "toLowerCase", "toUpperCase", "trim", "concat", "join", "slice", "indexOf", "lastIndexOf", "forEach", "every", "some", "filter", "map", "reduce", "reduceRight", "toLocaleString", 'apply', 'bind', 'call', 'toExponential', 'toFixed', 'toLocaleString', 'toPrecision', 'exec', 'test']; | ||
exports.mutators = ["reverse", "sort", "pop", "shift", "push", "splice", "unshift"]; | ||
exports.globals = ['eval', 'isFinite', 'isNaN', 'parseFloat', 'parseInt', 'decodeURI', 'decodeURIComponent', 'encodeURI', 'encodeURIComponent', 'escape', 'unescape']; | ||
exports.methods = { | ||
isArray: Array.isArray, | ||
stringify: JSON.stringify, | ||
parseJSON: JSON.parse, | ||
logarithm: Math.log, | ||
inc: function(x) { | ||
return x + 1; | ||
}, | ||
dec: function(x) { | ||
return x - 1; | ||
}, | ||
neg: function(x) { | ||
return -x; | ||
}, | ||
"typeof": function(x) { | ||
return typeof x; | ||
}, | ||
del: function(x, name) { | ||
delete x[name]; | ||
return x; | ||
}, | ||
"in": function(prop, obj) { | ||
return prop in obj; | ||
}, | ||
"instanceof": function(obj, type) { | ||
return obj instanceof type; | ||
}, | ||
mult: function(x, y) { | ||
return x * y; | ||
}, | ||
div: function(x, y) { | ||
return x / y; | ||
}, | ||
mod: function(x, y) { | ||
return x % y; | ||
}, | ||
add: function(x, y) { | ||
return x + y; | ||
}, | ||
sub: function(x, y) { | ||
return x - y; | ||
}, | ||
bitNot: function(x) { | ||
return ~x; | ||
}, | ||
bitAnd: function(x, y) { | ||
return x & y; | ||
}, | ||
bitOr: function(x, y) { | ||
return x | y; | ||
}, | ||
bitLeft: function(x, y) { | ||
return x << y; | ||
}, | ||
bitRight: function(x, y) { | ||
return x >> y; | ||
}, | ||
bitRightFill: function(x, y) { | ||
return x >>> y; | ||
}, | ||
gt: function(x, y) { | ||
return x > y; | ||
}, | ||
gte: function(x, y) { | ||
return x >= y; | ||
}, | ||
lt: function(x, y) { | ||
return x < y; | ||
}, | ||
lte: function(x, y) { | ||
return x <= y; | ||
}, | ||
and: function(x, y) { | ||
return x && y; | ||
}, | ||
or: function(x, y) { | ||
return x || y; | ||
}, | ||
not: function(x) { | ||
return !x; | ||
} | ||
}; | ||
exports.maths = ['abs', 'acos', 'asin', 'atan', 'atan2', 'ceil', 'cos', 'exp', 'floor', 'max', 'min', 'pow', 'round', 'sin', 'sqrt', 'tan']; | ||
}).call(this); |
@@ -12,3 +12,3 @@ { | ||
], | ||
"version": "0.1.2", | ||
"version": "0.2.1", | ||
"author": "Jakob Mattsson <jakob.mattsson@gmail.com> (jakobmattsson.se)", | ||
@@ -26,2 +26,49 @@ "license": "MIT", | ||
], | ||
"bucketful": { | ||
"bucket": "z-builtins.jdevab.com", | ||
"source": "tmp/browsertest", | ||
"region": "eu-west-1" | ||
}, | ||
"chalcogen": { | ||
"timeout": 60000, | ||
"domain": "z-builtins.jdevab.com.s3-website-eu-west-1.amazonaws.com", | ||
"path": "/default/index.html", | ||
"environments": [ | ||
{ | ||
"browserName": "internet explorer", | ||
"platform": "Windows 8", | ||
"version": "10" | ||
}, | ||
{ | ||
"browserName": "firefox", | ||
"platform": "Windows 8", | ||
"version": "26" | ||
}, | ||
{ | ||
"browserName": "chrome", | ||
"platform": "Linux", | ||
"version": "26" | ||
}, | ||
{ | ||
"browserName": "chrome", | ||
"platform": "Windows 8.1", | ||
"version": "beta" | ||
}, | ||
{ | ||
"browserName": "chrome", | ||
"platform": "OS X 10.8", | ||
"version": "beta" | ||
}, | ||
{ | ||
"browserName": "safari", | ||
"platform": "OS X 10.8", | ||
"version": "6" | ||
}, | ||
{ | ||
"browserName": "safari", | ||
"platform": "Windows 7", | ||
"version": "5" | ||
} | ||
] | ||
}, | ||
"engines": { | ||
@@ -32,8 +79,11 @@ "npm": ">= 1.3 < 2", | ||
"devDependencies": { | ||
"z-core": "^0.4.1", | ||
"z-core": "^0.5.5", | ||
"jsontool": "7.0.1", | ||
"commonjs-everywhere": "^0.9.7", | ||
"bucketful": "^0.15.1", | ||
"chalcogen": "^0.4.0", | ||
"jscov": "^0.5.7", | ||
"mocha": "^1.12.1", | ||
"mocha": "^1.18.0", | ||
"mocha-term-cov-reporter": "^0.2.0", | ||
"chai-as-promised": "^4.1.0", | ||
"mocha-as-promised": "^2.0.0", | ||
"chai-as-promised": "^3.3.1", | ||
"coffee-script": "^1.7.1" | ||
@@ -43,9 +93,4 @@ }, | ||
"scripts": { | ||
"build": "rm -rf lib && coffee -co lib src", | ||
"make-dist": "npm test && mkdir -p dist && browserify lib/index.js > dist/latest.js", | ||
"coverage": "jscov --expand --conditionals src .cov && JSCOV=.cov mocha --reporter mocha-term-cov-reporter", | ||
"test": "mocha --grep \"$TESTS\"", | ||
"pretest": "npm run build", | ||
"prepublish": "npm run build" | ||
"test": "make run-tests" | ||
} | ||
} |
306
README.md
# z-builtins [![Build Status](https://secure.travis-ci.org/jakobmattsson/z-builtins.png)](http://travis-ci.org/jakobmattsson/z-builtins) | ||
JavaScript builtin mixins for Z | ||
A plugin for [Z](https://github.com/jakobmattsson/z-core), 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](https://github.com/jakobmattsson/z-core). It includes this plugin. | ||
--- | ||
If you want to set it up yourself, you have a number of options: | ||
* `npm install z-builtins` and then `var zBuiltins = require('z-builtins');` | ||
* `bower install z-builtins` | ||
* Download it manually from the dist folder of this repo. | ||
When installed, you apply the plugin by calling `Z.mixin(zBuiltins)`. Note that you have to install [Z](https://github.com/jakobmattsson/z-core) first, if you're not using a ready-made package. | ||
## Crash course | ||
Go to the [Z main page](https://github.com/jakobmattsson/z-core) 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: | ||
```js | ||
// Let's assume this returns a Z-promise. | ||
// If it expects a callback then we can convert it using Z beforehand. | ||
var people = getJSON('/people'); | ||
// Even though "people" is a promise, and not an array, | ||
// we can now use all the regular methods as if it was an array. | ||
// No need to sprinke our code with ".then". | ||
var listOfToddlers = people.filter(function(person) { | ||
return person.age <= 3; | ||
}).map(function(person) { | ||
return person.name; | ||
}).join('\n'); | ||
// Even global methods like "console.log" are | ||
// available directly on our objects now. | ||
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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects) directly. All quirks and bugs will still get to you. As will all fixes and performance improvements in the future. | ||
### String methods | ||
```js | ||
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: | ||
```js | ||
var html = getHTML('http://www.google.com'); | ||
var str1 = html.toString(); // An actual string, containing "[object Object]" | ||
var str2 = html.toStr(); // A promise, containing the html-string | ||
console.log(str1); // The actual string can be used in all the ways strings can | ||
str2.then(function(s) { // The promised string has to be resolved | ||
console.log(s); // "<html><head>......" | ||
}); | ||
str2.slice(0, 5).log(); // Or better, used with methods from this plugin. | ||
// Prints "<html" | ||
``` | ||
### Array methods | ||
```js | ||
pop * | ||
push * | ||
reverse * | ||
shift * | ||
sort * | ||
splice * | ||
unshift * | ||
concat | ||
join | ||
slice | ||
toString *AND* toStr // same as String.toString, earlier in the docs | ||
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: | ||
```js | ||
var numbers = getJSON('/primeNumbers?n=5'); | ||
numbers.log(); // 2, 3, 5, 7, 11 | ||
var newNumbers = numbers.push(9000); | ||
numbers.log(); // still 2, 3, 5, 7, 11 | ||
newNumbers.log(); // 2, 3, 5, 7, 11, 9000 | ||
``` | ||
### 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 | ||
```js | ||
Z("1+5").eval().log(); // 6 | ||
Z("1111").parseInt(2); // 15 | ||
``` | ||
### Other methods | ||
```js | ||
length | ||
isArray | ||
log | ||
``` | ||
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: | ||
```js | ||
var people = getJSON('/people'); | ||
people.isArray().log(); // true | ||
people.length().log(); // 6000000000 | ||
``` |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
15749
203
308
10
7
1