sproutit-narwhal 0.1.106
Sign up to get free protection for your applications and to get access to all the features.
- data/DISTRIBUTION.yml +15 -0
- data/README.md +86 -0
- data/Rakefile +349 -0
- data/VERSION.yml +7 -0
- data/bin/activate +50 -0
- data/bin/activate.bash +50 -0
- data/bin/activate.cmd +3 -0
- data/bin/js +67 -0
- data/bin/json +2 -0
- data/bin/narwhal +67 -0
- data/bin/narwhal.cmd +29 -0
- data/bin/sea +45 -0
- data/bin/sea.cmd +25 -0
- data/bin/tusk +2 -0
- data/bin/tusk.cmd +5 -0
- data/catalog.json +902 -0
- data/docs/available-packages.md +32 -0
- data/docs/browser-api-plan.md +290 -0
- data/docs/browser-api.md +153 -0
- data/docs/download.md +25 -0
- data/docs/engines.md +32 -0
- data/docs/json-tool.md +121 -0
- data/docs/lib/binary.wiki +242 -0
- data/docs/lib/file.wiki +325 -0
- data/docs/lib/os/popen.md +70 -0
- data/docs/modules.md +38 -0
- data/docs/narwhal.md +487 -0
- data/docs/packages-howto.md +32 -0
- data/docs/packages.md +30 -0
- data/docs/posts/2009-07-29-hello-0.1.md +19 -0
- data/docs/quick-start.md +69 -0
- data/docs/sea.md +49 -0
- data/engines/browser/lib/binary.js +2 -0
- data/engines/browser/lib/reactor.js +21 -0
- data/engines/browser/lib/system.js +3 -0
- data/engines/default/lib/array.js +164 -0
- data/engines/default/lib/binary-engine.js +53 -0
- data/engines/default/lib/binary.js +755 -0
- data/engines/default/lib/date.js +8 -0
- data/engines/default/lib/file-engine.js +119 -0
- data/engines/default/lib/function.js +119 -0
- data/engines/default/lib/global.js +11 -0
- data/engines/default/lib/io-engine.js +26 -0
- data/engines/default/lib/json.js +488 -0
- data/engines/default/lib/object.js +69 -0
- data/engines/default/lib/os-engine.js +3 -0
- data/engines/default/lib/reactor.js +12 -0
- data/engines/default/lib/string.js +84 -0
- data/engines/default/lib/system.js +20 -0
- data/engines/default/lib/worker.js +133 -0
- data/engines/jsc/README.md +18 -0
- data/engines/jsc/bootstrap.js +53 -0
- data/engines/jsc/deps/http-parser/LICENSE +77 -0
- data/engines/jsc/deps/http-parser/README.md +145 -0
- data/engines/jsc/deps/http-parser/http_parser.c +6087 -0
- data/engines/jsc/deps/http-parser/http_parser.h +141 -0
- data/engines/jsc/deps/http-parser/http_parser.rl +500 -0
- data/engines/jsc/deps/http-parser/test.c +858 -0
- data/engines/jsc/include/binary-engine.h +11 -0
- data/engines/jsc/include/io-engine.h +23 -0
- data/engines/jsc/include/narwhal.h +427 -0
- data/engines/jsc/lib/file-engine.js +31 -0
- data/engines/jsc/lib/http.js +1 -0
- data/engines/jsc/lib/io-engine.js +202 -0
- data/engines/jsc/lib/os-engine.js +25 -0
- data/engines/jsc/lib/system.js +18 -0
- data/engines/jsc/lib/zip.js +1 -0
- data/engines/jsc/narwhal-jsc.c +273 -0
- data/engines/jsc/narwhal.c +29 -0
- data/engines/jsc/package.json +8 -0
- data/engines/jsc/src/binary-engine.cc +290 -0
- data/engines/jsc/src/file-engine.cc +405 -0
- data/engines/jsc/src/io-engine.cc +423 -0
- data/engines/jsc/src/jack/handler/jill.cc +710 -0
- data/engines/jsc/src/os-engine.cc +210 -0
- data/engines/rhino/bin/narwhal-rhino +68 -0
- data/engines/rhino/bin/narwhal-rhino.cmd +34 -0
- data/engines/rhino/bootstrap.js +119 -0
- data/engines/rhino/jars/jline.jar +0 -0
- data/engines/rhino/jars/jna.jar +0 -0
- data/engines/rhino/jars/js.jar +0 -0
- data/engines/rhino/lib/binary-engine.js +83 -0
- data/engines/rhino/lib/concurrency.js +6 -0
- data/engines/rhino/lib/event-queue.js +18 -0
- data/engines/rhino/lib/file-engine.js +216 -0
- data/engines/rhino/lib/http-client-engine.js +90 -0
- data/engines/rhino/lib/http-engine.js +10 -0
- data/engines/rhino/lib/io-engine.js +347 -0
- data/engines/rhino/lib/md5-engine.js +40 -0
- data/engines/rhino/lib/os-engine.js +150 -0
- data/engines/rhino/lib/packages-engine.js +71 -0
- data/engines/rhino/lib/sandbox-engine.js +70 -0
- data/engines/rhino/lib/system.js +38 -0
- data/engines/rhino/lib/worker-engine.js +23 -0
- data/engines/rhino/lib/zip.js +78 -0
- data/engines/rhino/package.json +4 -0
- data/engines/secure/lib/file.js +6 -0
- data/engines/secure/lib/system.js +6 -0
- data/engines/template/bin/narwhal-engine-name +32 -0
- data/engines/template/bootstrap.js +40 -0
- data/engines/template/lib/file-engine.js +118 -0
- data/engines/template/lib/system.js +17 -0
- data/examples/browser-deployment-jackconfig.js +35 -0
- data/examples/fibonacci-worker.js +35 -0
- data/examples/fibonacci.js +19 -0
- data/examples/hello +2 -0
- data/examples/narwhal +3 -0
- data/examples/not-quite-a-quine.js +1 -0
- data/extconf.rb +44 -0
- data/gem_bin/narwhal +5 -0
- data/gem_bin/sea +4 -0
- data/gem_bin/tusk +4 -0
- data/lib/args.js +849 -0
- data/lib/base16.js +16 -0
- data/lib/base64.js +120 -0
- data/lib/codec/base64.js +8 -0
- data/lib/crc32.js +60 -0
- data/lib/file-bootstrap.js +187 -0
- data/lib/file.js +659 -0
- data/lib/hash.js +28 -0
- data/lib/hashp.js +65 -0
- data/lib/html.js +16 -0
- data/lib/http-client.js +134 -0
- data/lib/http.js +17 -0
- data/lib/io.js +98 -0
- data/lib/jsmin.js +315 -0
- data/lib/jsonpath.js +89 -0
- data/lib/logger.js +55 -0
- data/lib/md4.js +146 -0
- data/lib/md5.js +164 -0
- data/lib/mime.js +166 -0
- data/lib/narwhal.js +102 -0
- data/lib/narwhal/client.js +261 -0
- data/lib/narwhal/compile.js +99 -0
- data/lib/narwhal/env.js +140 -0
- data/lib/narwhal/inline.js +106 -0
- data/lib/narwhal/json.js +324 -0
- data/lib/narwhal/json.md +178 -0
- data/lib/narwhal/repl.js +96 -0
- data/lib/narwhal/server-test.js +6 -0
- data/lib/narwhal/server.js +270 -0
- data/lib/narwhal/tusk.js +170 -0
- data/lib/narwhal/tusk/bin.js +13 -0
- data/lib/narwhal/tusk/bundle.js +0 -0
- data/lib/narwhal/tusk/catalog.js +22 -0
- data/lib/narwhal/tusk/clone.js +66 -0
- data/lib/narwhal/tusk/consolidate.js +25 -0
- data/lib/narwhal/tusk/create-catalog.js +80 -0
- data/lib/narwhal/tusk/engine.js +42 -0
- data/lib/narwhal/tusk/freeze.js +0 -0
- data/lib/narwhal/tusk/init.js +56 -0
- data/lib/narwhal/tusk/install.js +288 -0
- data/lib/narwhal/tusk/list.js +20 -0
- data/lib/narwhal/tusk/orphans.js +0 -0
- data/lib/narwhal/tusk/reheat.js +15 -0
- data/lib/narwhal/tusk/remove.js +15 -0
- data/lib/narwhal/tusk/search.js +145 -0
- data/lib/narwhal/tusk/update.js +21 -0
- data/lib/narwhal/tusk/upgrade.js +0 -0
- data/lib/os.js +33 -0
- data/lib/packages.js +423 -0
- data/lib/printf.js +169 -0
- data/lib/promise.js +352 -0
- data/lib/querystring.js +176 -0
- data/lib/ref-send.js +257 -0
- data/lib/regexp.js +12 -0
- data/lib/sandbox.js +422 -0
- data/lib/sha.js +112 -0
- data/lib/sha256.js +102 -0
- data/lib/struct.js +228 -0
- data/lib/term.js +179 -0
- data/lib/test/assert.js +95 -0
- data/lib/test/equiv.js +188 -0
- data/lib/test/jsdump.js +165 -0
- data/lib/test/runner.js +129 -0
- data/lib/unload.js +13 -0
- data/lib/uri.js +378 -0
- data/lib/url.js +5 -0
- data/lib/utf8.js +64 -0
- data/lib/util.js +985 -0
- data/lib/uuid.js +89 -0
- data/lib/xregexp.js +521 -0
- data/local.json.template +1 -0
- data/narwhal.gemspec +105 -0
- data/narwhal.js +213 -0
- data/package.json +26 -0
- data/packages/readline/engines/default/lib/readline.js +4 -0
- data/packages/readline/engines/rhino/lib/readline.js +6 -0
- data/packages/readline/package.json +5 -0
- data/sources.json +207 -0
- data/tests/all-tests.js +17 -0
- data/tests/args.js +31 -0
- data/tests/args/domain.js +215 -0
- data/tests/args/options.js +36 -0
- data/tests/args/shifting.js +92 -0
- data/tests/args/validation.js +31 -0
- data/tests/base64.js +23 -0
- data/tests/commonjs.js +3 -0
- data/tests/commonjs/all-tests.js +12 -0
- data/tests/commonjs/bytearray-encodings-tests.js +69 -0
- data/tests/commonjs/bytearray-tests.js +465 -0
- data/tests/commonjs/bytestring-encodings-tests.js +89 -0
- data/tests/commonjs/bytestring-tests.js +263 -0
- data/tests/commonjs/es5/all-tests.js +3 -0
- data/tests/commonjs/es5/bind.js +29 -0
- data/tests/commonjs/file-tests.js +315 -0
- data/tests/commonjs/file/dirname.js +31 -0
- data/tests/commonjs/file/extension.js +45 -0
- data/tests/commonjs/file/is-absolute.js +11 -0
- data/tests/commonjs/file/iterator.js +101 -0
- data/tests/commonjs/file/normal.js +27 -0
- data/tests/commonjs/file/path.js +17 -0
- data/tests/commonjs/file/relative.js +42 -0
- data/tests/commonjs/file/resolve.js +44 -0
- data/tests/commonjs/module-tests.js +9 -0
- data/tests/commonjs/modules/absolute/b.js +1 -0
- data/tests/commonjs/modules/absolute/program.js +5 -0
- data/tests/commonjs/modules/absolute/submodule/a.js +3 -0
- data/tests/commonjs/modules/absolute/test.js +9 -0
- data/tests/commonjs/modules/all-tests.js +47 -0
- data/tests/commonjs/modules/config.js +11 -0
- data/tests/commonjs/modules/cyclic/a.js +4 -0
- data/tests/commonjs/modules/cyclic/b.js +4 -0
- data/tests/commonjs/modules/cyclic/program.js +10 -0
- data/tests/commonjs/modules/cyclic/test.js +9 -0
- data/tests/commonjs/modules/determinism/program.js +3 -0
- data/tests/commonjs/modules/determinism/submodule/a.js +8 -0
- data/tests/commonjs/modules/determinism/submodule/b.js +2 -0
- data/tests/commonjs/modules/determinism/test.js +9 -0
- data/tests/commonjs/modules/exactExports/a.js +3 -0
- data/tests/commonjs/modules/exactExports/program.js +4 -0
- data/tests/commonjs/modules/exactExports/test.js +9 -0
- data/tests/commonjs/modules/hasOwnProperty/hasOwnProperty.js +0 -0
- data/tests/commonjs/modules/hasOwnProperty/program.js +3 -0
- data/tests/commonjs/modules/hasOwnProperty/test.js +9 -0
- data/tests/commonjs/modules/hasOwnProperty/toString.js +0 -0
- data/tests/commonjs/modules/method/a.js +12 -0
- data/tests/commonjs/modules/method/program.js +8 -0
- data/tests/commonjs/modules/method/test.js +9 -0
- data/tests/commonjs/modules/missing/program.js +8 -0
- data/tests/commonjs/modules/missing/test.js +9 -0
- data/tests/commonjs/modules/monkeys/a.js +1 -0
- data/tests/commonjs/modules/monkeys/program.js +4 -0
- data/tests/commonjs/modules/monkeys/test.js +9 -0
- data/tests/commonjs/modules/nested/a/b/c/d.js +3 -0
- data/tests/commonjs/modules/nested/program.js +3 -0
- data/tests/commonjs/modules/nested/test.js +9 -0
- data/tests/commonjs/modules/relative/program.js +5 -0
- data/tests/commonjs/modules/relative/submodule/a.js +1 -0
- data/tests/commonjs/modules/relative/submodule/b.js +2 -0
- data/tests/commonjs/modules/relative/test.js +9 -0
- data/tests/commonjs/modules/transitive/a.js +1 -0
- data/tests/commonjs/modules/transitive/b.js +1 -0
- data/tests/commonjs/modules/transitive/c.js +3 -0
- data/tests/commonjs/modules/transitive/program.js +3 -0
- data/tests/commonjs/modules/transitive/test.js +9 -0
- data/tests/file/all-tests.js +61 -0
- data/tests/file/fnmatch.js +102 -0
- data/tests/file/glob.js +466 -0
- data/tests/file/match.js +102 -0
- data/tests/global.js +6 -0
- data/tests/global/array.js +19 -0
- data/tests/hashes.js +94 -0
- data/tests/html.js +13 -0
- data/tests/io/stringio.js +21 -0
- data/tests/os/all-tests.js +4 -0
- data/tests/os/popen.js +41 -0
- data/tests/os/system.js +22 -0
- data/tests/printf.js +123 -0
- data/tests/query-string.js +87 -0
- data/tests/sandbox/byte-io.js +20 -0
- data/tests/sandbox/fileName.js +3 -0
- data/tests/sandbox/foo.js +0 -0
- data/tests/sandbox/reload.js +79 -0
- data/tests/string.js +35 -0
- data/tests/uri.js +41 -0
- data/tests/util/all-tests.js +79 -0
- data/tests/util/array.js +207 -0
- data/tests/util/array/is-arguments.js +29 -0
- data/tests/util/array/is-array-like.js +29 -0
- data/tests/util/case.js +9 -0
- data/tests/util/collection.js +104 -0
- data/tests/util/eq.js +57 -0
- data/tests/util/expand.js +45 -0
- data/tests/util/object.js +125 -0
- data/tests/util/operator.js +25 -0
- data/tests/util/range.js +19 -0
- data/tests/util/repr.js +26 -0
- data/tests/util/string.js +34 -0
- data/tests/util/unique.js +12 -0
- metadata +434 -0
data/lib/url.js
ADDED
data/lib/utf8.js
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
|
2
|
+
var binary = require('./binary');
|
3
|
+
|
4
|
+
/*** encode
|
5
|
+
converts a Unicode character string into a UTF-8
|
6
|
+
character stream.
|
7
|
+
*/
|
8
|
+
exports.encode = function (string) {
|
9
|
+
string = string.replace(/\r\n/g,"\n");
|
10
|
+
var utftext = [];
|
11
|
+
|
12
|
+
for (var n = 0; n < string.length; n++) {
|
13
|
+
|
14
|
+
var c = string.charCodeAt(n);
|
15
|
+
|
16
|
+
if (c < 128) {
|
17
|
+
utftext.push(c);
|
18
|
+
}
|
19
|
+
else if((c > 127) && (c < 2048)) {
|
20
|
+
utftext.push((c >> 6) | 192);
|
21
|
+
utftext.push((c & 63) | 128);
|
22
|
+
}
|
23
|
+
else {
|
24
|
+
utftext.push((c >> 12) | 224);
|
25
|
+
utftext.push(((c >> 6) & 63) | 128);
|
26
|
+
utftext.push((c & 63) | 128);
|
27
|
+
}
|
28
|
+
|
29
|
+
}
|
30
|
+
|
31
|
+
return binary.ByteString(utftext);
|
32
|
+
};
|
33
|
+
|
34
|
+
/*** decode
|
35
|
+
Converts a UTF-8 character string into a
|
36
|
+
Unicode character string.
|
37
|
+
*/
|
38
|
+
exports.decode = function (utftext) {
|
39
|
+
var string = "";
|
40
|
+
var i = 0;
|
41
|
+
var c = c1 = c2 = 0;
|
42
|
+
|
43
|
+
while (i < utftext.length) {
|
44
|
+
|
45
|
+
c = utftext.charCodeAt(i);
|
46
|
+
|
47
|
+
if (c < 128) {
|
48
|
+
string += String.fromCharCode(c);
|
49
|
+
i++;
|
50
|
+
} else if ((c > 191) && (c < 224)) {
|
51
|
+
c2 = utftext.charCodeAt(i+1);
|
52
|
+
string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
|
53
|
+
i += 2;
|
54
|
+
} else {
|
55
|
+
c2 = utftext.charCodeAt(i+1);
|
56
|
+
c3 = utftext.charCodeAt(i+2);
|
57
|
+
string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
|
58
|
+
i += 3;
|
59
|
+
}
|
60
|
+
}
|
61
|
+
|
62
|
+
return string;
|
63
|
+
};
|
64
|
+
|
data/lib/util.js
ADDED
@@ -0,0 +1,985 @@
|
|
1
|
+
|
2
|
+
// a decorator for functions that curry "polymorphically",
|
3
|
+
// that is, that return a function that can be tested
|
4
|
+
// against various objects if they're only "partially
|
5
|
+
// completed", or fewer arguments than needed are used.
|
6
|
+
//
|
7
|
+
// this enables the idioms:
|
8
|
+
// [1, 2, 3].every(lt(4)) eq true
|
9
|
+
// [1, 2, 3].map(add(1)) eq [2, 3, 4]
|
10
|
+
// [{}, {}, {}].forEach(set('a', 10))
|
11
|
+
//
|
12
|
+
exports.operator = function (name, length, block) {
|
13
|
+
var operator = function () {
|
14
|
+
var args = exports.array(arguments);
|
15
|
+
var completion = function (object) {
|
16
|
+
if (
|
17
|
+
typeof object == "object" &&
|
18
|
+
object !== null && // seriously? typeof null == "object"
|
19
|
+
name in object && // would throw if object === null
|
20
|
+
// not interested in literal objects:
|
21
|
+
!Object.prototype.hasOwnProperty.call(object, name)
|
22
|
+
)
|
23
|
+
return object[name].apply(object, args);
|
24
|
+
return block.apply(
|
25
|
+
this,
|
26
|
+
[object].concat(args)
|
27
|
+
);
|
28
|
+
};
|
29
|
+
if (arguments.length < length) {
|
30
|
+
// polymoprhic curry, delayed completion
|
31
|
+
return completion;
|
32
|
+
} else {
|
33
|
+
// immediate completion
|
34
|
+
return completion.call(this, args.shift());
|
35
|
+
}
|
36
|
+
};
|
37
|
+
operator.name = name;
|
38
|
+
operator.displayName = name;
|
39
|
+
operator.length = length;
|
40
|
+
operator.operator = block;
|
41
|
+
return operator;
|
42
|
+
};
|
43
|
+
|
44
|
+
exports.no = function (value) {
|
45
|
+
return value === null || value === undefined;
|
46
|
+
};
|
47
|
+
|
48
|
+
// object
|
49
|
+
|
50
|
+
exports.object = exports.operator('toObject', 1, function (object) {
|
51
|
+
var items = object;
|
52
|
+
if (!items.length)
|
53
|
+
items = exports.items(object);
|
54
|
+
var copy = {};
|
55
|
+
for (var i = 0; i < items.length; i++) {
|
56
|
+
var item = items[i];
|
57
|
+
var key = item[0];
|
58
|
+
var value = item[1];
|
59
|
+
copy[key] = value;
|
60
|
+
}
|
61
|
+
return copy;
|
62
|
+
});
|
63
|
+
|
64
|
+
exports.object.copy = function (object) {
|
65
|
+
var copy = {};
|
66
|
+
exports.object.keys(object).forEach(function (key) {
|
67
|
+
copy[key] = object[key];
|
68
|
+
});
|
69
|
+
return copy;
|
70
|
+
};
|
71
|
+
|
72
|
+
exports.object.deepCopy = function (object) {
|
73
|
+
var copy = {};
|
74
|
+
exports.object.keys(object).forEach(function (key) {
|
75
|
+
copy[key] = exports.deepCopy(object[key]);
|
76
|
+
});
|
77
|
+
return copy;
|
78
|
+
};
|
79
|
+
|
80
|
+
exports.object.eq = function (a, b, stack) {
|
81
|
+
return (
|
82
|
+
!exports.no(a) && !exports.no(b) &&
|
83
|
+
exports.array.eq(
|
84
|
+
exports.sort(exports.object.keys(a)),
|
85
|
+
exports.sort(exports.object.keys(b))
|
86
|
+
) &&
|
87
|
+
exports.object.keys(a).every(function (key) {
|
88
|
+
return exports.eq(a[key], b[key], stack);
|
89
|
+
})
|
90
|
+
);
|
91
|
+
};
|
92
|
+
|
93
|
+
exports.object.len = function (object) {
|
94
|
+
return exports.object.keys(object).length;
|
95
|
+
};
|
96
|
+
|
97
|
+
exports.object.has = function (object, key) {
|
98
|
+
return Object.prototype.hasOwnProperty.call(object, key);
|
99
|
+
};
|
100
|
+
|
101
|
+
exports.object.keys = function (object) {
|
102
|
+
var keys = [];
|
103
|
+
for (var key in object) {
|
104
|
+
if (exports.object.has(object, key))
|
105
|
+
keys.push(key);
|
106
|
+
}
|
107
|
+
return keys;
|
108
|
+
};
|
109
|
+
|
110
|
+
exports.object.values = function (object) {
|
111
|
+
var values = [];
|
112
|
+
exports.object.keys(object).forEach(function (key) {
|
113
|
+
values.push(object[key]);
|
114
|
+
});
|
115
|
+
return values;
|
116
|
+
};
|
117
|
+
|
118
|
+
exports.object.items = function (object) {
|
119
|
+
var items = [];
|
120
|
+
exports.object.keys(object).forEach(function (key) {
|
121
|
+
items.push([key, object[key]]);
|
122
|
+
});
|
123
|
+
return items;
|
124
|
+
};
|
125
|
+
|
126
|
+
exports.object.update = function (target, source) {
|
127
|
+
for (var key in source) {
|
128
|
+
if (exports.object.has(source, key)) {
|
129
|
+
target[key] = source[key];
|
130
|
+
}
|
131
|
+
}
|
132
|
+
};
|
133
|
+
|
134
|
+
exports.object.complete = function (target, source) {
|
135
|
+
for (var key in source) {
|
136
|
+
if (
|
137
|
+
exports.object.has(source, key) &&
|
138
|
+
!exports.object.has(target, key)
|
139
|
+
) {
|
140
|
+
target[key] = source[key];
|
141
|
+
}
|
142
|
+
}
|
143
|
+
};
|
144
|
+
|
145
|
+
exports.object.repr = function (object) {
|
146
|
+
return "{" +
|
147
|
+
exports.object.keys(object)
|
148
|
+
.map(function (key) {
|
149
|
+
return exports.enquote(key) + ": " +
|
150
|
+
exports.repr(object[key]);
|
151
|
+
}).join(", ") +
|
152
|
+
"}";
|
153
|
+
};
|
154
|
+
|
155
|
+
// array
|
156
|
+
|
157
|
+
exports.array = function (array) {
|
158
|
+
if (exports.no(array))
|
159
|
+
return [];
|
160
|
+
if (!exports.isArrayLike(array)) {
|
161
|
+
if (
|
162
|
+
array.toArray &&
|
163
|
+
!Object.prototype.hasOwnProperty.call(array, 'toArray')
|
164
|
+
) {
|
165
|
+
return array.toArray();
|
166
|
+
} else if (
|
167
|
+
array.forEach &&
|
168
|
+
!Object.prototype.hasOwnProperty.call(array, 'forEach')
|
169
|
+
) {
|
170
|
+
var results = [];
|
171
|
+
array.forEach(function (value) {
|
172
|
+
results.push(value);
|
173
|
+
});
|
174
|
+
return results;
|
175
|
+
} else {
|
176
|
+
return exports.items(array);
|
177
|
+
}
|
178
|
+
}
|
179
|
+
return Array.prototype.slice.call(array);
|
180
|
+
};
|
181
|
+
|
182
|
+
exports.array.coerce = function (array) {
|
183
|
+
if (!Array.isArray(array))
|
184
|
+
return exports.array(array);
|
185
|
+
return array;
|
186
|
+
};
|
187
|
+
|
188
|
+
exports.isArrayLike = function(object) {
|
189
|
+
return Array.isArray(object) || exports.isArguments(object);
|
190
|
+
};
|
191
|
+
|
192
|
+
// from http://code.google.com/p/google-caja/wiki/NiceNeighbor
|
193
|
+
// by "kangax"
|
194
|
+
//
|
195
|
+
// Mark Miller posted a solution that will work in ES5 compliant
|
196
|
+
// implementations, that may provide future insight:
|
197
|
+
// (http://groups.google.com/group/narwhaljs/msg/116097568bae41c6)
|
198
|
+
exports.isArguments = function (object) {
|
199
|
+
// ES5 reliable positive
|
200
|
+
if (Object.prototype.toString.call(object) == "[object Arguments]")
|
201
|
+
return true;
|
202
|
+
// for ES5, we will still need a way to distinguish false negatives
|
203
|
+
// from the following code (in ES5, it is possible to create
|
204
|
+
// an object that satisfies all of these constraints but is
|
205
|
+
// not an Arguments object).
|
206
|
+
// callee should exist
|
207
|
+
if (
|
208
|
+
!typeof object == "object" ||
|
209
|
+
!Object.prototype.hasOwnProperty.call(object, 'callee') ||
|
210
|
+
!object.callee ||
|
211
|
+
// It should be a Function object ([[Class]] === 'Function')
|
212
|
+
Object.prototype.toString.call(object.callee) !== '[object Function]' ||
|
213
|
+
typeof object.length != 'number'
|
214
|
+
)
|
215
|
+
return false;
|
216
|
+
for (var name in object) {
|
217
|
+
// both "callee" and "length" should be { DontEnum }
|
218
|
+
if (name === 'callee' || name === 'length') return false;
|
219
|
+
}
|
220
|
+
return true;
|
221
|
+
};
|
222
|
+
|
223
|
+
exports.array.copy = exports.array;
|
224
|
+
|
225
|
+
exports.array.deepCopy = function (array) {
|
226
|
+
return array.map(exports.deepCopy);
|
227
|
+
};
|
228
|
+
|
229
|
+
exports.array.len = function (array) {
|
230
|
+
return array.length;
|
231
|
+
};
|
232
|
+
|
233
|
+
exports.array.has = function (array, value) {
|
234
|
+
return Array.prototype.indexOf.call(array, value) >= 0;
|
235
|
+
};
|
236
|
+
|
237
|
+
exports.array.put = function (array, key, value) {
|
238
|
+
array.splice(key, 0, value);
|
239
|
+
return array;
|
240
|
+
};
|
241
|
+
|
242
|
+
exports.array.del = function (array, begin, end) {
|
243
|
+
array.splice(begin, end === undefined ? 1 : (end - begin));
|
244
|
+
return array;
|
245
|
+
};
|
246
|
+
|
247
|
+
exports.array.eq = function (a, b, stack) {
|
248
|
+
return exports.isArrayLike(b) &&
|
249
|
+
a.length == b.length &&
|
250
|
+
exports.zip(a, b).every(exports.apply(function (a, b) {
|
251
|
+
return exports.eq(a, b, stack);
|
252
|
+
}));
|
253
|
+
};
|
254
|
+
|
255
|
+
exports.array.lt = function (a, b) {
|
256
|
+
var length = Math.max(a.length, b.length);
|
257
|
+
for (var i = 0; i < length; i++)
|
258
|
+
if (!exports.eq(a[i], b[i]))
|
259
|
+
return exports.lt(a[i], b[i]);
|
260
|
+
return false;
|
261
|
+
};
|
262
|
+
|
263
|
+
exports.array.repr = function (array) {
|
264
|
+
return "[" + exports.map(array, exports.repr).join(', ') + "]";
|
265
|
+
};
|
266
|
+
|
267
|
+
exports.array.first = function (array) {
|
268
|
+
return array[0];
|
269
|
+
};
|
270
|
+
|
271
|
+
exports.array.last = function (array) {
|
272
|
+
return array[array.length - 1];
|
273
|
+
};
|
274
|
+
|
275
|
+
exports.apply = exports.operator('apply', 2, function (args, block) {
|
276
|
+
return block.apply(this, args);
|
277
|
+
});
|
278
|
+
|
279
|
+
exports.copy = exports.operator('copy', 1, function (object) {
|
280
|
+
if (exports.no(object))
|
281
|
+
return object;
|
282
|
+
if (exports.isArrayLike(object))
|
283
|
+
return exports.array.copy(object);
|
284
|
+
if (typeof object == 'object')
|
285
|
+
return exports.object.copy(object);
|
286
|
+
return object;
|
287
|
+
});
|
288
|
+
|
289
|
+
exports.deepCopy = exports.operator('deepCopy', 1, function (object) {
|
290
|
+
if (exports.no(object))
|
291
|
+
return object;
|
292
|
+
if (exports.isArrayLike(object))
|
293
|
+
return exports.array.deepCopy(object);
|
294
|
+
if (typeof object == 'object')
|
295
|
+
return exports.object.deepCopy(object);
|
296
|
+
return object;
|
297
|
+
});
|
298
|
+
|
299
|
+
exports.repr = exports.operator('repr', 1, function (object) {
|
300
|
+
if (exports.no(object))
|
301
|
+
return String(object);
|
302
|
+
if (exports.isArrayLike(object))
|
303
|
+
return exports.array.repr(object);
|
304
|
+
if (typeof object == 'object')
|
305
|
+
return exports.object.repr(object);
|
306
|
+
if (typeof object == 'string')
|
307
|
+
return exports.enquote(object);
|
308
|
+
return object.toString();
|
309
|
+
});
|
310
|
+
|
311
|
+
exports.keys = exports.operator('keys', 1, function (object) {
|
312
|
+
if (exports.isArrayLike(object))
|
313
|
+
return exports.range(object.length);
|
314
|
+
else if (typeof object == 'object')
|
315
|
+
return exports.object.keys(object);
|
316
|
+
return [];
|
317
|
+
});
|
318
|
+
|
319
|
+
exports.values = exports.operator('values', 1, function (object) {
|
320
|
+
if (exports.isArrayLike(object))
|
321
|
+
return exports.array(object);
|
322
|
+
else if (typeof object == 'object')
|
323
|
+
return exports.object.values(object);
|
324
|
+
return [];
|
325
|
+
});
|
326
|
+
|
327
|
+
exports.items = exports.operator('items', 1, function (object) {
|
328
|
+
if (exports.isArrayLike(object) || typeof object == "string")
|
329
|
+
return exports.enumerate(object);
|
330
|
+
else if (typeof object == 'object')
|
331
|
+
return exports.object.items(object);
|
332
|
+
return [];
|
333
|
+
});
|
334
|
+
|
335
|
+
exports.len = exports.operator('len', 1, function (object) {
|
336
|
+
if (exports.isArrayLike(object))
|
337
|
+
return exports.array.len(object);
|
338
|
+
else if (typeof object == 'object')
|
339
|
+
return exports.object.len(object);
|
340
|
+
});
|
341
|
+
|
342
|
+
exports.has = exports.operator('has', 2, function (object, value) {
|
343
|
+
if (exports.isArrayLike(object))
|
344
|
+
return exports.array.has(object, value);
|
345
|
+
else if (typeof object == 'object')
|
346
|
+
return exports.object.has(object, value);
|
347
|
+
return false;
|
348
|
+
});
|
349
|
+
|
350
|
+
exports.get = exports.operator('get', 2, function (object, key, value) {
|
351
|
+
if (typeof object == "string") {
|
352
|
+
if (!typeof key == "number")
|
353
|
+
throw new Error("TypeError: String keys must be numbers");
|
354
|
+
if (!exports.has(exports.range(object.length), key)) {
|
355
|
+
if (arguments.length == 3)
|
356
|
+
return value;
|
357
|
+
throw new Error("KeyError: " + exports.repr(key));
|
358
|
+
}
|
359
|
+
return object.charAt(key);
|
360
|
+
}
|
361
|
+
if (typeof object == "object") {
|
362
|
+
if (!exports.object.has(object, key)) {
|
363
|
+
if (arguments.length == 3)
|
364
|
+
return value;
|
365
|
+
throw new Error("KeyError: " + exports.repr(key));
|
366
|
+
}
|
367
|
+
return object[key];
|
368
|
+
}
|
369
|
+
throw new Error("Object does not have keys: " + exports.repr(object));
|
370
|
+
});
|
371
|
+
|
372
|
+
exports.set = exports.operator('set', 3, function (object, key, value) {
|
373
|
+
object[key] = value;
|
374
|
+
return object;
|
375
|
+
});
|
376
|
+
|
377
|
+
exports.getset = exports.operator('getset', 3, function (object, key, value) {
|
378
|
+
if (!exports.has(object, key))
|
379
|
+
exports.set(object, key, value);
|
380
|
+
return exports.get(object, key);
|
381
|
+
});
|
382
|
+
|
383
|
+
exports.del = exports.operator('del', 2, function (object, begin, end) {
|
384
|
+
if (exports.isArrayLike(object))
|
385
|
+
return exports.array.del(object, begin, end);
|
386
|
+
delete object[begin];
|
387
|
+
return object;
|
388
|
+
});
|
389
|
+
|
390
|
+
exports.cut = exports.operator('cut', 2, function (object, key) {
|
391
|
+
var result = exports.get(object, key);
|
392
|
+
exports.del(object, key);
|
393
|
+
return result;
|
394
|
+
});
|
395
|
+
|
396
|
+
exports.put = exports.operator('put', 2, function (object, key, value) {
|
397
|
+
if (exports.isArrayLike(object))
|
398
|
+
return exports.array.put(object, key, value);
|
399
|
+
return exports.set(object, key, value);
|
400
|
+
});
|
401
|
+
|
402
|
+
exports.first = exports.operator('first', 1, function (object) {
|
403
|
+
return object[0];
|
404
|
+
});
|
405
|
+
|
406
|
+
exports.last = exports.operator('last', 1, function (object) {
|
407
|
+
return object[object.length - 1];
|
408
|
+
});
|
409
|
+
|
410
|
+
exports.update = exports.operator('update', 2, function (target, source) {
|
411
|
+
exports.object.update(target, source);
|
412
|
+
});
|
413
|
+
|
414
|
+
exports.complete = exports.operator('complete', 2, function (target, source) {
|
415
|
+
exports.object.complete(target, source);
|
416
|
+
});
|
417
|
+
|
418
|
+
// TODO insert
|
419
|
+
// TODO remove
|
420
|
+
// TODO discard
|
421
|
+
|
422
|
+
exports.range = function () {
|
423
|
+
var start = 0, stop = 0, step = 1;
|
424
|
+
if (arguments.length == 1) {
|
425
|
+
stop = arguments[0];
|
426
|
+
} else {
|
427
|
+
start = arguments[0];
|
428
|
+
stop = arguments[1];
|
429
|
+
step = arguments[2] || 1;
|
430
|
+
}
|
431
|
+
var range = [];
|
432
|
+
for (var i = start; i < stop; i += step)
|
433
|
+
range.push(i);
|
434
|
+
return range;
|
435
|
+
};
|
436
|
+
|
437
|
+
exports.forEach = function (array, block) {
|
438
|
+
Array.prototype.forEach.call(
|
439
|
+
exports.array.coerce(array),
|
440
|
+
block
|
441
|
+
);
|
442
|
+
};
|
443
|
+
|
444
|
+
exports.forEachApply = function (array, block) {
|
445
|
+
Array.prototype.forEach.call(
|
446
|
+
exports.array.coerce(array),
|
447
|
+
exports.apply(block)
|
448
|
+
);
|
449
|
+
};
|
450
|
+
|
451
|
+
exports.map = function (array, block, context) {
|
452
|
+
return Array.prototype.map.call(
|
453
|
+
exports.array.coerce(array),
|
454
|
+
block,
|
455
|
+
context
|
456
|
+
);
|
457
|
+
};
|
458
|
+
|
459
|
+
exports.mapApply = function (array, block) {
|
460
|
+
return Array.prototype.map.call(
|
461
|
+
exports.array.coerce(array),
|
462
|
+
exports.apply(block)
|
463
|
+
);
|
464
|
+
};
|
465
|
+
|
466
|
+
exports.every = exports.operator('every', 2, function (array, block, context) {
|
467
|
+
return exports.all(exports.map(array, block, context));
|
468
|
+
});
|
469
|
+
|
470
|
+
exports.some = exports.operator('some', 2, function (array, block, context) {
|
471
|
+
return exports.any(exports.map(array, block, context));
|
472
|
+
});
|
473
|
+
|
474
|
+
exports.all = exports.operator('all', 1, function (array) {
|
475
|
+
array = exports.array.coerce(array);
|
476
|
+
for (var i = 0; i < array.length; i++)
|
477
|
+
if (!array[i])
|
478
|
+
return false;
|
479
|
+
return true;
|
480
|
+
});
|
481
|
+
|
482
|
+
exports.any = exports.operator('all', 1, function (array) {
|
483
|
+
array = exports.array.coerce(array);
|
484
|
+
for (var i = 0; i < array.length; i++)
|
485
|
+
if (array[i])
|
486
|
+
return true;
|
487
|
+
return false;
|
488
|
+
});
|
489
|
+
|
490
|
+
exports.reduce = exports.operator('reduce', 2, function (array, block, basis) {
|
491
|
+
array = exports.array.coerce(array);
|
492
|
+
return array.reduce.apply(array, arguments);
|
493
|
+
});
|
494
|
+
|
495
|
+
exports.reduceRight = exports.operator('reduceRight', 2, function (array, block, basis) {
|
496
|
+
array = exports.array.coerce(array);
|
497
|
+
return array.reduceRight.apply(array, arguments);
|
498
|
+
});
|
499
|
+
|
500
|
+
exports.zip = function () {
|
501
|
+
return exports.transpose(arguments);
|
502
|
+
};
|
503
|
+
|
504
|
+
exports.transpose = function (array) {
|
505
|
+
array = exports.array.coerce(array);
|
506
|
+
var transpose = [];
|
507
|
+
var length = Math.min.apply(this, exports.map(array, function (row) {
|
508
|
+
return row.length;
|
509
|
+
}));
|
510
|
+
for (var i = 0; i < array.length; i++) {
|
511
|
+
var row = array[i];
|
512
|
+
for (var j = 0; j < length; j++) {
|
513
|
+
var cell = row[j];
|
514
|
+
if (!transpose[j])
|
515
|
+
transpose[j] = [];
|
516
|
+
transpose[j][i] = cell;
|
517
|
+
}
|
518
|
+
}
|
519
|
+
return transpose;
|
520
|
+
};
|
521
|
+
|
522
|
+
exports.enumerate = function (array, start) {
|
523
|
+
array = exports.array.coerce(array);
|
524
|
+
if (exports.no(start))
|
525
|
+
start = 0;
|
526
|
+
return exports.zip(
|
527
|
+
exports.range(start, start + array.length),
|
528
|
+
array
|
529
|
+
);
|
530
|
+
};
|
531
|
+
|
532
|
+
// arithmetic, transitive, and logical operators
|
533
|
+
|
534
|
+
exports.is = function (a, b) {
|
535
|
+
return a === b;
|
536
|
+
};
|
537
|
+
|
538
|
+
exports.eq = exports.operator('eq', 2, function (a, b, stack) {
|
539
|
+
if (!stack)
|
540
|
+
stack = [];
|
541
|
+
if (a === b)
|
542
|
+
return true;
|
543
|
+
if (typeof a !== typeof b)
|
544
|
+
return false;
|
545
|
+
if (exports.no(a))
|
546
|
+
return exports.no(b);
|
547
|
+
if (typeof a == "date")
|
548
|
+
return a.valueOf() == b.valueOf();
|
549
|
+
if (typeof a == "regexp")
|
550
|
+
return a.source == b.source &&
|
551
|
+
a.global == b.global &&
|
552
|
+
a.ignoreCase == b.ignoreCase &&
|
553
|
+
a.multiline == b.multiline;
|
554
|
+
if (typeof a == "function") {
|
555
|
+
var caller = stack[stack.length - 1];
|
556
|
+
return caller !== Object &&
|
557
|
+
typeof caller != "undefined";
|
558
|
+
}
|
559
|
+
if (exports.isArrayLike(a))
|
560
|
+
return exports.array.eq(
|
561
|
+
a, b,
|
562
|
+
stack.concat([a.constructor])
|
563
|
+
);
|
564
|
+
if (typeof a == 'object')
|
565
|
+
return exports.object.eq(
|
566
|
+
a, b,
|
567
|
+
stack.concat([a.constructor])
|
568
|
+
);
|
569
|
+
return a == b;
|
570
|
+
});
|
571
|
+
|
572
|
+
exports.ne = exports.operator('ne', 2, function (a, b) {
|
573
|
+
return !exports.eq(a, b);
|
574
|
+
});
|
575
|
+
|
576
|
+
exports.lt = exports.operator('lt', 2, function (a, b) {
|
577
|
+
if (exports.no(a) != exports.no(b))
|
578
|
+
return exports.no(a) > exports.no(b);
|
579
|
+
if (exports.isArrayLike(a) && exports.isArrayLike(b))
|
580
|
+
return exports.array.lt(a, b);
|
581
|
+
return a < b;
|
582
|
+
});
|
583
|
+
|
584
|
+
exports.gt = exports.operator('gt', 2, function (a, b) {
|
585
|
+
return !(exports.lt(a, b) || exports.eq(a, b));
|
586
|
+
});
|
587
|
+
|
588
|
+
exports.le = exports.operator(2, 'le', function (a, b) {
|
589
|
+
return exports.lt(a, b) || exports.eq(a, b);
|
590
|
+
});
|
591
|
+
|
592
|
+
exports.ge = exports.operator(2, 'ge', function (a, b) {
|
593
|
+
return !exports.lt(a, b);
|
594
|
+
});
|
595
|
+
|
596
|
+
exports.mul = exports.operator(2, 'mul', function (a, b) {
|
597
|
+
if (typeof a == "string")
|
598
|
+
return exports.string.mul(a, b);
|
599
|
+
return a * b;
|
600
|
+
});
|
601
|
+
|
602
|
+
/*** by
|
603
|
+
returns a `comparator` that compares
|
604
|
+
values based on the values resultant from
|
605
|
+
a given `relation`.
|
606
|
+
accepts a `relation` and an optional comparator.
|
607
|
+
|
608
|
+
To sort a list of objects based on their
|
609
|
+
"a" key::
|
610
|
+
|
611
|
+
objects.sort(by(get("a")))
|
612
|
+
|
613
|
+
To get those in descending order::
|
614
|
+
|
615
|
+
objects.sort(by(get("a")), desc)
|
616
|
+
|
617
|
+
`by` returns a comparison function that also tracks
|
618
|
+
the arguments you used to construct it. This permits
|
619
|
+
`sort` and `sorted` to perform a Schwartzian transform
|
620
|
+
which can increase the performance of the sort
|
621
|
+
by a factor of 2.
|
622
|
+
*/
|
623
|
+
exports.by = function (relation) {
|
624
|
+
var compare = arguments[1];
|
625
|
+
if (exports.no(compare))
|
626
|
+
compare = exports.compare;
|
627
|
+
var comparator = function (a, b) {
|
628
|
+
a = relation(a);
|
629
|
+
b = relation(b);
|
630
|
+
return compare(a, b);
|
631
|
+
};
|
632
|
+
comparator.by = relation;
|
633
|
+
comparator.compare = compare;
|
634
|
+
return comparator;
|
635
|
+
};
|
636
|
+
|
637
|
+
exports.compare = exports.operator(2, 'compare', function (a, b) {
|
638
|
+
if (exports.no(a) != exports.no(b))
|
639
|
+
return exports.no(b) - exports.no(a);
|
640
|
+
if (typeof a == "number" && typeof b == "number")
|
641
|
+
return a - b;
|
642
|
+
return exports.eq(a, b) ? 0 : exports.lt(a, b) ? -1 : 1;
|
643
|
+
});
|
644
|
+
|
645
|
+
/*** sort
|
646
|
+
an in-place array sorter that uses a deep comparison
|
647
|
+
function by default (compare), and improves performance if
|
648
|
+
you provide a comparator returned by "by", using a
|
649
|
+
Schwartzian transform.
|
650
|
+
*/
|
651
|
+
exports.sort = function (array, compare) {
|
652
|
+
if (exports.no(compare))
|
653
|
+
compare = exports.compare;
|
654
|
+
if (compare.by) {
|
655
|
+
/* schwartzian transform */
|
656
|
+
array.splice.apply(
|
657
|
+
array,
|
658
|
+
[0, array.length].concat(
|
659
|
+
array.map(function (value) {
|
660
|
+
return [compare.by(value), value];
|
661
|
+
}).sort(function (a, b) {
|
662
|
+
return compare(a[0], b[0]);
|
663
|
+
}).map(function (pair) {
|
664
|
+
return pair[1];
|
665
|
+
})
|
666
|
+
)
|
667
|
+
);
|
668
|
+
} else {
|
669
|
+
array.sort(compare);
|
670
|
+
}
|
671
|
+
return array;
|
672
|
+
};
|
673
|
+
|
674
|
+
/*** sorted
|
675
|
+
returns a sorted copy of an array using a deep
|
676
|
+
comparison function by default (compare), and
|
677
|
+
improves performance if you provide a comparator
|
678
|
+
returned by "by", using a Schwartzian transform.
|
679
|
+
*/
|
680
|
+
exports.sorted = function (array, compare) {
|
681
|
+
return exports.sort(exports.array.copy(array), compare);
|
682
|
+
};
|
683
|
+
|
684
|
+
exports.hash = exports.operator(1, 'hash', function (object) {
|
685
|
+
return '' + object;
|
686
|
+
});
|
687
|
+
|
688
|
+
exports.unique = exports.operator(1, 'unique', function (array, eq, hash) {
|
689
|
+
var visited = {};
|
690
|
+
if (!eq) eq = exports.eq;
|
691
|
+
if (!hash) hash = exports.hash;
|
692
|
+
return array.filter(function (value) {
|
693
|
+
var bucket = exports.getset(visited, hash(value), []);
|
694
|
+
var finds = bucket.filter(function (other) {
|
695
|
+
return eq(value, other);
|
696
|
+
});
|
697
|
+
if (!finds.length)
|
698
|
+
bucket.push(value);
|
699
|
+
return !finds.length;
|
700
|
+
});
|
701
|
+
});
|
702
|
+
|
703
|
+
// string
|
704
|
+
|
705
|
+
exports.string = exports.operator(1, 'string', function (object) {
|
706
|
+
return '' + object;
|
707
|
+
});
|
708
|
+
|
709
|
+
exports.string.mul = function (string, n) {
|
710
|
+
return exports.range(n).map(function () {
|
711
|
+
return string;
|
712
|
+
}).join('');
|
713
|
+
};
|
714
|
+
|
715
|
+
/*** escape
|
716
|
+
escapes all characters of a string that are
|
717
|
+
special to JavaScript and many other languages.
|
718
|
+
Recognizes all of the relevant
|
719
|
+
control characters and formats all other
|
720
|
+
non-printable characters as Hex byte escape
|
721
|
+
sequences or Unicode escape sequences depending
|
722
|
+
on their size.
|
723
|
+
|
724
|
+
Pass ``true`` as an optional second argument and
|
725
|
+
``escape`` produces valid contents for escaped
|
726
|
+
JSON strings, wherein non-printable-characters are
|
727
|
+
all escaped with the Unicode ``\u`` notation.
|
728
|
+
*/
|
729
|
+
/* more Steve Levithan flagrence */
|
730
|
+
var escapeExpression = /[^ !#-[\]-~]/g;
|
731
|
+
/* from Doug Crockford's JSON library */
|
732
|
+
var escapePatterns = {
|
733
|
+
'\b': '\\b', '\t': '\\t',
|
734
|
+
'\n': '\\n', '\f': '\\f', '\r': '\\r',
|
735
|
+
'"' : '\\"', '\\': '\\\\'
|
736
|
+
};
|
737
|
+
exports.escape = function (value, strictJson) {
|
738
|
+
if (typeof value != "string")
|
739
|
+
throw new Error(
|
740
|
+
module.path +
|
741
|
+
"#escape: requires a string. got " +
|
742
|
+
exports.repr(value)
|
743
|
+
);
|
744
|
+
return value.replace(
|
745
|
+
escapeExpression,
|
746
|
+
function (match) {
|
747
|
+
if (escapePatterns[match])
|
748
|
+
return escapePatterns[match];
|
749
|
+
match = match.charCodeAt();
|
750
|
+
if (!strictJson && match < 256)
|
751
|
+
return "\\x" + exports.padBegin(match.toString(16), 2);
|
752
|
+
return '\\u' + exports.padBegin(match.toString(16), 4);
|
753
|
+
}
|
754
|
+
);
|
755
|
+
};
|
756
|
+
|
757
|
+
/*** enquote
|
758
|
+
transforms a string into a string literal, escaping
|
759
|
+
all characters of a string that are special to
|
760
|
+
JavaScript and and some other languages.
|
761
|
+
|
762
|
+
``enquote`` uses double quotes to be JSON compatible.
|
763
|
+
|
764
|
+
Pass ``true`` as an optional second argument to
|
765
|
+
be strictly JSON compliant, wherein all
|
766
|
+
non-printable-characters are represented with
|
767
|
+
Unicode escape sequences.
|
768
|
+
*/
|
769
|
+
exports.enquote = function (value, strictJson) {
|
770
|
+
return '"' + exports.escape(value, strictJson) + '"';
|
771
|
+
};
|
772
|
+
|
773
|
+
/*** expand
|
774
|
+
transforms tabs to an equivalent number of spaces.
|
775
|
+
*/
|
776
|
+
// TODO special case for \r if it ever matters
|
777
|
+
exports.expand = function (str, tabLength) {
|
778
|
+
str = String(str);
|
779
|
+
tabLength = tabLength || 4;
|
780
|
+
var output = [],
|
781
|
+
tabLf = /[\t\n]/g,
|
782
|
+
lastLastIndex = 0,
|
783
|
+
lastLfIndex = 0,
|
784
|
+
charsAddedThisLine = 0,
|
785
|
+
tabOffset, match;
|
786
|
+
while (match = tabLf.exec(str)) {
|
787
|
+
if (match[0] == "\t") {
|
788
|
+
tabOffset = (
|
789
|
+
tabLength - 1 -
|
790
|
+
(
|
791
|
+
(match.index - lastLfIndex) +
|
792
|
+
charsAddedThisLine
|
793
|
+
) % tabLength
|
794
|
+
);
|
795
|
+
charsAddedThisLine += tabOffset;
|
796
|
+
output.push(
|
797
|
+
str.slice(lastLastIndex, match.index) +
|
798
|
+
exports.mul(" ", tabOffset + 1)
|
799
|
+
);
|
800
|
+
} else if (match[0] === "\n") {
|
801
|
+
output.push(str.slice(lastLastIndex, tabLf.lastIndex));
|
802
|
+
lastLfIndex = tabLf.lastIndex;
|
803
|
+
charsAddedThisLine = 0;
|
804
|
+
}
|
805
|
+
lastLastIndex = tabLf.lastIndex;
|
806
|
+
}
|
807
|
+
return output.join("") + str.slice(lastLastIndex);
|
808
|
+
};
|
809
|
+
|
810
|
+
var trimBeginExpression = /^\s\s*/g;
|
811
|
+
exports.trimBegin = function (value) {
|
812
|
+
return String(value).replace(trimBeginExpression, "");
|
813
|
+
};
|
814
|
+
|
815
|
+
var trimEndExpression = /\s\s*$/g;
|
816
|
+
exports.trimEnd = function (value) {
|
817
|
+
return String(value).replace(trimEndExpression, "");
|
818
|
+
};
|
819
|
+
|
820
|
+
exports.trim = function (value) {
|
821
|
+
return String(value).replace(trimBeginExpression, "").replace(trimEndExpression, "");
|
822
|
+
};
|
823
|
+
|
824
|
+
/* generates padBegin and padEnd */
|
825
|
+
var augmentor = function (augment) {
|
826
|
+
return function (value, length, pad) {
|
827
|
+
if (exports.no(pad)) pad = '0';
|
828
|
+
if (exports.no(length)) length = 2;
|
829
|
+
value = String(value);
|
830
|
+
while (value.length < length) {
|
831
|
+
value = augment(value, pad);
|
832
|
+
}
|
833
|
+
return value;
|
834
|
+
};
|
835
|
+
};
|
836
|
+
|
837
|
+
/*** padBegin
|
838
|
+
|
839
|
+
accepts:
|
840
|
+
- a `String` or `Number` value
|
841
|
+
- a minimum length of the resultant `String`:
|
842
|
+
by default, 2
|
843
|
+
- a pad string: by default, ``'0'``.
|
844
|
+
|
845
|
+
returns a `String` of the value padded up to at least
|
846
|
+
the minimum length. adds the padding to the begining
|
847
|
+
side of the `String`.
|
848
|
+
|
849
|
+
*/
|
850
|
+
exports.padBegin = augmentor(function (value, pad) {
|
851
|
+
return pad + value;
|
852
|
+
});
|
853
|
+
|
854
|
+
/*** padEnd
|
855
|
+
|
856
|
+
accepts:
|
857
|
+
- a `String` or `Number` value
|
858
|
+
- a minimum length of the resultant `String`:
|
859
|
+
by default, 2
|
860
|
+
- a pad string: by default, ``'0'``.
|
861
|
+
|
862
|
+
returns a `String` of the value padded up to at least
|
863
|
+
the minimum length. adds the padding to the end
|
864
|
+
side of the `String`.
|
865
|
+
|
866
|
+
*/
|
867
|
+
exports.padEnd = augmentor(function (value, pad) {
|
868
|
+
return value + pad;
|
869
|
+
});
|
870
|
+
|
871
|
+
/*** splitName
|
872
|
+
splits a string into a `List` of words from an origin
|
873
|
+
string.
|
874
|
+
*/
|
875
|
+
var splitNameExpression = /[a-z]+|[A-Z](?:[a-z]+|[A-Z]*(?![a-z]))|[.\d]+/g;
|
876
|
+
exports.splitName = function (value) {
|
877
|
+
return String(value).match(splitNameExpression);
|
878
|
+
};
|
879
|
+
|
880
|
+
/*** joinName
|
881
|
+
joins a list of words with a given delimiter
|
882
|
+
between alphanumeric words.
|
883
|
+
*/
|
884
|
+
exports.joinName = function (delimiter, parts) {
|
885
|
+
if (exports.no(delimiter)) delimiter = '_';
|
886
|
+
parts.unshift([]);
|
887
|
+
return parts.reduce(function (parts, part) {
|
888
|
+
if (
|
889
|
+
part.match(/\d/) &&
|
890
|
+
exports.len(parts) && parts[parts.length-1].match(/\d/)
|
891
|
+
) {
|
892
|
+
return parts.concat([delimiter + part]);
|
893
|
+
} else {
|
894
|
+
return parts.concat([part]);
|
895
|
+
}
|
896
|
+
}).join('');
|
897
|
+
};
|
898
|
+
|
899
|
+
/*** upper
|
900
|
+
converts a name to ``UPPER CASE`` using
|
901
|
+
a given delimiter between numeric words.
|
902
|
+
|
903
|
+
see:
|
904
|
+
- `lower`
|
905
|
+
- `camel`
|
906
|
+
- `title`
|
907
|
+
|
908
|
+
*/
|
909
|
+
exports.upper = function (value, delimiter) {
|
910
|
+
if (exports.no(delimiter))
|
911
|
+
return value.toUpperCase();
|
912
|
+
return exports.splitName(value).map(function (part) {
|
913
|
+
return part.toUpperCase();
|
914
|
+
}).join(delimiter);
|
915
|
+
};
|
916
|
+
|
917
|
+
/*** lower
|
918
|
+
converts a name to a ``lower case`` using
|
919
|
+
a given delimiter between numeric words.
|
920
|
+
|
921
|
+
see:
|
922
|
+
- `upper`
|
923
|
+
- `camel`
|
924
|
+
- `title`
|
925
|
+
|
926
|
+
*/
|
927
|
+
exports.lower = function (value, delimiter) {
|
928
|
+
if (exports.no(delimiter))
|
929
|
+
return String(value).toLowerCase();
|
930
|
+
return exports.splitName(value).map(function (part) {
|
931
|
+
return part.toLowerCase();
|
932
|
+
}).join(delimiter);
|
933
|
+
};
|
934
|
+
|
935
|
+
/*** camel
|
936
|
+
converts a name to ``camel Case`` using
|
937
|
+
a given delimiter between numeric words.
|
938
|
+
|
939
|
+
see:
|
940
|
+
- `lower`
|
941
|
+
- `upper`
|
942
|
+
- `title`
|
943
|
+
|
944
|
+
*/
|
945
|
+
exports.camel = function (value, delimiter) {
|
946
|
+
return exports.joinName(
|
947
|
+
delimiter,
|
948
|
+
exports.mapApply(
|
949
|
+
exports.enumerate(exports.splitName(value)),
|
950
|
+
function (n, part) {
|
951
|
+
if (n) {
|
952
|
+
return (
|
953
|
+
part.substring(0, 1).toUpperCase() +
|
954
|
+
part.substring(1).toLowerCase()
|
955
|
+
);
|
956
|
+
} else {
|
957
|
+
return part.toLowerCase();
|
958
|
+
}
|
959
|
+
}
|
960
|
+
)
|
961
|
+
);
|
962
|
+
};
|
963
|
+
|
964
|
+
/*** title
|
965
|
+
converts a name to ``Title Case`` using
|
966
|
+
a given delimiter between numeric words.
|
967
|
+
|
968
|
+
see:
|
969
|
+
- `lower`
|
970
|
+
- `upper`
|
971
|
+
- `camel`
|
972
|
+
|
973
|
+
*/
|
974
|
+
exports.title = function (value, delimiter) {
|
975
|
+
return exports.joinName(
|
976
|
+
delimiter,
|
977
|
+
exports.splitName(value).map(function (part) {
|
978
|
+
return (
|
979
|
+
part.substring(0, 1).toUpperCase() +
|
980
|
+
part.substring(1).toLowerCase()
|
981
|
+
);
|
982
|
+
})
|
983
|
+
);
|
984
|
+
};
|
985
|
+
|