Comparing version 0.8.2 to 1.0.0-alpha-01
293
lib/array.js
@@ -1,53 +0,256 @@ | ||
// Generated by CoffeeScript 1.7.1 | ||
// Generated by CoffeeScript 1.8.0 | ||
(function() { | ||
var $; | ||
var assert, binary, compose, curry, deep_equal, describe, flip, identity, liberate, lt, odd, partial, ternary, unary, _, _ref, _ref1, _ref2, | ||
__slice = [].slice, | ||
__indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; | ||
$ = {}; | ||
_ref = require("./core"), curry = _ref.curry, flip = _ref.flip, compose = _ref.compose, partial = _ref.partial, _ = _ref._, identity = _ref.identity, unary = _ref.unary, binary = _ref.binary, ternary = _ref.ternary; | ||
$.remove = function(array, element) { | ||
var index, _ref; | ||
if ((index = array.indexOf(element)) > -1) { | ||
[].splice.apply(array, [index, index - index + 1].concat(_ref = [])), _ref; | ||
return element; | ||
} else { | ||
return null; | ||
} | ||
}; | ||
liberate = require("./object").liberate; | ||
$.uniq = function(array, hash) { | ||
var element, key, uniques, _i, _j, _len, _len1, _ref, _results; | ||
if (hash == null) { | ||
hash = function(object) { | ||
return object.toString(); | ||
}; | ||
} | ||
uniques = {}; | ||
for (_i = 0, _len = array.length; _i < _len; _i++) { | ||
element = array[_i]; | ||
uniques[hash(element)] = element; | ||
} | ||
_ref = Object.keys(uniques); | ||
_results = []; | ||
for (_j = 0, _len1 = _ref.length; _j < _len1; _j++) { | ||
key = _ref[_j]; | ||
_results.push(uniques[key]); | ||
} | ||
return _results; | ||
}; | ||
deep_equal = require("./type").deep_equal; | ||
$.shuffle = function(array) { | ||
var copy, i, j, _i, _ref, _ref1; | ||
copy = array.slice(0); | ||
if (copy.length <= 1) { | ||
return copy; | ||
} | ||
for (i = _i = _ref = copy.length - 1; _ref <= 1 ? _i <= 1 : _i >= 1; i = _ref <= 1 ? ++_i : --_i) { | ||
j = Math.floor(Math.random() * (i + 1)); | ||
_ref1 = [copy[j], copy[i]], copy[i] = _ref1[0], copy[j] = _ref1[1]; | ||
} | ||
return copy; | ||
}; | ||
_ref1 = require("./numeric"), odd = _ref1.odd, lt = _ref1.lt; | ||
module.exports = $; | ||
_ref2 = require("./helpers"), assert = _ref2.assert, describe = _ref2.describe; | ||
describe("Array functions", function(context) { | ||
var all, any, cat, difference, drop, dupes, each, filter, first, flatten, fold, foldr, includes, intersection, last, leave, map, remove, rest, shuffle, slice, take, union, uniq, unique, unique_by; | ||
fold = curry(flip(ternary(liberate(Array.prototype.reduce)))); | ||
context.test("fold", function() { | ||
var data, fn; | ||
data = [1, 2, 3, 4, 5]; | ||
fn = fold(1, function(acc, x) { | ||
return acc += x; | ||
}); | ||
return assert(deep_equal(fn(data), 16)); | ||
}); | ||
foldr = curry(flip(ternary(liberate(Array.prototype.reduce)))); | ||
context.test("foldr", function() { | ||
var data, fn; | ||
data = [1, 2, 3, 4, 5]; | ||
fn = foldr(1, function(acc, x) { | ||
return acc += x; | ||
}); | ||
return assert(deep_equal(fn(data), 16)); | ||
}); | ||
map = curry(flip(binary(liberate(Array.prototype.map)))); | ||
context.test("map", function() { | ||
var data; | ||
data = [1, 2, 3, 4, 5]; | ||
return assert(deep_equal(map((function(x) { | ||
return x * 2; | ||
}), data), [2, 4, 6, 8, 10])); | ||
}); | ||
filter = curry(flip(binary(liberate(Array.prototype.filter)))); | ||
context.test("filter", function() { | ||
var data; | ||
data = [1, 2, 3, 4, 5]; | ||
return assert(deep_equal(filter(odd, data), [1, 3, 5])); | ||
}); | ||
any = curry(flip(binary(liberate(Array.prototype.some)))); | ||
context.test("any", function() { | ||
var data; | ||
data = [1, 2, 3, 4, 5]; | ||
return assert(any(odd, data)); | ||
}); | ||
all = curry(flip(binary(liberate(Array.prototype.every)))); | ||
context.test("all", function() { | ||
var data; | ||
data = [1, 2, 3, 4, 5]; | ||
return assert(all(lt(6), data)); | ||
}); | ||
each = curry(flip(binary(liberate(Array.prototype.forEach)))); | ||
context.test("each", function() { | ||
return (function(ax, data) { | ||
each((function(x) { | ||
return ax.push(x); | ||
}), data); | ||
return assert(deep_equal(ax, data)); | ||
})([], [1, 2, 3, 4, 5]); | ||
}); | ||
cat = liberate(Array.prototype.concat); | ||
context.test("cat", function() { | ||
var data; | ||
data = [1, 2, 3, 4, 5]; | ||
return assert(deep_equal(cat(data), data)); | ||
}); | ||
slice = curry(function(i, j, ax) { | ||
return ax.slice(i, j); | ||
}); | ||
first = function(ax) { | ||
return ax[0]; | ||
}; | ||
context.test("first", function() { | ||
var data; | ||
data = [1, 2, 3, 4, 5]; | ||
return assert((first(data)) === 1); | ||
}); | ||
last = function(_arg) { | ||
var rest, x, _i; | ||
rest = 2 <= _arg.length ? __slice.call(_arg, 0, _i = _arg.length - 1) : (_i = 0, []), x = _arg[_i++]; | ||
return x; | ||
}; | ||
context.test("last", function() { | ||
return (function(data) { | ||
return assert((last(data)) === 5); | ||
})([1, 2, 3, 4, 5]); | ||
}); | ||
rest = slice(1, void 0); | ||
context.test("rest", function() { | ||
return (function(data) { | ||
return assert(deep_equal(rest(data), [2, 3, 4, 5])); | ||
})([1, 2, 3, 4, 5]); | ||
}); | ||
take = slice(0); | ||
context.test("take", function() { | ||
return (function(data) { | ||
return assert(deep_equal(take(3, data), [1, 2, 3])); | ||
})([1, 2, 3, 4, 5]); | ||
}); | ||
leave = curry(function(n, ax) { | ||
return slice(0, -n, ax); | ||
}); | ||
context.test("leave", function() { | ||
return (function(data) { | ||
return assert(deep_equal(leave(3, data), [1, 2])); | ||
})([1, 2, 3, 4, 5]); | ||
}); | ||
drop = curry(partial(slice, _, void 0, _)); | ||
context.test("drop", function() { | ||
return (function(data) { | ||
return assert(deep_equal(drop(3, data), [4, 5])); | ||
})([1, 2, 3, 4, 5]); | ||
}); | ||
includes = function(x, ax) { | ||
return __indexOf.call(ax, x) >= 0; | ||
}; | ||
context.test("includes", function() { | ||
return (function(data) { | ||
assert(includes(3, data)); | ||
return assert(!(includes(6, data))); | ||
})([1, 2, 3, 4, 5]); | ||
}); | ||
unique_by = curry(function(f, ax) { | ||
var a, bx, y, _i, _len; | ||
bx = []; | ||
for (_i = 0, _len = ax.length; _i < _len; _i++) { | ||
a = ax[_i]; | ||
y = f(a); | ||
if (__indexOf.call(bx, y) < 0) { | ||
bx.push(y); | ||
} | ||
} | ||
return bx; | ||
}); | ||
unique = uniq = unique_by(identity); | ||
context.test("unique", function() { | ||
return (function(data) { | ||
return assert(deep_equal(unique(data), [1, 2, 3, 5, 6])); | ||
})([1, 2, 1, 3, 5, 3, 6]); | ||
}); | ||
flatten = function(ax) { | ||
return fold([], (function(r, a) { | ||
if (a.forEach != null) { | ||
r.push.apply(r, flatten(a)); | ||
} else { | ||
r.push(a); | ||
} | ||
return r; | ||
}), ax); | ||
}; | ||
context.test("flatten", function() { | ||
return (function(data) { | ||
return assert(deep_equal(flatten(data), [1, 2, 3, 4, 5, 6, 7])); | ||
})([1, [2, 3], 4, [5, [6, 7]]]); | ||
}); | ||
difference = curry(function(ax, bx) { | ||
var cx; | ||
cx = union(ax, bx); | ||
return cx.filter(function(c) { | ||
return (__indexOf.call(ax, cx) >= 0 && !(__indexOf.call(bx, cx) >= 0)) || (__indexOf.call(bx, cx) >= 0 && !(__indexOf.call(bx, cx) >= 0)); | ||
}); | ||
}); | ||
dupes = function(_arg) { | ||
var first, rest; | ||
first = _arg[0], rest = 2 <= _arg.length ? __slice.call(_arg, 1) : []; | ||
if (rest.length === 0) { | ||
return []; | ||
} else if (__indexOf.call(rest, first) >= 0) { | ||
return [first].concat(__slice.call(dupes(rest))); | ||
} else { | ||
return dupes(rest); | ||
} | ||
}; | ||
context.test("dupes", function() { | ||
return (function(data) { | ||
return assert(deep_equal(dupes(data), [1, 3])); | ||
})([1, 2, 1, 3, 5, 3, 6]); | ||
}); | ||
union = curry(compose(unique, cat)); | ||
intersection = curry(compose(dupes, cat)); | ||
remove = function(array, element) { | ||
var index, _ref3; | ||
if ((index = array.indexOf(element)) > -1) { | ||
[].splice.apply(array, [index, index - index + 1].concat(_ref3 = [])), _ref3; | ||
return element; | ||
} else { | ||
return null; | ||
} | ||
}; | ||
shuffle = function(ax) { | ||
var b, bx, i, j, _i, _len, _ref3; | ||
bx = cat(ax); | ||
if (!(bx.length <= 1)) { | ||
for (i = _i = 0, _len = bx.length; _i < _len; i = ++_i) { | ||
b = bx[i]; | ||
j = i; | ||
while (j === i) { | ||
j = Math.floor(Math.random() * bx.length); | ||
} | ||
_ref3 = [bx[j], bx[i]], bx[i] = _ref3[0], bx[j] = _ref3[1]; | ||
} | ||
if (deep_equal(ax, bx)) { | ||
return shuffle(ax); | ||
} else { | ||
return bx; | ||
} | ||
} else { | ||
return bx; | ||
} | ||
}; | ||
context.test("shuffle", function() { | ||
return (function(data) { | ||
return assert(!deep_equal(shuffle(data), data)); | ||
})([1, 2, 3, 4, 5]); | ||
}); | ||
return module.exports = { | ||
fold: fold, | ||
foldr: foldr, | ||
map: map, | ||
filter: filter, | ||
any: any, | ||
all: all, | ||
each: each, | ||
cat: cat, | ||
slice: slice, | ||
first: first, | ||
last: last, | ||
rest: rest, | ||
take: take, | ||
leave: leave, | ||
drop: drop, | ||
includes: includes, | ||
unique_by: unique_by, | ||
unique: unique, | ||
uniq: uniq, | ||
flatten: flatten, | ||
dupes: dupes, | ||
union: union, | ||
intersection: intersection, | ||
remove: remove, | ||
shuffle: shuffle | ||
}; | ||
}); | ||
}).call(this); |
@@ -1,21 +0,40 @@ | ||
// Generated by CoffeeScript 1.7.1 | ||
// Generated by CoffeeScript 1.8.0 | ||
(function() { | ||
var $; | ||
var assert, describe, _ref; | ||
$ = {}; | ||
_ref = require("./helpers"), describe = _ref.describe, assert = _ref.assert; | ||
$.md5 = function(string) { | ||
var Crypto; | ||
Crypto = require("crypto"); | ||
return Crypto.createHash('md5').update(string, 'utf-8').digest("hex"); | ||
}; | ||
describe("Crypto Functions", function(context) { | ||
var base64, base64url, md5; | ||
md5 = function(string) { | ||
var Crypto; | ||
Crypto = require("crypto"); | ||
return Crypto.createHash('md5').update(string, 'utf-8').digest("hex"); | ||
}; | ||
context.test("md5", function() { | ||
return assert((md5("It was a dark and stormy night")).trim != null); | ||
}); | ||
base64 = function(string) { | ||
var Crypto; | ||
Crypto = require("crypto"); | ||
return new Buffer(string).toString('base64'); | ||
}; | ||
context.test("base64", function() { | ||
return (base64("It was a dark and stormy night")).trim != null; | ||
}); | ||
base64url = function(string) { | ||
var Crypto; | ||
Crypto = require("crypto"); | ||
return new Buffer(string).toString('base64').replace(/\+/g, '-').replace(/\//g, '_').replace(/\=+$/, ''); | ||
}; | ||
context.test("base64url", function() { | ||
return (base64url("It was a dark and stormy night")).trim != null; | ||
}); | ||
return module.exports = { | ||
md5: md5, | ||
base64: base64, | ||
base64url: base64url | ||
}; | ||
}); | ||
$.base64 = function(string) { | ||
var Crypto; | ||
Crypto = require("crypto"); | ||
return new Buffer(string).toString('base64'); | ||
}; | ||
module.exports = $; | ||
}).call(this); |
193
lib/fs.js
@@ -1,60 +0,147 @@ | ||
// Generated by CoffeeScript 1.7.1 | ||
// Generated by CoffeeScript 1.8.0 | ||
(function() { | ||
var $; | ||
var assert, describe, fs, _ref, | ||
__indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; | ||
$ = {}; | ||
$.exists = function(path) { | ||
var FileSystem; | ||
FileSystem = require("fs"); | ||
return FileSystem.existsSync(path); | ||
fs = function(f) { | ||
var async, call, liftAll; | ||
liftAll = require("when/node").liftAll; | ||
call = (require("when/generator")).call; | ||
async = (require("when/generator")).lift; | ||
return f(liftAll(require("fs")), { | ||
call: call, | ||
async: async | ||
}); | ||
}; | ||
$.read = function(path) { | ||
var FileSystem; | ||
FileSystem = require("fs"); | ||
return FileSystem.readFileSync(path, 'utf-8'); | ||
}; | ||
_ref = require("./helpers"), describe = _ref.describe, assert = _ref.assert; | ||
$.readdir = function(path) { | ||
var FileSystem; | ||
FileSystem = require("fs"); | ||
return FileSystem.readdirSync(path); | ||
}; | ||
describe("File system functions", function(context) { | ||
var chdir, exists, read, read_stream, readdir, rm, rmdir, stat, write; | ||
stat = function(path) { | ||
return fs(function(_arg) { | ||
var stat; | ||
stat = _arg.stat; | ||
return stat(path); | ||
}); | ||
}; | ||
context.test("stat", function*() { | ||
return assert(((yield stat("test/test.json"))).size != null); | ||
}); | ||
exists = stat; | ||
context.test("exists", function*() { | ||
(yield exists("test/test.json")); | ||
return assert((yield exists("test/test.json"))); | ||
}); | ||
read = function(path) { | ||
return fs(function(_arg, _arg1) { | ||
var call, readFile; | ||
readFile = _arg.readFile; | ||
call = _arg1.call; | ||
return call(function*() { | ||
return ((yield readFile(path))).toString(); | ||
}); | ||
}); | ||
}; | ||
context.test("read", function*() { | ||
return assert((JSON.parse((yield read("test/test.json")))).name === "fairmont"); | ||
}); | ||
readdir = function(path) { | ||
return fs(function(_arg) { | ||
var readdir; | ||
readdir = _arg.readdir; | ||
return readdir(path); | ||
}); | ||
}; | ||
context.test("readdir", function*() { | ||
return assert(__indexOf.call((yield readdir("test")), "test.json") >= 0); | ||
}); | ||
read_stream = (function(_arg) { | ||
var promise; | ||
promise = _arg.promise; | ||
return function(stream) { | ||
var buffer; | ||
buffer = ""; | ||
return promise(function(resolve, reject) { | ||
stream.on("data", function(data) { | ||
return buffer += data.toString(); | ||
}); | ||
stream.on("end", function() { | ||
return resolve(buffer); | ||
}); | ||
return stream.on("error", function(error) { | ||
return reject(error); | ||
}); | ||
}); | ||
}; | ||
})(require("when")); | ||
context.test("read_stream", function*() { | ||
var createReadStream, lines; | ||
createReadStream = require("fs").createReadStream; | ||
lines = ((yield read_stream(createReadStream("test/lines.txt")))).trim().split("\n"); | ||
assert(lines.length === 3); | ||
return assert(lines[0] === "one"); | ||
}); | ||
write = function(path, content) { | ||
return fs(function(_arg) { | ||
var writeFile; | ||
writeFile = _arg.writeFile; | ||
return writeFile(path, content); | ||
}); | ||
}; | ||
context.test("write", function*() { | ||
return write("test/test.json", (yield read("test/test.json"))); | ||
}); | ||
chdir = function(dir, fn) { | ||
return fs(function*(_arg, _arg1) { | ||
var async, cwd, rval; | ||
_arg; | ||
async = _arg1.async; | ||
cwd = process.cwd(); | ||
process.chdir(dir); | ||
rval = (yield fn()); | ||
process.chdir(cwd); | ||
return rval; | ||
}); | ||
}; | ||
fs(function(_arg, _arg1) { | ||
var async; | ||
_arg; | ||
async = _arg1.async; | ||
return context.test("chdir", function*() { | ||
(yield chdir("test", async(function*() { | ||
return assert((yield exists("test.json"))); | ||
}))); | ||
return assert((process.cwd().match(/test$/)) == null); | ||
}); | ||
}); | ||
rm = function(path) { | ||
return fs({ | ||
unlink: unlink | ||
})(function() { | ||
return unlink(path); | ||
}); | ||
}; | ||
context.test("rm"); | ||
rmdir = function(path) { | ||
return fs({ | ||
rmdir: rmdir | ||
})(function() { | ||
return rmdir(path); | ||
}); | ||
}; | ||
context.test("rmdir"); | ||
return module.exports = { | ||
exists: exists, | ||
read: read, | ||
read_stream: read_stream, | ||
readdir: readdir, | ||
stat: stat, | ||
write: write, | ||
chdir: chdir, | ||
rm: rm, | ||
rmdir: rmdir | ||
}; | ||
}); | ||
$.stat = function(path) { | ||
var FileSystem; | ||
FileSystem = require("fs"); | ||
return FileSystem.statSync(path); | ||
}; | ||
$.write = function(path, content) { | ||
var FileSystem; | ||
FileSystem = require("fs"); | ||
return FileSystem.writeFileSync(path, content); | ||
}; | ||
$.chdir = function(dir, fn) { | ||
var cwd, rval; | ||
cwd = process.cwd(); | ||
process.chdir(dir); | ||
rval = fn(); | ||
process.chdir(cwd); | ||
return rval; | ||
}; | ||
$.rm = function(path) { | ||
var FileSystem; | ||
FileSystem = require("fs"); | ||
return FileSystem.unlinkSync(path); | ||
}; | ||
$.rmdir = function(path) { | ||
var FileSystem; | ||
FileSystem = require("fs"); | ||
return FileSystem.rmdirSync(path); | ||
}; | ||
module.exports = $; | ||
}).call(this); |
106
lib/index.js
@@ -1,65 +0,71 @@ | ||
// Generated by CoffeeScript 1.7.1 | ||
// Generated by CoffeeScript 1.8.0 | ||
(function() { | ||
var $, basename, filename, include, readdir, _i, _len, _module, _ref; | ||
var assert, describe, include, _ref; | ||
$ = {}; | ||
_ref = require("./helpers"), describe = _ref.describe, assert = _ref.assert; | ||
$.w = function(string) { | ||
return string.trim().split(/\s+/); | ||
}; | ||
describe("General functions", function(context) { | ||
var abort, memoize, sleep, timer; | ||
abort = function() { | ||
return process.exit(-1); | ||
}; | ||
context.test("abort"); | ||
memoize = (function(_hash) { | ||
_hash = function(x) { | ||
return x.toString(); | ||
}; | ||
return function(fn, hash, memo) { | ||
if (hash == null) { | ||
hash = _hash; | ||
} | ||
if (memo == null) { | ||
memo = {}; | ||
} | ||
return function(x) { | ||
var _name; | ||
return memo[_name = hash(x)] != null ? memo[_name] : memo[_name] = fn(x); | ||
}; | ||
}; | ||
})(void 0); | ||
context.test("memoize"); | ||
timer = function(wait, action) { | ||
var id; | ||
id = setTimeout(action, wait); | ||
return function() { | ||
return clearTimeout(id); | ||
}; | ||
}; | ||
context.test("timer"); | ||
sleep = function(interval) { | ||
var promise; | ||
promise = require("when").promise; | ||
return promise(function(resolve, reject) { | ||
return timer(interval, function() { | ||
return resolve(); | ||
}); | ||
}); | ||
}; | ||
return context.test("sleep"); | ||
}); | ||
$.to = function(to, from) { | ||
if (from instanceof to) { | ||
return from; | ||
} else { | ||
return new to(from); | ||
} | ||
}; | ||
include = require("./object").include; | ||
$.abort = function() { | ||
return process.exit(-1); | ||
}; | ||
include(module.exports, require("./core")); | ||
$.memoize = function(fn, hash) { | ||
var memo; | ||
if (hash == null) { | ||
hash = function(object) { | ||
return object.toString(); | ||
}; | ||
} | ||
memo = {}; | ||
return function(thing) { | ||
var _name; | ||
return memo[_name = hash(thing)] != null ? memo[_name] : memo[_name] = fn(thing); | ||
}; | ||
}; | ||
include(module.exports, require("./logical")); | ||
include(module.exports, require("./numeric")); | ||
/* timer */ | ||
include(module.exports, require("./type")); | ||
$.timer = function(wait, action) { | ||
var id; | ||
id = setTimeout(action, wait); | ||
return function() { | ||
return clearTimeout(id); | ||
}; | ||
}; | ||
include(module.exports, require("./array")); | ||
basename = require("path").basename; | ||
include(module.exports, require("./crypto")); | ||
readdir = require("./fs").readdir; | ||
include(module.exports, require("./fs")); | ||
include = require("./object").include; | ||
include(module.exports, require("./object")); | ||
_ref = readdir(__dirname); | ||
for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
filename = _ref[_i]; | ||
_module = basename(filename, ".coffee"); | ||
if (_module !== "index") { | ||
include($, require("./" + _module)); | ||
} | ||
} | ||
include(module.exports, require("./string")); | ||
module.exports = $; | ||
}).call(this); |
@@ -1,25 +0,115 @@ | ||
// Generated by CoffeeScript 1.7.1 | ||
// Generated by CoffeeScript 1.8.0 | ||
(function() { | ||
var $, include, type, | ||
var assert, compose, curry, deep_equal, describe, _ref, _ref1, | ||
__slice = [].slice; | ||
$ = {}; | ||
_ref = require("./helpers"), describe = _ref.describe, assert = _ref.assert; | ||
type = require("./type"); | ||
_ref1 = require("./core"), compose = _ref1.compose, curry = _ref1.curry; | ||
$.include = include = function() { | ||
var key, mixin, mixins, object, value, _i, _len; | ||
object = arguments[0], mixins = 2 <= arguments.length ? __slice.call(arguments, 1) : []; | ||
for (_i = 0, _len = mixins.length; _i < _len; _i++) { | ||
mixin = mixins[_i]; | ||
for (key in mixin) { | ||
value = mixin[key]; | ||
object[key] = value; | ||
deep_equal = require("./type").deep_equal; | ||
describe("Object functions", function(context) { | ||
var clone, delegate, extend, include, liberate, merge, pluck, properties, property; | ||
include = extend = function() { | ||
var key, mixin, mixins, object, value, _i, _len; | ||
object = arguments[0], mixins = 2 <= arguments.length ? __slice.call(arguments, 1) : []; | ||
for (_i = 0, _len = mixins.length; _i < _len; _i++) { | ||
mixin = mixins[_i]; | ||
for (key in mixin) { | ||
value = mixin[key]; | ||
object[key] = value; | ||
} | ||
} | ||
} | ||
return object; | ||
}; | ||
$.Property = { | ||
property: (function() { | ||
return object; | ||
}; | ||
context.test("include"); | ||
merge = function() { | ||
var destination, k, object, objects, v, _i, _len; | ||
objects = 1 <= arguments.length ? __slice.call(arguments, 0) : []; | ||
destination = {}; | ||
for (_i = 0, _len = objects.length; _i < _len; _i++) { | ||
object = objects[_i]; | ||
for (k in object) { | ||
v = object[k]; | ||
destination[k] = v; | ||
} | ||
} | ||
return destination; | ||
}; | ||
context.test("merge"); | ||
clone = function(object) { | ||
var flags, key; | ||
if ((object == null) || typeof object !== 'object') { | ||
return object; | ||
} | ||
if (object instanceof Date) { | ||
return new Date(obj.getTime()); | ||
} | ||
if (object instanceof RegExp) { | ||
flags = ''; | ||
if (object.global != null) { | ||
flags += 'g'; | ||
} | ||
if (object.ignoreCase != null) { | ||
flags += 'i'; | ||
} | ||
if (object.multiline != null) { | ||
flags += 'm'; | ||
} | ||
if (object.sticky != null) { | ||
flags += 'y'; | ||
} | ||
return new RegExp(object.source, flags); | ||
} | ||
clone = new object.constructor(); | ||
for (key in object) { | ||
clone[key] = $.clone(object[key]); | ||
} | ||
return clone; | ||
}; | ||
context.test("clone"); | ||
pluck = curry(function(key, object) { | ||
return object[key]; | ||
}); | ||
context.test("pluck", function() { | ||
var a, baz_foo; | ||
a = { | ||
foo: 1, | ||
bar: 2, | ||
baz: { | ||
foo: 2 | ||
} | ||
}; | ||
assert((pluck("foo", a)) === 1); | ||
baz_foo = compose(pluck("foo"), pluck("baz")); | ||
return assert((baz_foo(a)) === 2); | ||
}); | ||
delegate = function(from, to) { | ||
var name, value, _results; | ||
_results = []; | ||
for (name in to) { | ||
value = to[name]; | ||
if ((type(value)) === "function") { | ||
_results.push((function(value) { | ||
return from[name] = function() { | ||
var args; | ||
args = 1 <= arguments.length ? __slice.call(arguments, 0) : []; | ||
return value.call.apply(value, [to].concat(__slice.call(args))); | ||
}; | ||
})(value)); | ||
} | ||
} | ||
return _results; | ||
}; | ||
context.test("delegate"); | ||
liberate = (function(f) { | ||
return f.bind.bind(f.call); | ||
})((function() {})); | ||
context.test("liberate", function() { | ||
var reverse; | ||
reverse = liberate(Array.prototype.reverse); | ||
return assert(deep_equal(reverse([1, 2, 3]), [3, 2, 1])); | ||
}); | ||
properties = property = (function() { | ||
var defaults; | ||
@@ -30,3 +120,3 @@ defaults = { | ||
}; | ||
return function(properties) { | ||
return function(object, properties) { | ||
var key, value, _results; | ||
@@ -37,74 +127,43 @@ _results = []; | ||
include(value, defaults); | ||
_results.push(Object.defineProperty(this.prototype, key, value)); | ||
_results.push(Object.defineProperty(object, key, value)); | ||
} | ||
return _results; | ||
}; | ||
})() | ||
}; | ||
})(); | ||
context.test("properties", function() { | ||
var A, a; | ||
A = (function() { | ||
function A() {} | ||
$.delegate = function(from, to) { | ||
var name, value, _results; | ||
_results = []; | ||
for (name in to) { | ||
value = to[name]; | ||
if ((type(value)) === "function") { | ||
_results.push((function(value) { | ||
return from[name] = function() { | ||
var args; | ||
args = 1 <= arguments.length ? __slice.call(arguments, 0) : []; | ||
return value.call.apply(value, [to].concat(__slice.call(args))); | ||
}; | ||
})(value)); | ||
} | ||
} | ||
return _results; | ||
}; | ||
properties(A.prototype, { | ||
foo: { | ||
get: function() { | ||
return this._foo; | ||
}, | ||
set: function(v) { | ||
return this._foo = v; | ||
} | ||
} | ||
}); | ||
$.merge = function() { | ||
var destination, k, object, objects, v, _i, _len; | ||
objects = 1 <= arguments.length ? __slice.call(arguments, 0) : []; | ||
destination = {}; | ||
for (_i = 0, _len = objects.length; _i < _len; _i++) { | ||
object = objects[_i]; | ||
for (k in object) { | ||
v = object[k]; | ||
destination[k] = v; | ||
} | ||
} | ||
return destination; | ||
}; | ||
return A; | ||
$.clone = function(object) { | ||
var clone, flags, key; | ||
if ((object == null) || typeof object !== 'object') { | ||
return object; | ||
} | ||
if (object instanceof Date) { | ||
return new Date(obj.getTime()); | ||
} | ||
if (object instanceof RegExp) { | ||
flags = ''; | ||
if (object.global != null) { | ||
flags += 'g'; | ||
} | ||
if (object.ignoreCase != null) { | ||
flags += 'i'; | ||
} | ||
if (object.multiline != null) { | ||
flags += 'm'; | ||
} | ||
if (object.sticky != null) { | ||
flags += 'y'; | ||
} | ||
return new RegExp(object.source, flags); | ||
} | ||
clone = new object.constructor(); | ||
for (key in object) { | ||
clone[key] = $.clone(object[key]); | ||
} | ||
return clone; | ||
}; | ||
})(); | ||
a = new A; | ||
a.foo = "bar"; | ||
assert(a.foo === "bar"); | ||
return assert(a._foo != null); | ||
}); | ||
return module.exports = { | ||
include: include, | ||
extend: extend, | ||
merge: merge, | ||
clone: clone, | ||
pluck: pluck, | ||
property: property, | ||
delegate: delegate, | ||
liberate: liberate | ||
}; | ||
}); | ||
module.exports = $; | ||
}).call(this); |
@@ -1,60 +0,93 @@ | ||
// Generated by CoffeeScript 1.7.1 | ||
// Generated by CoffeeScript 1.8.0 | ||
(function() { | ||
var $; | ||
var assert, describe, _ref; | ||
$ = {}; | ||
_ref = require("./helpers"), describe = _ref.describe, assert = _ref.assert; | ||
$.capitalize = function(string) { | ||
return string[0].toUpperCase() + string.slice(1); | ||
}; | ||
$.titleCase = function(string) { | ||
return string.toLowerCase().replace(/^(\w)|\W(\w)/g, function(char) { | ||
return char.toUpperCase(); | ||
describe("String functions", function(context) { | ||
var camel_case, capitalize, dashed, html_escape, plain_text, title_case, underscored, w; | ||
plain_text = function(string) { | ||
return string.replace(/^[A-Z]/g, function(c) { | ||
return c.toLowerCase(); | ||
}).replace(/[A-Z]/g, function(c) { | ||
return " " + (c.toLowerCase()); | ||
}).replace(/\W+/g, " "); | ||
}; | ||
context.test("plain_text", function() { | ||
assert(plain_text("hello-world") === "hello world"); | ||
return assert(plain_text("Hello World") === "hello world"); | ||
}); | ||
}; | ||
$.camelCase = $.camel_case = function(string) { | ||
return string.toLowerCase().replace(/(\W+\w)/g, function(string) { | ||
return string.trim().toUpperCase(); | ||
capitalize = function(string) { | ||
return string[0].toUpperCase() + string.slice(1); | ||
}; | ||
context.test("capitalize", function() { | ||
return assert(capitalize("hello world") === "Hello world"); | ||
}); | ||
}; | ||
$.underscored = function(string) { | ||
return $.plainText(string).replace(/\W+/g, "_"); | ||
}; | ||
$.dashed = function(string) { | ||
return $.plainText(string).replace(/\W+/g, "-"); | ||
}; | ||
$.plainText = $.plain_text = function(string) { | ||
return string.replace(/^[A-Z]/g, function(c) { | ||
return c.toLowerCase(); | ||
}).replace(/[A-Z]/g, function(c) { | ||
return " " + (c.toLowerCase()); | ||
}).replace(/\W+/g, " "); | ||
}; | ||
$.htmlEscape = $.html_escape = (function() { | ||
var entities, map, re; | ||
map = { | ||
"&": "&", | ||
"<": "<", | ||
">": ">", | ||
'"': '"', | ||
"'": ''', | ||
"/": '/' | ||
title_case = function(string) { | ||
return string.toLowerCase().replace(/^(\w)|\W(\w)/g, function(char) { | ||
return char.toUpperCase(); | ||
}); | ||
}; | ||
entities = Object.keys(map); | ||
re = new RegExp("" + (entities.join('|')), "g"); | ||
return function(string) { | ||
return string.replace(re, function(s) { | ||
return map[s]; | ||
context.test("title_case", function() { | ||
return assert(title_case("hello woRld") === "Hello World"); | ||
}); | ||
camel_case = function(string) { | ||
return string.toLowerCase().replace(/(\W+\w)/g, function(string) { | ||
return string.trim().toUpperCase(); | ||
}); | ||
}; | ||
})(); | ||
context.test("camel_case", function() { | ||
return assert(camel_case("Hello World") === "helloWorld"); | ||
}); | ||
underscored = function(string) { | ||
return plain_text(string).replace(/\W+/g, "_"); | ||
}; | ||
context.test("underscored", function() { | ||
return assert(underscored("Hello World") === "hello_world"); | ||
}); | ||
dashed = function(string) { | ||
return plain_text(string).replace(/\W+/g, "-"); | ||
}; | ||
context.test("dashed", function() { | ||
return assert(dashed("Hello World") === "hello-world"); | ||
}); | ||
html_escape = (function() { | ||
var entities, map, re; | ||
map = { | ||
"&": "&", | ||
"<": "<", | ||
">": ">", | ||
'"': '"', | ||
"'": ''', | ||
"/": '/' | ||
}; | ||
entities = Object.keys(map); | ||
re = new RegExp("" + (entities.join('|')), "g"); | ||
return function(string) { | ||
return string.replace(re, function(s) { | ||
return map[s]; | ||
}); | ||
}; | ||
})(); | ||
context.test("html_escape", function() { | ||
return assert.equal(html_escape("<a href='foo'>bar & baz</a>"), "<a href='foo'>bar & baz</a>"); | ||
}); | ||
w = function(string) { | ||
return string.trim().split(/\s+/); | ||
}; | ||
context.test("w", function() { | ||
return assert((w("one two three")).length === 3); | ||
}); | ||
return module.exports = { | ||
capitalize: capitalize, | ||
title_case: title_case, | ||
camel_case: camel_case, | ||
underscored: underscored, | ||
dashed: dashed, | ||
plain_text: plain_text, | ||
html_escape: html_escape, | ||
w: w | ||
}; | ||
}); | ||
module.exports = $; | ||
}).call(this); |
@@ -1,32 +0,31 @@ | ||
// Generated by CoffeeScript 1.7.1 | ||
// Generated by CoffeeScript 1.8.0 | ||
(function() { | ||
var classToType, classes, name, _i, _len; | ||
var assert, curry, deep_equal, describe, _ref, _ref1; | ||
classes = ["Boolean", "Number", "String", "Function", "Array", "Date", "RegExp"]; | ||
_ref = require("./core"), curry = _ref.curry, deep_equal = _ref.deep_equal; | ||
classToType = new Object; | ||
_ref1 = require("./helpers"), describe = _ref1.describe, assert = _ref1.assert; | ||
for (_i = 0, _len = classes.length; _i < _len; _i++) { | ||
name = classes[_i]; | ||
classToType["[object " + name + "]"] = name.toLowerCase(); | ||
} | ||
describe("Type functions", function(context) { | ||
var instance_of, is_type, type; | ||
type = function(x) { | ||
return Object.prototype.toString.call(x).slice(8, -1).toLowerCase(); | ||
}; | ||
context.test("type"); | ||
is_type = curry(function(t, x) { | ||
return type(x) === t; | ||
}); | ||
context.test("is_type"); | ||
instance_of = curry(function(t, x) { | ||
return x instanceof t; | ||
}); | ||
context.test("instance_of"); | ||
return module.exports = { | ||
deep_equal: deep_equal, | ||
type: type, | ||
is_type: is_type, | ||
instance_of: instance_of | ||
}; | ||
}); | ||
module.exports = { | ||
type: function(object) { | ||
var myClass; | ||
if (object === void 0) { | ||
return "undefined"; | ||
} | ||
if (object === null) { | ||
return "null"; | ||
} | ||
myClass = Object.prototype.toString.call(object); | ||
if (myClass in classToType) { | ||
return classToType[myClass]; | ||
} else { | ||
return "object"; | ||
} | ||
} | ||
}; | ||
}).call(this); |
{ | ||
"name": "fairmont", | ||
"version": "0.8.2", | ||
"version": "1.0.0-alpha-01", | ||
"description": "A collection of useful functions and utilities.", | ||
@@ -8,4 +8,2 @@ "files": [ | ||
"lib/", | ||
"docs.coffee", | ||
"test.coffee", | ||
"README.md" | ||
@@ -15,5 +13,5 @@ ], | ||
"scripts": { | ||
"prepublish": "coffee -o lib/ -c src/*.coffee && npm run docs", | ||
"docs": "coffee doc.coffee > README.md", | ||
"test": "coffee test/shred.coffee" | ||
"prepublish": "coffee -o lib/ -c src/*.*coffee", | ||
"test": "coffee ./test.coffee", | ||
"watch": "coffee --nodejs --harmony -o lib/ -cw src/*.*coffee" | ||
}, | ||
@@ -35,4 +33,8 @@ "repository": { | ||
"devDependencies": { | ||
"testify": "~0.2.4" | ||
} | ||
"amen": "^1.0.0-alpha-03" | ||
}, | ||
"dependencies": { | ||
"when": "^3.7.2" | ||
}, | ||
"engine": "node >= 0.11.x" | ||
} |
364
README.md
# Fairmont | ||
A collection of useful CoffeeScript/JavaScript functions. | ||
# | ||
## Array Functions ## | ||
# | ||
### remove ### | ||
# | ||
Destructively remove an element from an array. Returns the element removed. | ||
# | ||
```coffee-script | ||
a = w "foo bar baz" | ||
remove( a, "bar" ) | ||
``` | ||
### uniq ### | ||
# | ||
Takes an array and returns a new array with all duplicate values from the original array removed. Also takes an optional hash function that defaults to calling `toString` on the elements. | ||
# | ||
```coffee-script | ||
uniq [1,2,3,1,2,3,4,5,6,3,6,2,4] | ||
# returns [1,2,3,4,5,6] | ||
``` | ||
### shuffle ### | ||
# | ||
Takes an array and returns a new array with all values shuffled randomly. | ||
```coffee-script | ||
shuffle ["a", "b", "c", "d", "e", "f"] | ||
# for e.g.: returns ["b", "c", "d", "e", "f", "a"] | ||
``` | ||
# | ||
Use the [Fisher-Yates algorithm][shuffle-1]. | ||
# | ||
Adapted from the [CoffeeScript Cookbook][shuffle-2]. | ||
# | ||
[shuffle-1]:http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle | ||
[shuffle-2]:http://coffeescriptcookbook.com/chapters/arrays/shuffling-array-elements | ||
# | ||
## Hashing/Encoding Functions | ||
# | ||
# | ||
### md5 ### | ||
# | ||
Return the MD5 hash of a string. | ||
# | ||
```coffee-script | ||
nutshell = md5( myLifeStory ) | ||
``` | ||
# | ||
### base64 ### | ||
# | ||
Base64 encode a string. (Not URL safe.) | ||
# | ||
```coffee-script | ||
image = data: base64( imageData ) | ||
``` | ||
# | ||
A collection of useful CoffeeScript/JavaScript functions. These include functions to help with functional programming, arrays, objects, and more. It's intended as an alternative to Underscore. | ||
## Core Functions | ||
* [API Reference][core] | ||
identity, wrap, curry, _, partial, flip, | ||
compose, pipe, variadic, unary, binary, ternary | ||
[core]:src/core.litcoffee | ||
## Logical Functions | ||
* [API Reference][logical] | ||
f_and, f_or, f_not, f_eq, f_neq | ||
[logical]:src/logical.litcoffee | ||
## Numeric Functions | ||
* [API Reference][numeric] | ||
gt, lt, gte, lte, add, sub, mul, div, mod, | ||
even, odd, min, max | ||
[numeric]:src/numeric.litcoffee | ||
## Type Functions | ||
* [API Reference][core] | ||
deep_equal, type, is_type, instance_of | ||
[type]:src/type.litcoffee | ||
## Array functions | ||
* [API Reference][array] | ||
fold, foldr, map, filter, any, all, each, cat, slice, | ||
first, last, rest, take, leave, drop, includes, unique_by, | ||
unique, uniq, flatten, dupes, union, intersection, remove, shuffle | ||
[array]:src/array.litcoffee | ||
## Crypto Functions | ||
* [API Reference][crypto] | ||
md5, base64, base64url | ||
[crypto]:src/crypto.litcoffee | ||
## File System Functions | ||
# | ||
All file-system functions are based on Node's `fs` API. This is not `require`d unless the function is actually invoked. | ||
# | ||
### exists ### | ||
# | ||
Check to see if a file exists. | ||
# | ||
```coffee-script | ||
source = read( sourcePath ) if exists( sourcePath ) | ||
``` | ||
# | ||
### read ### | ||
# | ||
Read a file synchronously and return a UTF-8 string of the contents. | ||
# | ||
```coffee-script | ||
source = read( sourcePath ) if exists( sourcePath ) | ||
``` | ||
# | ||
### readdir ### | ||
# | ||
Synchronously get the contents of a directory as an array. | ||
# | ||
```coffee-script | ||
for file in readdir("documents") | ||
console.log read( file ) if stat( file ).isFile() | ||
``` | ||
# | ||
### stat ### | ||
# | ||
Synchronously get the stat object for a file. | ||
# | ||
```coffee-script | ||
for file in readdir("documents") | ||
console.log read( file ) if stat( file ).isFile() | ||
``` | ||
# | ||
### write ### | ||
# | ||
Synchronously write a UTF-8 string to a file. | ||
# | ||
```coffee-script | ||
write( file.replace( /foo/g, 'bar' ) ) | ||
``` | ||
# | ||
### chdir ### | ||
# | ||
Change directories, execute a function, and then restore the original working directory. | ||
# | ||
```coffee-script | ||
chdir "documents", -> | ||
console.log read( "README" ) | ||
``` | ||
# | ||
### rm ### | ||
# | ||
Removes a file. | ||
# | ||
```coffee-script | ||
rm "documents/reamde.txt" | ||
``` | ||
# | ||
### rmdir ### | ||
# | ||
Removes a directory. | ||
# | ||
```coffee-script | ||
rmdir "documents" | ||
``` | ||
# | ||
## General Purpose Functions ## | ||
# | ||
### w ### | ||
# | ||
Split a string on whitespace. Useful for concisely creating arrays of strings. | ||
# | ||
```coffee-script | ||
console.log word for word in w "foo bar baz" | ||
``` | ||
### to ### | ||
# | ||
Hoist a value to a given type if it isn't already. Useful when you want to wrap a value without having to check to see if it's already wrapped. | ||
# | ||
For example, to hoist an error message into an error, you would use: | ||
# | ||
```coffee-script | ||
to(error, Error) | ||
``` | ||
### abort ### | ||
# | ||
Simple wrapper around `process.exit(-1)`. | ||
# | ||
### memoize ### | ||
# | ||
A very simple way to cache results of functions that take a single argument. Also takes an optional hash function that defaults to calling `toString` on the function's argument. | ||
# | ||
```coffee-script | ||
nickname = (email) -> | ||
expensiveLookupToGetNickname( email ) | ||
# | ||
memoize( nickname ) | ||
``` | ||
# | ||
### timer ### | ||
# | ||
Set a timer. Takes an interval in microseconds and an action. Returns a function to cancel the timer. Basically, a more convenient way to call `setTimeout` and `clearTimeout`. | ||
# | ||
```coffee-script | ||
cancel = timer 1000, -> console.log "Done" | ||
cancel() | ||
``` | ||
# | ||
* [API Reference][fs] | ||
exists, stat, read, readdir, write, chdir, rm, rmdir | ||
[fs]:src/fs.litcoffee | ||
## Object Functions | ||
# | ||
# | ||
### include ### | ||
# | ||
Adds the properties of one or more objects to another. | ||
# | ||
```coffee-script | ||
include( @, ScrollbarMixin, SidebarMixin ) | ||
``` | ||
# | ||
### property ### | ||
# | ||
Add a `property` method to a class, making it easier to define getters and setters on its prototype. | ||
# | ||
```coffee-script | ||
class Foo | ||
include @, Property | ||
property "foo", get: -> @_foo, set: (v) -> @_foo = v | ||
``` | ||
# | ||
Properties defined using `property` are enumerable. | ||
# | ||
### delegate ### | ||
# | ||
Delegates from one object to another by creating functions in the first object that call the second. | ||
# | ||
```coffee-script | ||
delegate( aProxy, aServer ) | ||
``` | ||
# | ||
### merge ### | ||
# | ||
Creates new object by progressively adding the properties of each given object. | ||
# | ||
```coffee-script | ||
options = merge( defaults, globalOptions, localOptions ) | ||
``` | ||
# | ||
### clone ### | ||
# | ||
Perform a deep clone on an object. Taken from [The CoffeeScript Cookboox][clone-1]. | ||
# | ||
[clone-1]:http://coffeescriptcookbook.com/chapters/classes_and_objects/cloning | ||
# | ||
```coffee-script | ||
copy = clone original | ||
``` | ||
# | ||
## String Functions ## | ||
# | ||
# | ||
### capitalize ### | ||
# | ||
Capitalize the first letter of a string. | ||
# | ||
### title_case ### | ||
# | ||
Capitalize the first letter of each word in a string. | ||
# | ||
### camel_case ### | ||
# | ||
Convert a sequence of words into a camel-cased string. | ||
# | ||
```coffee-script | ||
# yields fooBarBaz | ||
camel_case "foo bar baz" | ||
``` | ||
# | ||
### underscored ### | ||
# | ||
Convert a sequence of words into an underscore-separated string. | ||
# | ||
```coffee-script | ||
# yields foo_bar_baz | ||
underscored "foo bar baz" | ||
``` | ||
# | ||
### dashed ### | ||
# | ||
Convert a sequence of words into a dash-separated string. | ||
# | ||
```coffee-script | ||
# yields foo-bar-baz | ||
dashed "foo bar baz" | ||
``` | ||
# | ||
### plain_text ### | ||
# | ||
Convert an camel-case or underscore- or dash-separated string into a | ||
whitespace separated string. | ||
# | ||
```coffee-script | ||
# all of the following yield foo bar baz | ||
plain_text "fooBarBaz" | ||
plain_text "foo-bar-baz" | ||
plain_text "foo_bar_baz" | ||
``` | ||
# | ||
### html_escape ### | ||
# | ||
Escape a string so that it can be embedded into HTML. Adapted from Mustache.js. | ||
# | ||
## Type Functions ## | ||
# | ||
### type ### | ||
# | ||
Get the type of a value. Possible values are: `number`, `string`, '`boolean`, `data`, `regexp`, `function`, `array`, `object`, `null`, `undefined`. Adapted from [The CoffeeScript Cookbook][type-0] and based on Douglas Crockford's [remedial JavaScript blog post][type-1]. | ||
[type-0]:http://coffeescriptcookbook.com/chapters/classes_and_objects/type-function | ||
[type-1]:http://javascript.crockford.com/remedial.html | ||
# | ||
```coffee-script | ||
foo() if type( foo ) == "function" | ||
``` | ||
* [API Reference][object] | ||
include/extend, merge, clone, pluck, property, delegate, liberate | ||
[object]:src/object.litcoffee | ||
## String Functions | ||
* [API Reference][string] | ||
capitalize, title_case, camel_case, underscored, | ||
dashed, plain_text, html_escape, w | ||
[string]:src/string.litcoffee |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
58149
24
1095
2
1
90
+ Addedwhen@^3.7.2
+ Addedwhen@3.7.8(transitive)