2013-03-11 19:14:26 +00:00
|
|
|
/*
|
|
|
|
Copyright (c) 2013, Alan Kligman
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
|
|
|
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
|
|
|
Neither the name of the Mozilla Foundation nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
(function( root, factory ) {
|
|
|
|
|
|
|
|
if ( typeof exports === "object" ) {
|
|
|
|
// Node
|
|
|
|
module.exports = factory();
|
|
|
|
} else if (typeof define === "function" && define.amd) {
|
|
|
|
// AMD. Register as an anonymous module.
|
|
|
|
define( factory );
|
|
|
|
} else if( !root.IDBFS ) {
|
|
|
|
// Browser globals
|
|
|
|
root.IDBFS = factory();
|
|
|
|
}
|
|
|
|
|
|
|
|
}( this, function() {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* almond 0.2.5 Copyright (c) 2011-2012, The Dojo Foundation All Rights Reserved.
|
|
|
|
* Available via the MIT or new BSD license.
|
|
|
|
* see: http://github.com/jrburke/almond for details
|
|
|
|
*/
|
|
|
|
//Going sloppy to avoid 'use strict' string cost, but strict practices should
|
|
|
|
//be followed.
|
|
|
|
/*jslint sloppy: true */
|
|
|
|
/*global setTimeout: false */
|
|
|
|
|
|
|
|
var requirejs, require, define;
|
|
|
|
(function (undef) {
|
|
|
|
var main, req, makeMap, handlers,
|
|
|
|
defined = {},
|
|
|
|
waiting = {},
|
|
|
|
config = {},
|
|
|
|
defining = {},
|
|
|
|
hasOwn = Object.prototype.hasOwnProperty,
|
|
|
|
aps = [].slice;
|
|
|
|
|
|
|
|
function hasProp(obj, prop) {
|
|
|
|
return hasOwn.call(obj, prop);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a relative module name, like ./something, normalize it to
|
|
|
|
* a real name that can be mapped to a path.
|
|
|
|
* @param {String} name the relative name
|
|
|
|
* @param {String} baseName a real name that the name arg is relative
|
|
|
|
* to.
|
|
|
|
* @returns {String} normalized name
|
|
|
|
*/
|
|
|
|
function normalize(name, baseName) {
|
|
|
|
var nameParts, nameSegment, mapValue, foundMap,
|
|
|
|
foundI, foundStarMap, starI, i, j, part,
|
|
|
|
baseParts = baseName && baseName.split("/"),
|
|
|
|
map = config.map,
|
|
|
|
starMap = (map && map['*']) || {};
|
|
|
|
|
|
|
|
//Adjust any relative paths.
|
|
|
|
if (name && name.charAt(0) === ".") {
|
|
|
|
//If have a base name, try to normalize against it,
|
|
|
|
//otherwise, assume it is a top-level require that will
|
|
|
|
//be relative to baseUrl in the end.
|
|
|
|
if (baseName) {
|
|
|
|
//Convert baseName to array, and lop off the last part,
|
|
|
|
//so that . matches that "directory" and not name of the baseName's
|
|
|
|
//module. For instance, baseName of "one/two/three", maps to
|
|
|
|
//"one/two/three.js", but we want the directory, "one/two" for
|
|
|
|
//this normalization.
|
|
|
|
baseParts = baseParts.slice(0, baseParts.length - 1);
|
|
|
|
|
|
|
|
name = baseParts.concat(name.split("/"));
|
|
|
|
|
|
|
|
//start trimDots
|
|
|
|
for (i = 0; i < name.length; i += 1) {
|
|
|
|
part = name[i];
|
|
|
|
if (part === ".") {
|
|
|
|
name.splice(i, 1);
|
|
|
|
i -= 1;
|
|
|
|
} else if (part === "..") {
|
|
|
|
if (i === 1 && (name[2] === '..' || name[0] === '..')) {
|
|
|
|
//End of the line. Keep at least one non-dot
|
|
|
|
//path segment at the front so it can be mapped
|
|
|
|
//correctly to disk. Otherwise, there is likely
|
|
|
|
//no path mapping for a path starting with '..'.
|
|
|
|
//This can still fail, but catches the most reasonable
|
|
|
|
//uses of ..
|
|
|
|
break;
|
|
|
|
} else if (i > 0) {
|
|
|
|
name.splice(i - 1, 2);
|
|
|
|
i -= 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//end trimDots
|
|
|
|
|
|
|
|
name = name.join("/");
|
|
|
|
} else if (name.indexOf('./') === 0) {
|
|
|
|
// No baseName, so this is ID is resolved relative
|
|
|
|
// to baseUrl, pull off the leading dot.
|
|
|
|
name = name.substring(2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Apply map config if available.
|
|
|
|
if ((baseParts || starMap) && map) {
|
|
|
|
nameParts = name.split('/');
|
|
|
|
|
|
|
|
for (i = nameParts.length; i > 0; i -= 1) {
|
|
|
|
nameSegment = nameParts.slice(0, i).join("/");
|
|
|
|
|
|
|
|
if (baseParts) {
|
|
|
|
//Find the longest baseName segment match in the config.
|
|
|
|
//So, do joins on the biggest to smallest lengths of baseParts.
|
|
|
|
for (j = baseParts.length; j > 0; j -= 1) {
|
|
|
|
mapValue = map[baseParts.slice(0, j).join('/')];
|
|
|
|
|
|
|
|
//baseName segment has config, find if it has one for
|
|
|
|
//this name.
|
|
|
|
if (mapValue) {
|
|
|
|
mapValue = mapValue[nameSegment];
|
|
|
|
if (mapValue) {
|
|
|
|
//Match, update name to the new value.
|
|
|
|
foundMap = mapValue;
|
|
|
|
foundI = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (foundMap) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Check for a star map match, but just hold on to it,
|
|
|
|
//if there is a shorter segment match later in a matching
|
|
|
|
//config, then favor over this star map.
|
|
|
|
if (!foundStarMap && starMap && starMap[nameSegment]) {
|
|
|
|
foundStarMap = starMap[nameSegment];
|
|
|
|
starI = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!foundMap && foundStarMap) {
|
|
|
|
foundMap = foundStarMap;
|
|
|
|
foundI = starI;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (foundMap) {
|
|
|
|
nameParts.splice(0, foundI, foundMap);
|
|
|
|
name = nameParts.join('/');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
function makeRequire(relName, forceSync) {
|
|
|
|
return function () {
|
|
|
|
//A version of a require function that passes a moduleName
|
|
|
|
//value for items that may need to
|
|
|
|
//look up paths relative to the moduleName
|
|
|
|
return req.apply(undef, aps.call(arguments, 0).concat([relName, forceSync]));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function makeNormalize(relName) {
|
|
|
|
return function (name) {
|
|
|
|
return normalize(name, relName);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function makeLoad(depName) {
|
|
|
|
return function (value) {
|
|
|
|
defined[depName] = value;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function callDep(name) {
|
|
|
|
if (hasProp(waiting, name)) {
|
|
|
|
var args = waiting[name];
|
|
|
|
delete waiting[name];
|
|
|
|
defining[name] = true;
|
|
|
|
main.apply(undef, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!hasProp(defined, name) && !hasProp(defining, name)) {
|
|
|
|
throw new Error('No ' + name);
|
|
|
|
}
|
|
|
|
return defined[name];
|
|
|
|
}
|
|
|
|
|
|
|
|
//Turns a plugin!resource to [plugin, resource]
|
|
|
|
//with the plugin being undefined if the name
|
|
|
|
//did not have a plugin prefix.
|
|
|
|
function splitPrefix(name) {
|
|
|
|
var prefix,
|
|
|
|
index = name ? name.indexOf('!') : -1;
|
|
|
|
if (index > -1) {
|
|
|
|
prefix = name.substring(0, index);
|
|
|
|
name = name.substring(index + 1, name.length);
|
|
|
|
}
|
|
|
|
return [prefix, name];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Makes a name map, normalizing the name, and using a plugin
|
|
|
|
* for normalization if necessary. Grabs a ref to plugin
|
|
|
|
* too, as an optimization.
|
|
|
|
*/
|
|
|
|
makeMap = function (name, relName) {
|
|
|
|
var plugin,
|
|
|
|
parts = splitPrefix(name),
|
|
|
|
prefix = parts[0];
|
|
|
|
|
|
|
|
name = parts[1];
|
|
|
|
|
|
|
|
if (prefix) {
|
|
|
|
prefix = normalize(prefix, relName);
|
|
|
|
plugin = callDep(prefix);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Normalize according
|
|
|
|
if (prefix) {
|
|
|
|
if (plugin && plugin.normalize) {
|
|
|
|
name = plugin.normalize(name, makeNormalize(relName));
|
|
|
|
} else {
|
|
|
|
name = normalize(name, relName);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
name = normalize(name, relName);
|
|
|
|
parts = splitPrefix(name);
|
|
|
|
prefix = parts[0];
|
|
|
|
name = parts[1];
|
|
|
|
if (prefix) {
|
|
|
|
plugin = callDep(prefix);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Using ridiculous property names for space reasons
|
|
|
|
return {
|
|
|
|
f: prefix ? prefix + '!' + name : name, //fullName
|
|
|
|
n: name,
|
|
|
|
pr: prefix,
|
|
|
|
p: plugin
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
function makeConfig(name) {
|
|
|
|
return function () {
|
|
|
|
return (config && config.config && config.config[name]) || {};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
handlers = {
|
|
|
|
require: function (name) {
|
|
|
|
return makeRequire(name);
|
|
|
|
},
|
|
|
|
exports: function (name) {
|
|
|
|
var e = defined[name];
|
|
|
|
if (typeof e !== 'undefined') {
|
|
|
|
return e;
|
|
|
|
} else {
|
|
|
|
return (defined[name] = {});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
module: function (name) {
|
|
|
|
return {
|
|
|
|
id: name,
|
|
|
|
uri: '',
|
|
|
|
exports: defined[name],
|
|
|
|
config: makeConfig(name)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
main = function (name, deps, callback, relName) {
|
|
|
|
var cjsModule, depName, ret, map, i,
|
|
|
|
args = [],
|
|
|
|
usingExports;
|
|
|
|
|
|
|
|
//Use name if no relName
|
|
|
|
relName = relName || name;
|
|
|
|
|
|
|
|
//Call the callback to define the module, if necessary.
|
|
|
|
if (typeof callback === 'function') {
|
|
|
|
|
|
|
|
//Pull out the defined dependencies and pass the ordered
|
|
|
|
//values to the callback.
|
|
|
|
//Default to [require, exports, module] if no deps
|
|
|
|
deps = !deps.length && callback.length ? ['require', 'exports', 'module'] : deps;
|
|
|
|
for (i = 0; i < deps.length; i += 1) {
|
|
|
|
map = makeMap(deps[i], relName);
|
|
|
|
depName = map.f;
|
|
|
|
|
|
|
|
//Fast path CommonJS standard dependencies.
|
|
|
|
if (depName === "require") {
|
|
|
|
args[i] = handlers.require(name);
|
|
|
|
} else if (depName === "exports") {
|
|
|
|
//CommonJS module spec 1.1
|
|
|
|
args[i] = handlers.exports(name);
|
|
|
|
usingExports = true;
|
|
|
|
} else if (depName === "module") {
|
|
|
|
//CommonJS module spec 1.1
|
|
|
|
cjsModule = args[i] = handlers.module(name);
|
|
|
|
} else if (hasProp(defined, depName) ||
|
|
|
|
hasProp(waiting, depName) ||
|
|
|
|
hasProp(defining, depName)) {
|
|
|
|
args[i] = callDep(depName);
|
|
|
|
} else if (map.p) {
|
|
|
|
map.p.load(map.n, makeRequire(relName, true), makeLoad(depName), {});
|
|
|
|
args[i] = defined[depName];
|
|
|
|
} else {
|
|
|
|
throw new Error(name + ' missing ' + depName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = callback.apply(defined[name], args);
|
|
|
|
|
|
|
|
if (name) {
|
|
|
|
//If setting exports via "module" is in play,
|
|
|
|
//favor that over return value and exports. After that,
|
|
|
|
//favor a non-undefined return value over exports use.
|
|
|
|
if (cjsModule && cjsModule.exports !== undef &&
|
|
|
|
cjsModule.exports !== defined[name]) {
|
|
|
|
defined[name] = cjsModule.exports;
|
|
|
|
} else if (ret !== undef || !usingExports) {
|
|
|
|
//Use the return value from the function.
|
|
|
|
defined[name] = ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (name) {
|
|
|
|
//May just be an object definition for the module. Only
|
|
|
|
//worry about defining if have a module name.
|
|
|
|
defined[name] = callback;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
requirejs = require = req = function (deps, callback, relName, forceSync, alt) {
|
|
|
|
if (typeof deps === "string") {
|
|
|
|
if (handlers[deps]) {
|
|
|
|
//callback in this case is really relName
|
|
|
|
return handlers[deps](callback);
|
|
|
|
}
|
|
|
|
//Just return the module wanted. In this scenario, the
|
|
|
|
//deps arg is the module name, and second arg (if passed)
|
|
|
|
//is just the relName.
|
|
|
|
//Normalize module name, if it contains . or ..
|
|
|
|
return callDep(makeMap(deps, callback).f);
|
|
|
|
} else if (!deps.splice) {
|
|
|
|
//deps is a config object, not an array.
|
|
|
|
config = deps;
|
|
|
|
if (callback.splice) {
|
|
|
|
//callback is an array, which means it is a dependency list.
|
|
|
|
//Adjust args if there are dependencies
|
|
|
|
deps = callback;
|
|
|
|
callback = relName;
|
|
|
|
relName = null;
|
|
|
|
} else {
|
|
|
|
deps = undef;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Support require(['a'])
|
|
|
|
callback = callback || function () {};
|
|
|
|
|
|
|
|
//If relName is a function, it is an errback handler,
|
|
|
|
//so remove it.
|
|
|
|
if (typeof relName === 'function') {
|
|
|
|
relName = forceSync;
|
|
|
|
forceSync = alt;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Simulate async callback;
|
|
|
|
if (forceSync) {
|
|
|
|
main(undef, deps, callback, relName);
|
|
|
|
} else {
|
|
|
|
//Using a non-zero value because of concern for what old browsers
|
|
|
|
//do, and latest browsers "upgrade" to 4 if lower value is used:
|
|
|
|
//http://www.whatwg.org/specs/web-apps/current-work/multipage/timers.html#dom-windowtimers-settimeout:
|
|
|
|
//If want a value immediately, use require('id') instead -- something
|
|
|
|
//that works in almond on the global level, but not guaranteed and
|
|
|
|
//unlikely to work in other AMD implementations.
|
|
|
|
setTimeout(function () {
|
|
|
|
main(undef, deps, callback, relName);
|
|
|
|
}, 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
return req;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Just drops the config on the floor, but returns req in case
|
|
|
|
* the config return value is used.
|
|
|
|
*/
|
|
|
|
req.config = function (cfg) {
|
|
|
|
config = cfg;
|
|
|
|
if (config.deps) {
|
|
|
|
req(config.deps, config.callback);
|
|
|
|
}
|
|
|
|
return req;
|
|
|
|
};
|
|
|
|
|
|
|
|
define = function (name, deps, callback) {
|
|
|
|
|
|
|
|
//This module may not have dependencies
|
|
|
|
if (!deps.splice) {
|
|
|
|
//deps is not an array, so probably means
|
|
|
|
//an object literal or factory function for
|
|
|
|
//the value. Adjust args.
|
|
|
|
callback = deps;
|
|
|
|
deps = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!hasProp(defined, name) && !hasProp(waiting, name)) {
|
|
|
|
waiting[name] = [name, deps, callback];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
define.amd = {
|
|
|
|
jQuery: true
|
|
|
|
};
|
|
|
|
}());
|
|
|
|
|
|
|
|
define("build/almond", function(){});
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* Lo-Dash v0.8.1 <http://lodash.com>
|
|
|
|
* (c) 2012 John-David Dalton <http://allyoucanleet.com/>
|
|
|
|
* Based on Underscore.js 1.4.1 <http://underscorejs.org>
|
|
|
|
* (c) 2009-2012 Jeremy Ashkenas, DocumentCloud Inc.
|
|
|
|
* Available under MIT license <http://lodash.com/license>
|
|
|
|
*/
|
|
|
|
;(function(window, undefined) {
|
|
|
|
|
|
|
|
|
|
|
|
/** Detect free variable `exports` */
|
|
|
|
var freeExports = typeof exports == 'object' && exports &&
|
|
|
|
(typeof global == 'object' && global && global == global.global && (window = global), exports);
|
|
|
|
|
|
|
|
/** Native prototype shortcuts */
|
|
|
|
var ArrayProto = Array.prototype,
|
|
|
|
BoolProto = Boolean.prototype,
|
|
|
|
ObjectProto = Object.prototype,
|
|
|
|
NumberProto = Number.prototype,
|
|
|
|
StringProto = String.prototype;
|
|
|
|
|
|
|
|
/** Used to generate unique IDs */
|
|
|
|
var idCounter = 0;
|
|
|
|
|
|
|
|
/** Used by `cachedContains` as the default size when optimizations are enabled for large arrays */
|
|
|
|
var largeArraySize = 30;
|
|
|
|
|
|
|
|
/** Used to restore the original `_` reference in `noConflict` */
|
|
|
|
var oldDash = window._;
|
|
|
|
|
|
|
|
/** Used to detect delimiter values that should be processed by `tokenizeEvaluate` */
|
|
|
|
var reComplexDelimiter = /[-?+=!~*%&^<>|{(\/]|\[\D|\b(?:delete|in|instanceof|new|typeof|void)\b/;
|
|
|
|
|
|
|
|
/** Used to match HTML entities */
|
|
|
|
var reEscapedHtml = /&(?:amp|lt|gt|quot|#x27);/g;
|
|
|
|
|
|
|
|
/** Used to match empty string literals in compiled template source */
|
|
|
|
var reEmptyStringLeading = /\b__p \+= '';/g,
|
|
|
|
reEmptyStringMiddle = /\b(__p \+=) '' \+/g,
|
|
|
|
reEmptyStringTrailing = /(__e\(.*?\)|\b__t\)) \+\n'';/g;
|
|
|
|
|
|
|
|
/** Used to match regexp flags from their coerced string values */
|
|
|
|
var reFlags = /\w*$/;
|
|
|
|
|
|
|
|
/** Used to insert the data object variable into compiled template source */
|
|
|
|
var reInsertVariable = /(?:__e|__t = )\(\s*(?![\d\s"']|this\.)/g;
|
|
|
|
|
|
|
|
/** Used to detect if a method is native */
|
|
|
|
var reNative = RegExp('^' +
|
|
|
|
(ObjectProto.valueOf + '')
|
|
|
|
.replace(/[.*+?^=!:${}()|[\]\/\\]/g, '\\$&')
|
|
|
|
.replace(/valueOf|for [^\]]+/g, '.+?') + '$'
|
|
|
|
);
|
|
|
|
|
|
|
|
/** Used to ensure capturing order and avoid matches for undefined delimiters */
|
|
|
|
var reNoMatch = /($^)/;
|
|
|
|
|
|
|
|
/** Used to match HTML characters */
|
|
|
|
var reUnescapedHtml = /[&<>"']/g;
|
|
|
|
|
|
|
|
/** Used to match unescaped characters in compiled string literals */
|
|
|
|
var reUnescapedString = /['\n\r\t\u2028\u2029\\]/g;
|
|
|
|
|
|
|
|
/** Used to fix the JScript [[DontEnum]] bug */
|
|
|
|
var shadowed = [
|
|
|
|
'constructor', 'hasOwnProperty', 'isPrototypeOf', 'propertyIsEnumerable',
|
|
|
|
'toLocaleString', 'toString', 'valueOf'
|
|
|
|
];
|
|
|
|
|
|
|
|
/** Used to make template sourceURLs easier to identify */
|
|
|
|
var templateCounter = 0;
|
|
|
|
|
|
|
|
/** Native method shortcuts */
|
|
|
|
var concat = ArrayProto.concat,
|
|
|
|
hasOwnProperty = ObjectProto.hasOwnProperty,
|
|
|
|
push = ArrayProto.push,
|
|
|
|
propertyIsEnumerable = ObjectProto.propertyIsEnumerable,
|
|
|
|
slice = ArrayProto.slice,
|
|
|
|
toString = ObjectProto.toString;
|
|
|
|
|
|
|
|
/* Native method shortcuts for methods with the same name as other `lodash` methods */
|
|
|
|
var nativeBind = reNative.test(nativeBind = slice.bind) && nativeBind,
|
|
|
|
nativeFloor = Math.floor,
|
|
|
|
nativeGetPrototypeOf = reNative.test(nativeGetPrototypeOf = Object.getPrototypeOf) && nativeGetPrototypeOf,
|
|
|
|
nativeIsArray = reNative.test(nativeIsArray = Array.isArray) && nativeIsArray,
|
|
|
|
nativeIsFinite = window.isFinite,
|
|
|
|
nativeKeys = reNative.test(nativeKeys = Object.keys) && nativeKeys,
|
|
|
|
nativeMax = Math.max,
|
|
|
|
nativeMin = Math.min,
|
|
|
|
nativeRandom = Math.random;
|
|
|
|
|
|
|
|
/** `Object#toString` result shortcuts */
|
|
|
|
var argsClass = '[object Arguments]',
|
|
|
|
arrayClass = '[object Array]',
|
|
|
|
boolClass = '[object Boolean]',
|
|
|
|
dateClass = '[object Date]',
|
|
|
|
funcClass = '[object Function]',
|
|
|
|
numberClass = '[object Number]',
|
|
|
|
objectClass = '[object Object]',
|
|
|
|
regexpClass = '[object RegExp]',
|
|
|
|
stringClass = '[object String]';
|
|
|
|
|
|
|
|
/** Timer shortcuts */
|
|
|
|
var clearTimeout = window.clearTimeout,
|
|
|
|
setTimeout = window.setTimeout;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Detect the JScript [[DontEnum]] bug:
|
|
|
|
*
|
|
|
|
* In IE < 9 an objects own properties, shadowing non-enumerable ones, are
|
|
|
|
* made non-enumerable as well.
|
|
|
|
*/
|
|
|
|
var hasDontEnumBug;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Detect if `Array#shift` and `Array#splice` augment array-like objects
|
|
|
|
* incorrectly:
|
|
|
|
*
|
|
|
|
* Firefox < 10, IE compatibility mode, and IE < 9 have buggy Array `shift()`
|
|
|
|
* and `splice()` functions that fail to remove the last element, `value[0]`,
|
|
|
|
* of array-like objects even though the `length` property is set to `0`.
|
|
|
|
* The `shift()` method is buggy in IE 8 compatibility mode, while `splice()`
|
|
|
|
* is buggy regardless of mode in IE < 9 and buggy in compatibility mode in IE 9.
|
|
|
|
*/
|
|
|
|
var hasObjectSpliceBug;
|
|
|
|
|
|
|
|
/** Detect if own properties are iterated after inherited properties (IE < 9) */
|
|
|
|
var iteratesOwnLast;
|
|
|
|
|
|
|
|
/** Detect if an `arguments` object's indexes are non-enumerable (IE < 9) */
|
|
|
|
var noArgsEnum = true;
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
var object = { '0': 1, 'length': 1 },
|
|
|
|
props = [];
|
|
|
|
|
|
|
|
function ctor() { this.x = 1; }
|
|
|
|
ctor.prototype = { 'valueOf': 1, 'y': 1 };
|
|
|
|
for (var prop in new ctor) { props.push(prop); }
|
|
|
|
for (prop in arguments) { noArgsEnum = !prop; }
|
|
|
|
|
|
|
|
hasDontEnumBug = (props + '').length < 4;
|
|
|
|
iteratesOwnLast = props[0] != 'x';
|
|
|
|
hasObjectSpliceBug = (props.splice.call(object, 0, 1), object[0]);
|
|
|
|
}(1));
|
|
|
|
|
|
|
|
/** Detect if an `arguments` object's [[Class]] is unresolvable (Firefox < 4, IE < 9) */
|
|
|
|
var noArgsClass = !isArguments(arguments);
|
|
|
|
|
|
|
|
/** Detect if `Array#slice` cannot be used to convert strings to arrays (Opera < 10.52) */
|
|
|
|
var noArraySliceOnStrings = slice.call('x')[0] != 'x';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Detect lack of support for accessing string characters by index:
|
|
|
|
*
|
|
|
|
* IE < 8 can't access characters by index and IE 8 can only access
|
|
|
|
* characters by index on string literals.
|
|
|
|
*/
|
|
|
|
var noCharByIndex = ('x'[0] + Object('x')[0]) != 'xx';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Detect if a node's [[Class]] is unresolvable (IE < 9)
|
|
|
|
* and that the JS engine won't error when attempting to coerce an object to
|
|
|
|
* a string without a `toString` property value of `typeof` "function".
|
|
|
|
*/
|
|
|
|
try {
|
|
|
|
var noNodeClass = ({ 'toString': 0 } + '', toString.call(window.document || 0) == objectClass);
|
|
|
|
} catch(e) { }
|
|
|
|
|
|
|
|
/* Detect if `Function#bind` exists and is inferred to be fast (all but V8) */
|
|
|
|
var isBindFast = nativeBind && /\n|Opera/.test(nativeBind + toString.call(window.opera));
|
|
|
|
|
|
|
|
/* Detect if `Object.keys` exists and is inferred to be fast (IE, Opera, V8) */
|
|
|
|
var isKeysFast = nativeKeys && /^.+$|true/.test(nativeKeys + !!window.attachEvent);
|
|
|
|
|
|
|
|
/* Detect if strict mode, "use strict", is inferred to be fast (V8) */
|
|
|
|
var isStrictFast = !isBindFast;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Detect if sourceURL syntax is usable without erroring:
|
|
|
|
*
|
|
|
|
* The JS engine in Adobe products, like InDesign, will throw a syntax error
|
|
|
|
* when it encounters a single line comment beginning with the `@` symbol.
|
|
|
|
*
|
|
|
|
* The JS engine in Narwhal will generate the function `function anonymous(){//}`
|
|
|
|
* and throw a syntax error.
|
|
|
|
*
|
|
|
|
* Avoid comments beginning `@` symbols in IE because they are part of its
|
|
|
|
* non-standard conditional compilation support.
|
|
|
|
* http://msdn.microsoft.com/en-us/library/121hztk3(v=vs.94).aspx
|
|
|
|
*/
|
|
|
|
try {
|
|
|
|
var useSourceURL = (Function('//@')(), !window.attachEvent);
|
|
|
|
} catch(e) { }
|
|
|
|
|
|
|
|
/** Used to identify object classifications that are array-like */
|
|
|
|
var arrayLikeClasses = {};
|
|
|
|
arrayLikeClasses[boolClass] = arrayLikeClasses[dateClass] = arrayLikeClasses[funcClass] =
|
|
|
|
arrayLikeClasses[numberClass] = arrayLikeClasses[objectClass] = arrayLikeClasses[regexpClass] = false;
|
|
|
|
arrayLikeClasses[argsClass] = arrayLikeClasses[arrayClass] = arrayLikeClasses[stringClass] = true;
|
|
|
|
|
|
|
|
/** Used to identify object classifications that `_.clone` supports */
|
|
|
|
var cloneableClasses = {};
|
|
|
|
cloneableClasses[argsClass] = cloneableClasses[funcClass] = false;
|
|
|
|
cloneableClasses[arrayClass] = cloneableClasses[boolClass] = cloneableClasses[dateClass] =
|
|
|
|
cloneableClasses[numberClass] = cloneableClasses[objectClass] = cloneableClasses[regexpClass] =
|
|
|
|
cloneableClasses[stringClass] = true;
|
|
|
|
|
|
|
|
/** Used to determine if values are of the language type Object */
|
|
|
|
var objectTypes = {
|
|
|
|
'boolean': false,
|
|
|
|
'function': true,
|
|
|
|
'object': true,
|
|
|
|
'number': false,
|
|
|
|
'string': false,
|
|
|
|
'undefined': false,
|
|
|
|
'unknown': true
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Used to escape characters for inclusion in compiled string literals */
|
|
|
|
var stringEscapes = {
|
|
|
|
'\\': '\\',
|
|
|
|
"'": "'",
|
|
|
|
'\n': 'n',
|
|
|
|
'\r': 'r',
|
|
|
|
'\t': 't',
|
|
|
|
'\u2028': 'u2028',
|
|
|
|
'\u2029': 'u2029'
|
|
|
|
};
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The `lodash` function.
|
|
|
|
*
|
|
|
|
* @name _
|
|
|
|
* @constructor
|
|
|
|
* @param {Mixed} value The value to wrap in a `lodash` instance.
|
|
|
|
* @returns {Object} Returns a `lodash` instance.
|
|
|
|
*/
|
|
|
|
function lodash(value) {
|
|
|
|
// exit early if already wrapped
|
|
|
|
if (value && value.__wrapped__) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
// allow invoking `lodash` without the `new` operator
|
|
|
|
if (!(this instanceof lodash)) {
|
|
|
|
return new lodash(value);
|
|
|
|
}
|
|
|
|
this.__wrapped__ = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* By default, the template delimiters used by Lo-Dash are similar to those in
|
|
|
|
* embedded Ruby (ERB). Change the following template settings to use alternative
|
|
|
|
* delimiters.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @type Object
|
|
|
|
*/
|
|
|
|
lodash.templateSettings = {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to detect `data` property values to be HTML-escaped.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _.templateSettings
|
|
|
|
* @type RegExp
|
|
|
|
*/
|
|
|
|
'escape': /<%-([\s\S]+?)%>/g,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to detect code to be evaluated.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _.templateSettings
|
|
|
|
* @type RegExp
|
|
|
|
*/
|
|
|
|
'evaluate': /<%([\s\S]+?)%>/g,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to detect `data` property values to inject.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _.templateSettings
|
|
|
|
* @type RegExp
|
|
|
|
*/
|
|
|
|
'interpolate': /<%=([\s\S]+?)%>/g,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to reference the data object in the template text.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _.templateSettings
|
|
|
|
* @type String
|
|
|
|
*/
|
|
|
|
'variable': ''
|
|
|
|
};
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The template used to create iterator functions.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param {Obect} data The data object used to populate the text.
|
|
|
|
* @returns {String} Returns the interpolated text.
|
|
|
|
*/
|
|
|
|
var iteratorTemplate = template(
|
|
|
|
// conditional strict mode
|
|
|
|
'<% if (useStrict) { %>\'use strict\';\n<% } %>' +
|
|
|
|
|
|
|
|
// the `iteratee` may be reassigned by the `top` snippet
|
|
|
|
'var index, value, iteratee = <%= firstArg %>, ' +
|
|
|
|
// assign the `result` variable an initial value
|
|
|
|
'result<% if (init) { %> = <%= init %><% } %>;\n' +
|
|
|
|
// exit early if the first argument is falsey
|
|
|
|
'if (!<%= firstArg %>) return result;\n' +
|
|
|
|
// add code before the iteration branches
|
|
|
|
'<%= top %>;\n' +
|
|
|
|
|
|
|
|
// the following branch is for iterating arrays and array-like objects
|
|
|
|
'<% if (arrayBranch) { %>' +
|
|
|
|
'var length = iteratee.length; index = -1;' +
|
|
|
|
' <% if (objectBranch) { %>\nif (length === +length) {<% } %>' +
|
|
|
|
|
|
|
|
// add support for accessing string characters by index if needed
|
|
|
|
' <% if (noCharByIndex) { %>\n' +
|
|
|
|
' if (toString.call(iteratee) == stringClass) {\n' +
|
|
|
|
' iteratee = iteratee.split(\'\')\n' +
|
|
|
|
' }' +
|
|
|
|
' <% } %>\n' +
|
|
|
|
|
|
|
|
' <%= arrayBranch.beforeLoop %>;\n' +
|
|
|
|
' while (++index < length) {\n' +
|
|
|
|
' value = iteratee[index];\n' +
|
|
|
|
' <%= arrayBranch.inLoop %>\n' +
|
|
|
|
' }' +
|
|
|
|
' <% if (objectBranch) { %>\n}<% } %>' +
|
|
|
|
'<% } %>' +
|
|
|
|
|
|
|
|
// the following branch is for iterating an object's own/inherited properties
|
|
|
|
'<% if (objectBranch) { %>' +
|
|
|
|
' <% if (arrayBranch) { %>\nelse {' +
|
|
|
|
|
|
|
|
// add support for iterating over `arguments` objects if needed
|
|
|
|
' <% } else if (noArgsEnum) { %>\n' +
|
|
|
|
' var length = iteratee.length; index = -1;\n' +
|
|
|
|
' if (length && isArguments(iteratee)) {\n' +
|
|
|
|
' while (++index < length) {\n' +
|
|
|
|
' value = iteratee[index += \'\'];\n' +
|
|
|
|
' <%= objectBranch.inLoop %>\n' +
|
|
|
|
' }\n' +
|
|
|
|
' } else {' +
|
|
|
|
' <% } %>' +
|
|
|
|
|
|
|
|
// Firefox < 3.6, Opera > 9.50 - Opera < 11.60, and Safari < 5.1
|
|
|
|
// (if the prototype or a property on the prototype has been set)
|
|
|
|
// incorrectly sets a function's `prototype` property [[Enumerable]]
|
|
|
|
// value to `true`. Because of this Lo-Dash standardizes on skipping
|
|
|
|
// the the `prototype` property of functions regardless of its
|
|
|
|
// [[Enumerable]] value.
|
|
|
|
' <% if (!hasDontEnumBug) { %>\n' +
|
|
|
|
' var skipProto = typeof iteratee == \'function\' && \n' +
|
|
|
|
' propertyIsEnumerable.call(iteratee, \'prototype\');\n' +
|
|
|
|
' <% } %>' +
|
|
|
|
|
|
|
|
// iterate own properties using `Object.keys` if it's fast
|
|
|
|
' <% if (isKeysFast && useHas) { %>\n' +
|
|
|
|
' var ownIndex = -1,\n' +
|
|
|
|
' ownProps = objectTypes[typeof iteratee] ? nativeKeys(iteratee) : [],\n' +
|
|
|
|
' length = ownProps.length;\n\n' +
|
|
|
|
' <%= objectBranch.beforeLoop %>;\n' +
|
|
|
|
' while (++ownIndex < length) {\n' +
|
|
|
|
' index = ownProps[ownIndex];\n' +
|
|
|
|
' <% if (!hasDontEnumBug) { %>if (!(skipProto && index == \'prototype\')) {\n <% } %>' +
|
|
|
|
' value = iteratee[index];\n' +
|
|
|
|
' <%= objectBranch.inLoop %>\n' +
|
|
|
|
' <% if (!hasDontEnumBug) { %>}\n<% } %>' +
|
|
|
|
' }' +
|
|
|
|
|
|
|
|
// else using a for-in loop
|
|
|
|
' <% } else { %>\n' +
|
|
|
|
' <%= objectBranch.beforeLoop %>;\n' +
|
|
|
|
' for (index in iteratee) {<%' +
|
|
|
|
' if (!hasDontEnumBug || useHas) { %>\n if (<%' +
|
|
|
|
' if (!hasDontEnumBug) { %>!(skipProto && index == \'prototype\')<% }' +
|
|
|
|
' if (!hasDontEnumBug && useHas) { %> && <% }' +
|
|
|
|
' if (useHas) { %>hasOwnProperty.call(iteratee, index)<% }' +
|
|
|
|
' %>) {' +
|
|
|
|
' <% } %>\n' +
|
|
|
|
' value = iteratee[index];\n' +
|
|
|
|
' <%= objectBranch.inLoop %>;' +
|
|
|
|
' <% if (!hasDontEnumBug || useHas) { %>\n }<% } %>\n' +
|
|
|
|
' }' +
|
|
|
|
' <% } %>' +
|
|
|
|
|
|
|
|
// Because IE < 9 can't set the `[[Enumerable]]` attribute of an
|
|
|
|
// existing property and the `constructor` property of a prototype
|
|
|
|
// defaults to non-enumerable, Lo-Dash skips the `constructor`
|
|
|
|
// property when it infers it's iterating over a `prototype` object.
|
|
|
|
' <% if (hasDontEnumBug) { %>\n\n' +
|
|
|
|
' var ctor = iteratee.constructor;\n' +
|
|
|
|
' <% for (var k = 0; k < 7; k++) { %>\n' +
|
|
|
|
' index = \'<%= shadowed[k] %>\';\n' +
|
|
|
|
' if (<%' +
|
|
|
|
' if (shadowed[k] == \'constructor\') {' +
|
|
|
|
' %>!(ctor && ctor.prototype === iteratee) && <%' +
|
|
|
|
' } %>hasOwnProperty.call(iteratee, index)) {\n' +
|
|
|
|
' value = iteratee[index];\n' +
|
|
|
|
' <%= objectBranch.inLoop %>\n' +
|
|
|
|
' }' +
|
|
|
|
' <% } %>' +
|
|
|
|
' <% } %>' +
|
|
|
|
' <% if (arrayBranch || noArgsEnum) { %>\n}<% } %>' +
|
|
|
|
'<% } %>\n' +
|
|
|
|
|
|
|
|
// add code to the bottom of the iteration function
|
|
|
|
'<%= bottom %>;\n' +
|
|
|
|
// finally, return the `result`
|
|
|
|
'return result'
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reusable iterator options shared by
|
|
|
|
* `countBy`, `every`, `filter`, `find`, `forEach`, `forIn`, `forOwn`, `groupBy`,
|
|
|
|
* `map`, `reject`, `some`, and `sortBy`.
|
|
|
|
*/
|
|
|
|
var baseIteratorOptions = {
|
|
|
|
'args': 'collection, callback, thisArg',
|
|
|
|
'init': 'collection',
|
|
|
|
'top': 'callback = createCallback(callback, thisArg)',
|
|
|
|
'inLoop': 'if (callback(value, index, collection) === false) return result'
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Reusable iterator options for `countBy`, `groupBy`, and `sortBy` */
|
|
|
|
var countByIteratorOptions = {
|
|
|
|
'init': '{}',
|
|
|
|
'top': 'callback = createCallback(callback, thisArg)',
|
|
|
|
'inLoop':
|
|
|
|
'var prop = callback(value, index, collection);\n' +
|
|
|
|
'(hasOwnProperty.call(result, prop) ? result[prop]++ : result[prop] = 1)'
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Reusable iterator options for `every` and `some` */
|
|
|
|
var everyIteratorOptions = {
|
|
|
|
'init': 'true',
|
|
|
|
'inLoop': 'if (!callback(value, index, collection)) return !result'
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Reusable iterator options for `defaults` and `extend` */
|
|
|
|
var extendIteratorOptions = {
|
|
|
|
'useHas': false,
|
|
|
|
'useStrict': false,
|
|
|
|
'args': 'object',
|
|
|
|
'init': 'object',
|
|
|
|
'top':
|
|
|
|
'for (var argsIndex = 1, argsLength = arguments.length; argsIndex < argsLength; argsIndex++) {\n' +
|
|
|
|
' if (iteratee = arguments[argsIndex]) {',
|
|
|
|
'inLoop': 'result[index] = value',
|
|
|
|
'bottom': ' }\n}'
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Reusable iterator options for `filter`, `reject`, and `where` */
|
|
|
|
var filterIteratorOptions = {
|
|
|
|
'init': '[]',
|
|
|
|
'inLoop': 'callback(value, index, collection) && result.push(value)'
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Reusable iterator options for `find`, `forEach`, `forIn`, and `forOwn` */
|
|
|
|
var forEachIteratorOptions = {
|
|
|
|
'top': 'callback = createCallback(callback, thisArg)'
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Reusable iterator options for `forIn` and `forOwn` */
|
|
|
|
var forOwnIteratorOptions = {
|
|
|
|
'inLoop': {
|
|
|
|
'object': baseIteratorOptions.inLoop
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Reusable iterator options for `invoke`, `map`, `pluck`, and `sortBy` */
|
|
|
|
var mapIteratorOptions = {
|
|
|
|
'init': false,
|
|
|
|
'beforeLoop': {
|
|
|
|
'array': 'result = Array(length)',
|
|
|
|
'object': 'result = ' + (isKeysFast ? 'Array(length)' : '[]')
|
|
|
|
},
|
|
|
|
'inLoop': {
|
|
|
|
'array': 'result[index] = callback(value, index, collection)',
|
|
|
|
'object': 'result' + (isKeysFast ? '[ownIndex] = ' : '.push') + '(callback(value, index, collection))'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Reusable iterator options for `omit` and `pick` */
|
|
|
|
var omitIteratorOptions = {
|
|
|
|
'useHas': false,
|
|
|
|
'args': 'object, callback, thisArg',
|
|
|
|
'init': '{}',
|
|
|
|
'top':
|
|
|
|
'var isFunc = typeof callback == \'function\';\n' +
|
|
|
|
'if (isFunc) callback = createCallback(callback, thisArg);\n' +
|
|
|
|
'else var props = concat.apply(ArrayProto, arguments)',
|
|
|
|
'inLoop':
|
|
|
|
'if (isFunc\n' +
|
|
|
|
' ? !callback(value, index, object)\n' +
|
|
|
|
' : indexOf(props, index) < 0\n' +
|
|
|
|
') result[index] = value'
|
|
|
|
};
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a function optimized for searching large arrays for a given `value`,
|
|
|
|
* starting at `fromIndex`, using strict equality for comparisons, i.e. `===`.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param {Array} array The array to search.
|
|
|
|
* @param {Mixed} value The value to search for.
|
|
|
|
* @param {Number} [fromIndex=0] The index to start searching from.
|
|
|
|
* @param {Number} [largeSize=30] The length at which an array is considered large.
|
|
|
|
* @returns {Boolean} Returns `true` if `value` is found, else `false`.
|
|
|
|
*/
|
|
|
|
function cachedContains(array, fromIndex, largeSize) {
|
|
|
|
fromIndex || (fromIndex = 0);
|
|
|
|
|
|
|
|
var length = array.length,
|
|
|
|
isLarge = (length - fromIndex) >= (largeSize || largeArraySize),
|
|
|
|
cache = isLarge ? {} : array;
|
|
|
|
|
|
|
|
if (isLarge) {
|
|
|
|
// init value cache
|
|
|
|
var key,
|
|
|
|
index = fromIndex - 1;
|
|
|
|
|
|
|
|
while (++index < length) {
|
|
|
|
// manually coerce `value` to string because `hasOwnProperty`, in some
|
|
|
|
// older versions of Firefox, coerces objects incorrectly
|
|
|
|
key = array[index] + '';
|
|
|
|
(hasOwnProperty.call(cache, key) ? cache[key] : (cache[key] = [])).push(array[index]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return function(value) {
|
|
|
|
if (isLarge) {
|
|
|
|
var key = value + '';
|
|
|
|
return hasOwnProperty.call(cache, key) && indexOf(cache[key], value) > -1;
|
|
|
|
}
|
|
|
|
return indexOf(cache, value, fromIndex) > -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used by `sortBy` to compare transformed `collection` values, stable sorting
|
|
|
|
* them in ascending order.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param {Object} a The object to compare to `b`.
|
|
|
|
* @param {Object} b The object to compare to `a`.
|
|
|
|
* @returns {Number} Returns the sort order indicator of `1` or `-1`.
|
|
|
|
*/
|
|
|
|
function compareAscending(a, b) {
|
|
|
|
var ai = a.index,
|
|
|
|
bi = b.index;
|
|
|
|
|
|
|
|
a = a.criteria;
|
|
|
|
b = b.criteria;
|
|
|
|
|
|
|
|
// ensure a stable sort in V8 and other engines
|
|
|
|
// http://code.google.com/p/v8/issues/detail?id=90
|
|
|
|
if (a !== b) {
|
|
|
|
if (a > b || a === undefined) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (a < b || b === undefined) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ai < bi ? -1 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a function that, when called, invokes `func` with the `this`
|
|
|
|
* binding of `thisArg` and prepends any `partailArgs` to the arguments passed
|
|
|
|
* to the bound function.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param {Function|String} func The function to bind or the method name.
|
|
|
|
* @param {Mixed} [thisArg] The `this` binding of `func`.
|
|
|
|
* @param {Array} partialArgs An array of arguments to be partially applied.
|
|
|
|
* @returns {Function} Returns the new bound function.
|
|
|
|
*/
|
|
|
|
function createBound(func, thisArg, partialArgs) {
|
|
|
|
var isFunc = isFunction(func),
|
|
|
|
isPartial = !partialArgs,
|
|
|
|
methodName = func;
|
|
|
|
|
|
|
|
// juggle arguments
|
|
|
|
if (isPartial) {
|
|
|
|
partialArgs = thisArg;
|
|
|
|
}
|
|
|
|
|
|
|
|
function bound() {
|
|
|
|
// `Function#bind` spec
|
|
|
|
// http://es5.github.com/#x15.3.4.5
|
|
|
|
var args = arguments,
|
|
|
|
thisBinding = isPartial ? this : thisArg;
|
|
|
|
|
|
|
|
if (!isFunc) {
|
|
|
|
func = thisArg[methodName];
|
|
|
|
}
|
|
|
|
if (partialArgs.length) {
|
|
|
|
args = args.length
|
|
|
|
? partialArgs.concat(slice.call(args))
|
|
|
|
: partialArgs;
|
|
|
|
}
|
|
|
|
if (this instanceof bound) {
|
|
|
|
// get `func` instance if `bound` is invoked in a `new` expression
|
|
|
|
noop.prototype = func.prototype;
|
|
|
|
thisBinding = new noop;
|
|
|
|
|
|
|
|
// mimic the constructor's `return` behavior
|
|
|
|
// http://es5.github.com/#x13.2.2
|
|
|
|
var result = func.apply(thisBinding, args);
|
|
|
|
return result && objectTypes[typeof result]
|
|
|
|
? result
|
|
|
|
: thisBinding
|
|
|
|
}
|
|
|
|
return func.apply(thisBinding, args);
|
|
|
|
}
|
|
|
|
return bound;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Produces an iteration callback bound to an optional `thisArg`. If `func` is
|
|
|
|
* a property name, the callback will return the property value for a given element.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param {Function|String} [func=identity|property] The function called per
|
|
|
|
* iteration or property name to query.
|
|
|
|
* @param {Mixed} [thisArg] The `this` binding of `callback`.
|
|
|
|
* @returns {Function} Returns a callback function.
|
|
|
|
*/
|
|
|
|
function createCallback(func, thisArg) {
|
|
|
|
if (!func) {
|
|
|
|
return identity;
|
|
|
|
}
|
|
|
|
if (typeof func != 'function') {
|
|
|
|
return function(object) {
|
|
|
|
return object[func];
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (thisArg !== undefined) {
|
|
|
|
return function(value, index, object) {
|
|
|
|
return func.call(thisArg, value, index, object);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return func;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates compiled iteration functions. The iteration function will be created
|
|
|
|
* to iterate over only objects if the first argument of `options.args` is
|
|
|
|
* "object" or `options.inLoop.array` is falsey.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param {Object} [options1, options2, ...] The compile options objects.
|
|
|
|
*
|
|
|
|
* useHas - A boolean to specify whether or not to use `hasOwnProperty` checks
|
|
|
|
* in the object loop.
|
|
|
|
*
|
|
|
|
* useStrict - A boolean to specify whether or not to include the ES5
|
|
|
|
* "use strict" directive.
|
|
|
|
*
|
|
|
|
* args - A string of comma separated arguments the iteration function will accept.
|
|
|
|
*
|
|
|
|
* init - A string to specify the initial value of the `result` variable.
|
|
|
|
*
|
|
|
|
* top - A string of code to execute before the iteration branches.
|
|
|
|
*
|
|
|
|
* beforeLoop - A string or object containing an "array" or "object" property
|
|
|
|
* of code to execute before the array or object loops.
|
|
|
|
*
|
|
|
|
* inLoop - A string or object containing an "array" or "object" property
|
|
|
|
* of code to execute in the array or object loops.
|
|
|
|
*
|
|
|
|
* bottom - A string of code to execute after the iteration branches but
|
|
|
|
* before the `result` is returned.
|
|
|
|
*
|
|
|
|
* @returns {Function} Returns the compiled function.
|
|
|
|
*/
|
|
|
|
function createIterator() {
|
|
|
|
var index = -1,
|
|
|
|
length = arguments.length;
|
|
|
|
|
|
|
|
// merge options into a template data object
|
|
|
|
var data = {
|
|
|
|
'bottom': '',
|
|
|
|
'hasDontEnumBug': hasDontEnumBug,
|
|
|
|
'isKeysFast': isKeysFast,
|
|
|
|
'noArgsEnum': noArgsEnum,
|
|
|
|
'noCharByIndex': noCharByIndex,
|
|
|
|
'shadowed': shadowed,
|
|
|
|
'top': '',
|
|
|
|
'useHas': true,
|
|
|
|
'useStrict': isStrictFast,
|
|
|
|
'arrayBranch': {},
|
|
|
|
'objectBranch': {}
|
|
|
|
};
|
|
|
|
|
|
|
|
while (++index < length) {
|
|
|
|
var object = arguments[index];
|
|
|
|
for (var prop in object) {
|
|
|
|
var value = object[prop];
|
|
|
|
// keep this regexp explicit for the build pre-process
|
|
|
|
if (/beforeLoop|inLoop/.test(prop)) {
|
|
|
|
if (typeof value == 'string') {
|
|
|
|
value = { 'array': value, 'object': value };
|
|
|
|
}
|
|
|
|
data.arrayBranch[prop] = value.array;
|
|
|
|
data.objectBranch[prop] = value.object;
|
|
|
|
} else {
|
|
|
|
data[prop] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// set additional template `data` values
|
|
|
|
var args = data.args,
|
|
|
|
firstArg = /^[^,]+/.exec(args)[0],
|
|
|
|
init = data.init;
|
|
|
|
|
|
|
|
data.firstArg = firstArg;
|
|
|
|
data.init = init == null ? firstArg : init;
|
|
|
|
|
|
|
|
if (firstArg != 'collection' || !data.arrayBranch.inLoop) {
|
|
|
|
data.arrayBranch = null;
|
|
|
|
}
|
|
|
|
// create the function factory
|
|
|
|
var factory = Function(
|
|
|
|
'arrayLikeClasses, ArrayProto, bind, compareAscending, concat, createCallback, ' +
|
|
|
|
'forIn, hasOwnProperty, indexOf, isArguments, isArray, isFunction, ' +
|
|
|
|
'isPlainObject, objectClass, objectTypes, nativeKeys, propertyIsEnumerable, ' +
|
|
|
|
'slice, stringClass, toString, undefined',
|
|
|
|
'var callee = function(' + args + ') {\n' + iteratorTemplate(data) + '\n};\n' +
|
|
|
|
'return callee'
|
|
|
|
);
|
|
|
|
// return the compiled function
|
|
|
|
return factory(
|
|
|
|
arrayLikeClasses, ArrayProto, bind, compareAscending, concat, createCallback,
|
|
|
|
forIn, hasOwnProperty, indexOf, isArguments, isArray, isFunction,
|
|
|
|
isPlainObject, objectClass, objectTypes, nativeKeys, propertyIsEnumerable,
|
|
|
|
slice, stringClass, toString
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used by `template` to escape characters for inclusion in compiled
|
|
|
|
* string literals.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param {String} match The matched character to escape.
|
|
|
|
* @returns {String} Returns the escaped character.
|
|
|
|
*/
|
|
|
|
function escapeStringChar(match) {
|
|
|
|
return '\\' + stringEscapes[match];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used by `escape` to convert characters to HTML entities.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param {String} match The matched character to escape.
|
|
|
|
* @returns {String} Returns the escaped character.
|
|
|
|
*/
|
|
|
|
function escapeHtmlChar(match) {
|
|
|
|
return htmlEscapes[match];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A no-operation function.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
function noop() {
|
|
|
|
// no operation performed
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used by `unescape` to convert HTML entities to characters.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param {String} match The matched character to unescape.
|
|
|
|
* @returns {String} Returns the unescaped character.
|
|
|
|
*/
|
|
|
|
function unescapeHtmlChar(match) {
|
|
|
|
return htmlUnescapes[match];
|
|
|
|
}
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an object composed of the inverted keys and values of the given `object`.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Objects
|
|
|
|
* @param {Object} object The object to invert.
|
|
|
|
* @returns {Object} Returns the created inverted object.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* _.invert({ 'first': 'Moe', 'second': 'Larry', 'third': 'Curly' });
|
|
|
|
* // => { 'Moe': 'first', 'Larry': 'second', 'Curly': 'third' } (order is not guaranteed)
|
|
|
|
*/
|
|
|
|
var invert = createIterator({
|
|
|
|
'args': 'object',
|
|
|
|
'init': '{}',
|
|
|
|
'inLoop': 'result[value] = index'
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if `value` is an `arguments` object.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Objects
|
|
|
|
* @param {Mixed} value The value to check.
|
|
|
|
* @returns {Boolean} Returns `true` if the `value` is an `arguments` object, else `false`.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* (function() { return _.isArguments(arguments); })(1, 2, 3);
|
|
|
|
* // => true
|
|
|
|
*
|
|
|
|
* _.isArguments([1, 2, 3]);
|
|
|
|
* // => false
|
|
|
|
*/
|
|
|
|
function isArguments(value) {
|
|
|
|
return toString.call(value) == argsClass;
|
|
|
|
}
|
|
|
|
// fallback for browsers that can't detect `arguments` objects by [[Class]]
|
|
|
|
if (noArgsClass) {
|
|
|
|
isArguments = function(value) {
|
|
|
|
return value ? hasOwnProperty.call(value, 'callee') : false;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if `value` is an array.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Objects
|
|
|
|
* @param {Mixed} value The value to check.
|
|
|
|
* @returns {Boolean} Returns `true` if the `value` is an array, else `false`.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* (function() { return _.isArray(arguments); })();
|
|
|
|
* // => false
|
|
|
|
*
|
|
|
|
* _.isArray([1, 2, 3]);
|
|
|
|
* // => true
|
|
|
|
*/
|
|
|
|
var isArray = nativeIsArray || function(value) {
|
|
|
|
return toString.call(value) == arrayClass;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if `value` is a function.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Objects
|
|
|
|
* @param {Mixed} value The value to check.
|
|
|
|
* @returns {Boolean} Returns `true` if the `value` is a function, else `false`.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* _.isFunction(_);
|
|
|
|
* // => true
|
|
|
|
*/
|
|
|
|
function isFunction(value) {
|
|
|
|
return typeof value == 'function';
|
|
|
|
}
|
|
|
|
// fallback for older versions of Chrome and Safari
|
|
|
|
if (isFunction(/x/)) {
|
|
|
|
isFunction = function(value) {
|
|
|
|
return toString.call(value) == funcClass;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if a given `value` is an object created by the `Object` constructor.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Objects
|
|
|
|
* @param {Mixed} value The value to check.
|
|
|
|
* @returns {Boolean} Returns `true` if `value` is a plain object, else `false`.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* function Stooge(name, age) {
|
|
|
|
* this.name = name;
|
|
|
|
* this.age = age;
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* _.isPlainObject(new Stooge('moe', 40));
|
|
|
|
* // false
|
|
|
|
*
|
|
|
|
* _.isPlainObject([1, 2, 3]);
|
|
|
|
* // false
|
|
|
|
*
|
|
|
|
* _.isPlainObject({ 'name': 'moe', 'age': 40 });
|
|
|
|
* // => true
|
|
|
|
*/
|
|
|
|
var isPlainObject = !nativeGetPrototypeOf ? isPlainFallback : function(value) {
|
|
|
|
if (!(value && typeof value == 'object')) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
var valueOf = value.valueOf,
|
|
|
|
objProto = typeof valueOf == 'function' && (objProto = nativeGetPrototypeOf(valueOf)) && nativeGetPrototypeOf(objProto);
|
|
|
|
|
|
|
|
return objProto
|
|
|
|
? value == objProto || (nativeGetPrototypeOf(value) == objProto && !isArguments(value))
|
|
|
|
: isPlainFallback(value);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A fallback implementation of `isPlainObject` that checks if a given `value`
|
|
|
|
* is an object created by the `Object` constructor, assuming objects created
|
|
|
|
* by the `Object` constructor have no inherited enumerable properties and that
|
|
|
|
* there are no `Object.prototype` extensions.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param {Mixed} value The value to check.
|
|
|
|
* @returns {Boolean} Returns `true` if `value` is a plain object, else `false`.
|
|
|
|
*/
|
|
|
|
function isPlainFallback(value) {
|
|
|
|
// avoid non-objects and false positives for `arguments` objects
|
|
|
|
var result = false;
|
|
|
|
if (!(value && typeof value == 'object') || isArguments(value)) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
// IE < 9 presents DOM nodes as `Object` objects except they have `toString`
|
|
|
|
// methods that are `typeof` "string" and still can coerce nodes to strings.
|
|
|
|
// Also check that the constructor is `Object` (i.e. `Object instanceof Object`)
|
|
|
|
var ctor = value.constructor;
|
|
|
|
if ((!noNodeClass || !(typeof value.toString != 'function' && typeof (value + '') == 'string')) &&
|
|
|
|
(!isFunction(ctor) || ctor instanceof ctor)) {
|
|
|
|
// IE < 9 iterates inherited properties before own properties. If the first
|
|
|
|
// iterated property is an object's own property then there are no inherited
|
|
|
|
// enumerable properties.
|
|
|
|
if (iteratesOwnLast) {
|
|
|
|
forIn(value, function(value, key, object) {
|
|
|
|
result = !hasOwnProperty.call(object, key);
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
return result === false;
|
|
|
|
}
|
|
|
|
// In most environments an object's own properties are iterated before
|
|
|
|
// its inherited properties. If the last iterated property is an object's
|
|
|
|
// own property then there are no inherited enumerable properties.
|
|
|
|
forIn(value, function(value, key) {
|
|
|
|
result = key;
|
|
|
|
});
|
|
|
|
return result === false || hasOwnProperty.call(value, result);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A shim implementation of `Object.keys` that produces an array of the given
|
|
|
|
* object's own enumerable property names.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param {Object} object The object to inspect.
|
|
|
|
* @returns {Array} Returns a new array of property names.
|
|
|
|
*/
|
|
|
|
var shimKeys = createIterator({
|
|
|
|
'args': 'object',
|
|
|
|
'init': '[]',
|
|
|
|
'inLoop': 'result.push(index)'
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to convert characters to HTML entities:
|
|
|
|
*
|
|
|
|
* Though the `>` character is escaped for symmetry, characters like `>` and `/`
|
|
|
|
* don't require escaping in HTML and have no special meaning unless they're part
|
|
|
|
* of a tag or an unquoted attribute value.
|
|
|
|
* http://mathiasbynens.be/notes/ambiguous-ampersands (under "semi-related fun fact")
|
|
|
|
*/
|
|
|
|
var htmlEscapes = {
|
|
|
|
'&': '&',
|
|
|
|
'<': '<',
|
|
|
|
'>': '>',
|
|
|
|
'"': '"',
|
|
|
|
"'": '''
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Used to convert HTML entities to characters */
|
|
|
|
var htmlUnescapes = invert(htmlEscapes);
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a clone of `value`. If `deep` is `true`, all nested objects will
|
|
|
|
* also be cloned otherwise they will be assigned by reference. Functions, DOM
|
|
|
|
* nodes, `arguments` objects, and objects created by constructors other than
|
|
|
|
* `Object` are **not** cloned.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Objects
|
|
|
|
* @param {Mixed} value The value to clone.
|
|
|
|
* @param {Boolean} deep A flag to indicate a deep clone.
|
|
|
|
* @param- {Object} [guard] Internally used to allow this method to work with
|
|
|
|
* others like `_.map` without using their callback `index` argument for `deep`.
|
|
|
|
* @param- {Array} [stackA=[]] Internally used to track traversed source objects.
|
|
|
|
* @param- {Array} [stackB=[]] Internally used to associate clones with their
|
|
|
|
* source counterparts.
|
|
|
|
* @returns {Mixed} Returns the cloned `value`.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* var stooges = [
|
|
|
|
* { 'name': 'moe', 'age': 40 },
|
|
|
|
* { 'name': 'larry', 'age': 50 },
|
|
|
|
* { 'name': 'curly', 'age': 60 }
|
|
|
|
* ];
|
|
|
|
*
|
|
|
|
* _.clone({ 'name': 'moe' });
|
|
|
|
* // => { 'name': 'moe' }
|
|
|
|
*
|
|
|
|
* var shallow = _.clone(stooges);
|
|
|
|
* shallow[0] === stooges[0];
|
|
|
|
* // => true
|
|
|
|
*
|
|
|
|
* var deep = _.clone(stooges, true);
|
|
|
|
* shallow[0] === stooges[0];
|
|
|
|
* // => false
|
|
|
|
*/
|
|
|
|
function clone(value, deep, guard, stackA, stackB) {
|
|
|
|
if (value == null) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
if (guard) {
|
|
|
|
deep = false;
|
|
|
|
}
|
|
|
|
// inspect [[Class]]
|
|
|
|
var isObj = objectTypes[typeof value];
|
|
|
|
if (isObj) {
|
|
|
|
// don't clone `arguments` objects, functions, or non-object Objects
|
|
|
|
var className = toString.call(value);
|
|
|
|
if (!cloneableClasses[className] || (noArgsClass && isArguments(value))) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
var isArr = className == arrayClass;
|
|
|
|
isObj = isArr || (className == objectClass ? isPlainObject(value) : isObj);
|
|
|
|
}
|
|
|
|
// shallow clone
|
|
|
|
if (!isObj || !deep) {
|
|
|
|
// don't clone functions
|
|
|
|
return isObj
|
|
|
|
? (isArr ? slice.call(value) : extend({}, value))
|
|
|
|
: value;
|
|
|
|
}
|
|
|
|
|
|
|
|
var ctor = value.constructor;
|
|
|
|
switch (className) {
|
|
|
|
case boolClass:
|
|
|
|
return new ctor(value == true);
|
|
|
|
|
|
|
|
case dateClass:
|
|
|
|
return new ctor(+value);
|
|
|
|
|
|
|
|
case numberClass:
|
|
|
|
case stringClass:
|
|
|
|
return new ctor(value);
|
|
|
|
|
|
|
|
case regexpClass:
|
|
|
|
return ctor(value.source, reFlags.exec(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for circular references and return corresponding clone
|
|
|
|
stackA || (stackA = []);
|
|
|
|
stackB || (stackB = []);
|
|
|
|
|
|
|
|
var length = stackA.length;
|
|
|
|
while (length--) {
|
|
|
|
if (stackA[length] == value) {
|
|
|
|
return stackB[length];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// init cloned object
|
|
|
|
var result = isArr ? ctor(length = value.length) : {};
|
|
|
|
|
|
|
|
// add the source value to the stack of traversed objects
|
|
|
|
// and associate it with its clone
|
|
|
|
stackA.push(value);
|
|
|
|
stackB.push(result);
|
|
|
|
|
|
|
|
// recursively populate clone (susceptible to call stack limits)
|
|
|
|
if (isArr) {
|
|
|
|
var index = -1;
|
|
|
|
while (++index < length) {
|
|
|
|
result[index] = clone(value[index], deep, null, stackA, stackB);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
forOwn(value, function(objValue, key) {
|
|
|
|
result[key] = clone(objValue, deep, null, stackA, stackB);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assigns enumerable properties of the default object(s) to the `destination`
|
|
|
|
* object for all `destination` properties that resolve to `null`/`undefined`.
|
|
|
|
* Once a property is set, additional defaults of the same property will be
|
|
|
|
* ignored.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Objects
|
|
|
|
* @param {Object} object The destination object.
|
|
|
|
* @param {Object} [default1, default2, ...] The default objects.
|
|
|
|
* @returns {Object} Returns the destination object.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* var iceCream = { 'flavor': 'chocolate' };
|
|
|
|
* _.defaults(iceCream, { 'flavor': 'vanilla', 'sprinkles': 'rainbow' });
|
|
|
|
* // => { 'flavor': 'chocolate', 'sprinkles': 'rainbow' }
|
|
|
|
*/
|
|
|
|
var defaults = createIterator(extendIteratorOptions, {
|
|
|
|
'inLoop': 'if (result[index] == null) ' + extendIteratorOptions.inLoop
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assigns enumerable properties of the source object(s) to the `destination`
|
|
|
|
* object. Subsequent sources will overwrite propery assignments of previous
|
|
|
|
* sources.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Objects
|
|
|
|
* @param {Object} object The destination object.
|
|
|
|
* @param {Object} [source1, source2, ...] The source objects.
|
|
|
|
* @returns {Object} Returns the destination object.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* _.extend({ 'name': 'moe' }, { 'age': 40 });
|
|
|
|
* // => { 'name': 'moe', 'age': 40 }
|
|
|
|
*/
|
|
|
|
var extend = createIterator(extendIteratorOptions);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Iterates over `object`'s own and inherited enumerable properties, executing
|
|
|
|
* the `callback` for each property. The `callback` is bound to `thisArg` and
|
|
|
|
* invoked with three arguments; (value, key, object). Callbacks may exit iteration
|
|
|
|
* early by explicitly returning `false`.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Objects
|
|
|
|
* @param {Object} object The object to iterate over.
|
|
|
|
* @param {Function} callback The function called per iteration.
|
|
|
|
* @param {Mixed} [thisArg] The `this` binding of `callback`.
|
|
|
|
* @returns {Object} Returns `object`.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* function Dog(name) {
|
|
|
|
* this.name = name;
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* Dog.prototype.bark = function() {
|
|
|
|
* alert('Woof, woof!');
|
|
|
|
* };
|
|
|
|
*
|
|
|
|
* _.forIn(new Dog('Dagny'), function(value, key) {
|
|
|
|
* alert(key);
|
|
|
|
* });
|
|
|
|
* // => alerts 'name' and 'bark' (order is not guaranteed)
|
|
|
|
*/
|
|
|
|
var forIn = createIterator(baseIteratorOptions, forEachIteratorOptions, forOwnIteratorOptions, {
|
|
|
|
'useHas': false
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Iterates over `object`'s own enumerable properties, executing the `callback`
|
|
|
|
* for each property. The `callback` is bound to `thisArg` and invoked with three
|
|
|
|
* arguments; (value, key, object). Callbacks may exit iteration early by explicitly
|
|
|
|
* returning `false`.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Objects
|
|
|
|
* @param {Object} object The object to iterate over.
|
|
|
|
* @param {Function} callback The function called per iteration.
|
|
|
|
* @param {Mixed} [thisArg] The `this` binding of `callback`.
|
|
|
|
* @returns {Object} Returns `object`.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* _.forOwn({ '0': 'zero', '1': 'one', 'length': 2 }, function(num, key) {
|
|
|
|
* alert(key);
|
|
|
|
* });
|
|
|
|
* // => alerts '0', '1', and 'length' (order is not guaranteed)
|
|
|
|
*/
|
|
|
|
var forOwn = createIterator(baseIteratorOptions, forEachIteratorOptions, forOwnIteratorOptions);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a sorted array of all enumerable properties, own and inherited,
|
|
|
|
* of `object` that have function values.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @alias methods
|
|
|
|
* @category Objects
|
|
|
|
* @param {Object} object The object to inspect.
|
|
|
|
* @returns {Array} Returns a new array of property names that have function values.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* _.functions(_);
|
|
|
|
* // => ['all', 'any', 'bind', 'bindAll', 'clone', 'compact', 'compose', ...]
|
|
|
|
*/
|
|
|
|
var functions = createIterator({
|
|
|
|
'useHas': false,
|
|
|
|
'args': 'object',
|
|
|
|
'init': '[]',
|
|
|
|
'inLoop': 'if (isFunction(value)) result.push(index)',
|
|
|
|
'bottom': 'result.sort()'
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the specified object `property` exists and is a direct property,
|
|
|
|
* instead of an inherited property.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Objects
|
|
|
|
* @param {Object} object The object to check.
|
|
|
|
* @param {String} property The property to check for.
|
|
|
|
* @returns {Boolean} Returns `true` if key is a direct property, else `false`.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* _.has({ 'a': 1, 'b': 2, 'c': 3 }, 'b');
|
|
|
|
* // => true
|
|
|
|
*/
|
|
|
|
function has(object, property) {
|
|
|
|
return object ? hasOwnProperty.call(object, property) : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if `value` is a boolean (`true` or `false`) value.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Objects
|
|
|
|
* @param {Mixed} value The value to check.
|
|
|
|
* @returns {Boolean} Returns `true` if the `value` is a boolean value, else `false`.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* _.isBoolean(null);
|
|
|
|
* // => false
|
|
|
|
*/
|
|
|
|
function isBoolean(value) {
|
|
|
|
return value === true || value === false || toString.call(value) == boolClass;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if `value` is a date.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Objects
|
|
|
|
* @param {Mixed} value The value to check.
|
|
|
|
* @returns {Boolean} Returns `true` if the `value` is a date, else `false`.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* _.isDate(new Date);
|
|
|
|
* // => true
|
|
|
|
*/
|
|
|
|
function isDate(value) {
|
|
|
|
return toString.call(value) == dateClass;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if `value` is a DOM element.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Objects
|
|
|
|
* @param {Mixed} value The value to check.
|
|
|
|
* @returns {Boolean} Returns `true` if the `value` is a DOM element, else `false`.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* _.isElement(document.body);
|
|
|
|
* // => true
|
|
|
|
*/
|
|
|
|
function isElement(value) {
|
|
|
|
return value ? value.nodeType === 1 : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if `value` is empty. Arrays, strings, or `arguments` objects with a
|
|
|
|
* length of `0` and objects with no own enumerable properties are considered
|
|
|
|
* "empty".
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Objects
|
|
|
|
* @param {Array|Object|String} value The value to inspect.
|
|
|
|
* @returns {Boolean} Returns `true` if the `value` is empty, else `false`.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* _.isEmpty([1, 2, 3]);
|
|
|
|
* // => false
|
|
|
|
*
|
|
|
|
* _.isEmpty({});
|
|
|
|
* // => true
|
|
|
|
*
|
|
|
|
* _.isEmpty('');
|
|
|
|
* // => true
|
|
|
|
*/
|
|
|
|
var isEmpty = createIterator({
|
|
|
|
'args': 'value',
|
|
|
|
'init': 'true',
|
|
|
|
'top':
|
|
|
|
'var className = toString.call(value),\n' +
|
|
|
|
' length = value.length;\n' +
|
|
|
|
'if (arrayLikeClasses[className]' +
|
|
|
|
(noArgsClass ? ' || isArguments(value)' : '') + ' ||\n' +
|
|
|
|
' (className == objectClass && length === +length &&\n' +
|
|
|
|
' isFunction(value.splice))' +
|
|
|
|
') return !length',
|
|
|
|
'inLoop': {
|
|
|
|
'object': 'return false'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Performs a deep comparison between two values to determine if they are
|
|
|
|
* equivalent to each other.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Objects
|
|
|
|
* @param {Mixed} a The value to compare.
|
|
|
|
* @param {Mixed} b The other value to compare.
|
|
|
|
* @param- {Object} [stackA=[]] Internally used track traversed `a` objects.
|
|
|
|
* @param- {Object} [stackB=[]] Internally used track traversed `b` objects.
|
|
|
|
* @returns {Boolean} Returns `true` if the values are equvalent, else `false`.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* var moe = { 'name': 'moe', 'luckyNumbers': [13, 27, 34] };
|
|
|
|
* var clone = { 'name': 'moe', 'luckyNumbers': [13, 27, 34] };
|
|
|
|
*
|
|
|
|
* moe == clone;
|
|
|
|
* // => false
|
|
|
|
*
|
|
|
|
* _.isEqual(moe, clone);
|
|
|
|
* // => true
|
|
|
|
*/
|
|
|
|
function isEqual(a, b, stackA, stackB) {
|
|
|
|
// a strict comparison is necessary because `null == undefined`
|
|
|
|
if (a == null || b == null) {
|
|
|
|
return a === b;
|
|
|
|
}
|
|
|
|
// exit early for identical values
|
|
|
|
if (a === b) {
|
|
|
|
// treat `+0` vs. `-0` as not equal
|
|
|
|
return a !== 0 || (1 / a == 1 / b);
|
|
|
|
}
|
|
|
|
// unwrap any `lodash` wrapped values
|
|
|
|
if (objectTypes[typeof a] || objectTypes[typeof b]) {
|
|
|
|
a = a.__wrapped__ || a;
|
|
|
|
b = b.__wrapped__ || b;
|
|
|
|
}
|
|
|
|
// compare [[Class]] names
|
|
|
|
var className = toString.call(a);
|
|
|
|
if (className != toString.call(b)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
switch (className) {
|
|
|
|
case boolClass:
|
|
|
|
case dateClass:
|
|
|
|
// coerce dates and booleans to numbers, dates to milliseconds and booleans
|
|
|
|
// to `1` or `0`, treating invalid dates coerced to `NaN` as not equal
|
|
|
|
return +a == +b;
|
|
|
|
|
|
|
|
case numberClass:
|
|
|
|
// treat `NaN` vs. `NaN` as equal
|
|
|
|
return a != +a
|
|
|
|
? b != +b
|
|
|
|
// but treat `+0` vs. `-0` as not equal
|
|
|
|
: (a == 0 ? (1 / a == 1 / b) : a == +b);
|
|
|
|
|
|
|
|
case regexpClass:
|
|
|
|
case stringClass:
|
|
|
|
// coerce regexes to strings (http://es5.github.com/#x15.10.6.4)
|
|
|
|
// treat string primitives and their corresponding object instances as equal
|
|
|
|
return a == b + '';
|
|
|
|
}
|
|
|
|
// exit early, in older browsers, if `a` is array-like but not `b`
|
|
|
|
var isArr = arrayLikeClasses[className];
|
|
|
|
if (noArgsClass && !isArr && (isArr = isArguments(a)) && !isArguments(b)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// exit for functions and DOM nodes
|
|
|
|
if (!isArr && (className != objectClass || (noNodeClass && (
|
|
|
|
(typeof a.toString != 'function' && typeof (a + '') == 'string') ||
|
|
|
|
(typeof b.toString != 'function' && typeof (b + '') == 'string'))))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// assume cyclic structures are equal
|
|
|
|
// the algorithm for detecting cyclic structures is adapted from ES 5.1
|
|
|
|
// section 15.12.3, abstract operation `JO` (http://es5.github.com/#x15.12.3)
|
|
|
|
stackA || (stackA = []);
|
|
|
|
stackB || (stackB = []);
|
|
|
|
|
|
|
|
var length = stackA.length;
|
|
|
|
while (length--) {
|
|
|
|
if (stackA[length] == a) {
|
|
|
|
return stackB[length] == b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var index = -1,
|
|
|
|
result = true,
|
|
|
|
size = 0;
|
|
|
|
|
|
|
|
// add `a` and `b` to the stack of traversed objects
|
|
|
|
stackA.push(a);
|
|
|
|
stackB.push(b);
|
|
|
|
|
|
|
|
// recursively compare objects and arrays (susceptible to call stack limits)
|
|
|
|
if (isArr) {
|
|
|
|
// compare lengths to determine if a deep comparison is necessary
|
|
|
|
size = a.length;
|
|
|
|
result = size == b.length;
|
|
|
|
|
|
|
|
if (result) {
|
|
|
|
// deep compare the contents, ignoring non-numeric properties
|
|
|
|
while (size--) {
|
|
|
|
if (!(result = isEqual(a[size], b[size], stackA, stackB))) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
var ctorA = a.constructor,
|
|
|
|
ctorB = b.constructor;
|
|
|
|
|
|
|
|
// non `Object` object instances with different constructors are not equal
|
|
|
|
if (ctorA != ctorB && !(
|
|
|
|
isFunction(ctorA) && ctorA instanceof ctorA &&
|
|
|
|
isFunction(ctorB) && ctorB instanceof ctorB
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// deep compare objects
|
|
|
|
for (var prop in a) {
|
|
|
|
if (hasOwnProperty.call(a, prop)) {
|
|
|
|
// count the number of properties.
|
|
|
|
size++;
|
|
|
|
// deep compare each property value.
|
|
|
|
if (!(hasOwnProperty.call(b, prop) && isEqual(a[prop], b[prop], stackA, stackB))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// ensure both objects have the same number of properties
|
|
|
|
for (prop in b) {
|
|
|
|
// The JS engine in Adobe products, like InDesign, has a bug that causes
|
|
|
|
// `!size--` to throw an error so it must be wrapped in parentheses.
|
|
|
|
// https://github.com/documentcloud/underscore/issues/355
|
|
|
|
if (hasOwnProperty.call(b, prop) && !(size--)) {
|
|
|
|
// `size` will be `-1` if `b` has more properties than `a`
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// handle JScript [[DontEnum]] bug
|
|
|
|
if (hasDontEnumBug) {
|
|
|
|
while (++index < 7) {
|
|
|
|
prop = shadowed[index];
|
|
|
|
if (hasOwnProperty.call(a, prop) &&
|
|
|
|
!(hasOwnProperty.call(b, prop) && isEqual(a[prop], b[prop], stackA, stackB))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if `value` is a finite number.
|
|
|
|
*
|
|
|
|
* Note: This is not the same as native `isFinite`, which will return true for
|
|
|
|
* booleans and other values. See http://es5.github.com/#x15.1.2.5.
|
|
|
|
*
|
|
|
|
* @deprecated
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Objects
|
|
|
|
* @param {Mixed} value The value to check.
|
|
|
|
* @returns {Boolean} Returns `true` if the `value` is a finite number, else `false`.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* _.isFinite(-101);
|
|
|
|
* // => true
|
|
|
|
*
|
|
|
|
* _.isFinite('10');
|
|
|
|
* // => false
|
|
|
|
*
|
|
|
|
* _.isFinite(Infinity);
|
|
|
|
* // => false
|
|
|
|
*/
|
|
|
|
function isFinite(value) {
|
|
|
|
return nativeIsFinite(value) && toString.call(value) == numberClass;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if `value` is the language type of Object.
|
|
|
|
* (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`)
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Objects
|
|
|
|
* @param {Mixed} value The value to check.
|
|
|
|
* @returns {Boolean} Returns `true` if the `value` is an object, else `false`.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* _.isObject({});
|
|
|
|
* // => true
|
|
|
|
*
|
|
|
|
* _.isObject([1, 2, 3]);
|
|
|
|
* // => true
|
|
|
|
*
|
|
|
|
* _.isObject(1);
|
|
|
|
* // => false
|
|
|
|
*/
|
|
|
|
function isObject(value) {
|
|
|
|
// check if the value is the ECMAScript language type of Object
|
|
|
|
// http://es5.github.com/#x8
|
|
|
|
// and avoid a V8 bug
|
|
|
|
// http://code.google.com/p/v8/issues/detail?id=2291
|
|
|
|
return value ? objectTypes[typeof value] : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if `value` is `NaN`.
|
|
|
|
*
|
|
|
|
* Note: This is not the same as native `isNaN`, which will return true for
|
|
|
|
* `undefined` and other values. See http://es5.github.com/#x15.1.2.4.
|
|
|
|
*
|
|
|
|
* @deprecated
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Objects
|
|
|
|
* @param {Mixed} value The value to check.
|
|
|
|
* @returns {Boolean} Returns `true` if the `value` is `NaN`, else `false`.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* _.isNaN(NaN);
|
|
|
|
* // => true
|
|
|
|
*
|
|
|
|
* _.isNaN(new Number(NaN));
|
|
|
|
* // => true
|
|
|
|
*
|
|
|
|
* isNaN(undefined);
|
|
|
|
* // => true
|
|
|
|
*
|
|
|
|
* _.isNaN(undefined);
|
|
|
|
* // => false
|
|
|
|
*/
|
|
|
|
function isNaN(value) {
|
|
|
|
// `NaN` as a primitive is the only value that is not equal to itself
|
|
|
|
// (perform the [[Class]] check first to avoid errors with some host objects in IE)
|
|
|
|
return toString.call(value) == numberClass && value != +value
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if `value` is `null`.
|
|
|
|
*
|
|
|
|
* @deprecated
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Objects
|
|
|
|
* @param {Mixed} value The value to check.
|
|
|
|
* @returns {Boolean} Returns `true` if the `value` is `null`, else `false`.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* _.isNull(null);
|
|
|
|
* // => true
|
|
|
|
*
|
|
|
|
* _.isNull(undefined);
|
|
|
|
* // => false
|
|
|
|
*/
|
|
|
|
function isNull(value) {
|
|
|
|
return value === null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if `value` is a number.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Objects
|
|
|
|
* @param {Mixed} value The value to check.
|
|
|
|
* @returns {Boolean} Returns `true` if the `value` is a number, else `false`.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* _.isNumber(8.4 * 5);
|
|
|
|
* // => true
|
|
|
|
*/
|
|
|
|
function isNumber(value) {
|
|
|
|
return toString.call(value) == numberClass;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if `value` is a regular expression.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Objects
|
|
|
|
* @param {Mixed} value The value to check.
|
|
|
|
* @returns {Boolean} Returns `true` if the `value` is a regular expression, else `false`.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* _.isRegExp(/moe/);
|
|
|
|
* // => true
|
|
|
|
*/
|
|
|
|
function isRegExp(value) {
|
|
|
|
return toString.call(value) == regexpClass;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if `value` is a string.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Objects
|
|
|
|
* @param {Mixed} value The value to check.
|
|
|
|
* @returns {Boolean} Returns `true` if the `value` is a string, else `false`.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* _.isString('moe');
|
|
|
|
* // => true
|
|
|
|
*/
|
|
|
|
function isString(value) {
|
|
|
|
return toString.call(value) == stringClass;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if `value` is `undefined`.
|
|
|
|
*
|
|
|
|
* @deprecated
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Objects
|
|
|
|
* @param {Mixed} value The value to check.
|
|
|
|
* @returns {Boolean} Returns `true` if the `value` is `undefined`, else `false`.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* _.isUndefined(void 0);
|
|
|
|
* // => true
|
|
|
|
*/
|
|
|
|
function isUndefined(value) {
|
|
|
|
return value === undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an array composed of the own enumerable property names of `object`.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Objects
|
|
|
|
* @param {Object} object The object to inspect.
|
|
|
|
* @returns {Array} Returns a new array of property names.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* _.keys({ 'one': 1, 'two': 2, 'three': 3 });
|
|
|
|
* // => ['one', 'two', 'three'] (order is not guaranteed)
|
|
|
|
*/
|
|
|
|
var keys = !nativeKeys ? shimKeys : function(object) {
|
|
|
|
var type = typeof object;
|
|
|
|
|
|
|
|
// avoid iterating over the `prototype` property
|
|
|
|
if (type == 'function' && propertyIsEnumerable.call(object, 'prototype')) {
|
|
|
|
return shimKeys(object);
|
|
|
|
}
|
|
|
|
return object && objectTypes[type]
|
|
|
|
? nativeKeys(object)
|
|
|
|
: [];
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Merges enumerable properties of the source object(s) into the `destination`
|
|
|
|
* object. Subsequent sources will overwrite propery assignments of previous
|
|
|
|
* sources.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Objects
|
|
|
|
* @param {Object} object The destination object.
|
|
|
|
* @param {Object} [source1, source2, ...] The source objects.
|
|
|
|
* @param- {Object} [indicator] Internally used to indicate that the `stack`
|
|
|
|
* argument is an array of traversed objects instead of another source object.
|
|
|
|
* @param- {Array} [stackA=[]] Internally used to track traversed source objects.
|
|
|
|
* @param- {Array} [stackB=[]] Internally used to associate clones with their
|
|
|
|
* source counterparts.
|
|
|
|
* @returns {Object} Returns the destination object.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* var stooges = [
|
|
|
|
* { 'name': 'moe' },
|
|
|
|
* { 'name': 'larry' }
|
|
|
|
* ];
|
|
|
|
*
|
|
|
|
* var ages = [
|
|
|
|
* { 'age': 40 },
|
|
|
|
* { 'age': 50 }
|
|
|
|
* ];
|
|
|
|
*
|
|
|
|
* _.merge(stooges, ages);
|
|
|
|
* // => [{ 'name': 'moe', 'age': 40 }, { 'name': 'larry', 'age': 50 }]
|
|
|
|
*/
|
|
|
|
var merge = createIterator(extendIteratorOptions, {
|
|
|
|
'args': 'object, source, indicator',
|
|
|
|
'top':
|
|
|
|
'var isArr, args = arguments, argsIndex = 0;\n' +
|
|
|
|
'if (indicator == compareAscending) {\n' +
|
|
|
|
' var argsLength = 2, stackA = args[3], stackB = args[4]\n' +
|
|
|
|
'} else {\n' +
|
|
|
|
' var argsLength = args.length, stackA = [], stackB = []\n' +
|
|
|
|
'}\n' +
|
|
|
|
'while (++argsIndex < argsLength) {\n' +
|
|
|
|
' if (iteratee = args[argsIndex]) {',
|
|
|
|
'inLoop':
|
|
|
|
'if ((source = value) && ((isArr = isArray(source)) || isPlainObject(source))) {\n' +
|
|
|
|
' var found = false, stackLength = stackA.length;\n' +
|
|
|
|
' while (stackLength--) {\n' +
|
|
|
|
' if (found = stackA[stackLength] == source) break\n' +
|
|
|
|
' }\n' +
|
|
|
|
' if (found) {\n' +
|
|
|
|
' result[index] = stackB[stackLength]\n' +
|
|
|
|
' } else {\n' +
|
|
|
|
' stackA.push(source);\n' +
|
|
|
|
' stackB.push(value = (value = result[index]) && isArr\n' +
|
|
|
|
' ? (isArray(value) ? value : [])\n' +
|
|
|
|
' : (isPlainObject(value) ? value : {})\n' +
|
|
|
|
' );\n' +
|
|
|
|
' result[index] = callee(value, source, compareAscending, stackA, stackB)\n' +
|
|
|
|
' }\n' +
|
|
|
|
'} else if (source != null) {\n' +
|
|
|
|
' result[index] = source\n' +
|
|
|
|
'}'
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a shallow clone of `object` excluding the specified properties.
|
|
|
|
* Property names may be specified as individual arguments or as arrays of
|
|
|
|
* property names. If `callback` is passed, it will be executed for each property
|
|
|
|
* in the `object`, omitting the properties `callback` returns truthy for. The
|
|
|
|
* `callback` is bound to `thisArg` and invoked with three arguments; (value, key, object).
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Objects
|
|
|
|
* @param {Object} object The source object.
|
|
|
|
* @param {Function|String} callback|[prop1, prop2, ...] The properties to omit
|
|
|
|
* or the function called per iteration.
|
|
|
|
* @param {Mixed} [thisArg] The `this` binding of `callback`.
|
|
|
|
* @returns {Object} Returns an object without the omitted properties.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* _.omit({ 'name': 'moe', 'age': 40, 'userid': 'moe1' }, 'userid');
|
|
|
|
* // => { 'name': 'moe', 'age': 40 }
|
|
|
|
*
|
|
|
|
* _.omit({ 'name': 'moe', '_hint': 'knucklehead', '_seed': '96c4eb' }, function(value, key) {
|
|
|
|
* return key.charAt(0) == '_';
|
|
|
|
* });
|
|
|
|
* // => { 'name': 'moe' }
|
|
|
|
*/
|
|
|
|
var omit = createIterator(omitIteratorOptions);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a two dimensional array of the given object's key-value pairs,
|
|
|
|
* i.e. `[[key1, value1], [key2, value2]]`.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Objects
|
|
|
|
* @param {Object} object The object to inspect.
|
|
|
|
* @returns {Array} Returns new array of key-value pairs.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* _.pairs({ 'moe': 30, 'larry': 40, 'curly': 50 });
|
|
|
|
* // => [['moe', 30], ['larry', 40], ['curly', 50]] (order is not guaranteed)
|
|
|
|
*/
|
|
|
|
var pairs = createIterator({
|
|
|
|
'args': 'object',
|
|
|
|
'init':'[]',
|
|
|
|
'inLoop': 'result' + (isKeysFast ? '[ownIndex] = ' : '.push') + '([index, value])'
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a shallow clone of `object` composed of the specified properties.
|
|
|
|
* Property names may be specified as individual arguments or as arrays of
|
|
|
|
* property names. If `callback` is passed, it will be executed for each property
|
|
|
|
* in the `object`, picking the properties `callback` returns truthy for. The
|
|
|
|
* `callback` is bound to `thisArg` and invoked with three arguments; (value, key, object).
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Objects
|
|
|
|
* @param {Object} object The source object.
|
|
|
|
* @param {Function|String} callback|[prop1, prop2, ...] The properties to pick
|
|
|
|
* or the function called per iteration.
|
|
|
|
* @param {Mixed} [thisArg] The `this` binding of `callback`.
|
|
|
|
* @returns {Object} Returns an object composed of the picked properties.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* _.pick({ 'name': 'moe', 'age': 40, 'userid': 'moe1' }, 'name', 'age');
|
|
|
|
* // => { 'name': 'moe', 'age': 40 }
|
|
|
|
*
|
|
|
|
* _.pick({ 'name': 'moe', '_hint': 'knucklehead', '_seed': '96c4eb' }, function(value, key) {
|
|
|
|
* return key.charAt(0) != '_';
|
|
|
|
* });
|
|
|
|
* // => { 'name': 'moe' }
|
|
|
|
*/
|
|
|
|
var pick = createIterator(omitIteratorOptions, {
|
|
|
|
'top':
|
|
|
|
'if (typeof callback != \'function\') {\n' +
|
|
|
|
' var prop,\n' +
|
|
|
|
' props = concat.apply(ArrayProto, arguments),\n' +
|
|
|
|
' length = props.length;\n' +
|
|
|
|
' for (index = 1; index < length; index++) {\n' +
|
|
|
|
' prop = props[index];\n' +
|
|
|
|
' if (prop in object) result[prop] = object[prop]\n' +
|
|
|
|
' }\n' +
|
|
|
|
'} else {\n' +
|
|
|
|
' callback = createCallback(callback, thisArg)',
|
|
|
|
'inLoop':
|
|
|
|
'if (callback(value, index, object)) result[index] = value',
|
|
|
|
'bottom': '}'
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an array composed of the own enumerable property values of `object`.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Objects
|
|
|
|
* @param {Object} object The object to inspect.
|
|
|
|
* @returns {Array} Returns a new array of property values.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* _.values({ 'one': 1, 'two': 2, 'three': 3 });
|
|
|
|
* // => [1, 2, 3]
|
|
|
|
*/
|
|
|
|
var values = createIterator({
|
|
|
|
'args': 'object',
|
|
|
|
'init': '[]',
|
|
|
|
'inLoop': 'result.push(value)'
|
|
|
|
});
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if a given `target` element is present in a `collection` using strict
|
|
|
|
* equality for comparisons, i.e. `===`.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @alias include
|
|
|
|
* @category Collections
|
|
|
|
* @param {Array|Object|String} collection The collection to iterate over.
|
|
|
|
* @param {Mixed} target The value to check for.
|
|
|
|
* @returns {Boolean} Returns `true` if the `target` element is found, else `false`.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* _.contains([1, 2, 3], 3);
|
|
|
|
* // => true
|
|
|
|
*
|
|
|
|
* _.contains({ 'name': 'moe', 'age': 40 }, 'moe');
|
|
|
|
* // => true
|
|
|
|
*
|
|
|
|
* _.contains('curly', 'ur');
|
|
|
|
* // => true
|
|
|
|
*/
|
|
|
|
var contains = createIterator({
|
|
|
|
'args': 'collection, target',
|
|
|
|
'init': 'false',
|
|
|
|
'noCharByIndex': false,
|
|
|
|
'beforeLoop': {
|
|
|
|
'array': 'if (toString.call(collection) == stringClass) return collection.indexOf(target) > -1'
|
|
|
|
},
|
|
|
|
'inLoop': 'if (value === target) return true'
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an object composed of keys returned from running each element of
|
|
|
|
* `collection` through a `callback`. The corresponding value of each key is
|
|
|
|
* the number of times the key was returned by `callback`. The `callback` is
|
|
|
|
* bound to `thisArg` and invoked with three arguments; (value, index|key, collection).
|
|
|
|
* The `callback` argument may also be the name of a property to count by (e.g. 'length').
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Collections
|
|
|
|
* @param {Array|Object|String} collection The collection to iterate over.
|
|
|
|
* @param {Function|String} callback|property The function called per iteration
|
|
|
|
* or property name to count by.
|
|
|
|
* @param {Mixed} [thisArg] The `this` binding of `callback`.
|
|
|
|
* @returns {Object} Returns the composed aggregate object.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* _.countBy([4.3, 6.1, 6.4], function(num) { return Math.floor(num); });
|
|
|
|
* // => { '4': 1, '6': 2 }
|
|
|
|
*
|
|
|
|
* _.countBy([4.3, 6.1, 6.4], function(num) { return this.floor(num); }, Math);
|
|
|
|
* // => { '4': 1, '6': 2 }
|
|
|
|
*
|
|
|
|
* _.countBy(['one', 'two', 'three'], 'length');
|
|
|
|
* // => { '3': 2, '5': 1 }
|
|
|
|
*/
|
|
|
|
var countBy = createIterator(baseIteratorOptions, countByIteratorOptions);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the `callback` returns a truthy value for **all** elements of a
|
|
|
|
* `collection`. The `callback` is bound to `thisArg` and invoked with three
|
|
|
|
* arguments; (value, index|key, collection).
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @alias all
|
|
|
|
* @category Collections
|
|
|
|
* @param {Array|Object|String} collection The collection to iterate over.
|
|
|
|
* @param {Function} [callback=identity] The function called per iteration.
|
|
|
|
* @param {Mixed} [thisArg] The `this` binding of `callback`.
|
|
|
|
* @returns {Boolean} Returns `true` if all elements pass the callback check,
|
|
|
|
* else `false`.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* _.every([true, 1, null, 'yes'], Boolean);
|
|
|
|
* // => false
|
|
|
|
*/
|
|
|
|
var every = createIterator(baseIteratorOptions, everyIteratorOptions);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Examines each element in a `collection`, returning an array of all elements
|
|
|
|
* the `callback` returns truthy for. The `callback` is bound to `thisArg` and
|
|
|
|
* invoked with three arguments; (value, index|key, collection).
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @alias select
|
|
|
|
* @category Collections
|
|
|
|
* @param {Array|Object|String} collection The collection to iterate over.
|
|
|
|
* @param {Function} [callback=identity] The function called per iteration.
|
|
|
|
* @param {Mixed} [thisArg] The `this` binding of `callback`.
|
|
|
|
* @returns {Array} Returns a new array of elements that passed the callback check.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* var evens = _.filter([1, 2, 3, 4, 5, 6], function(num) { return num % 2 == 0; });
|
|
|
|
* // => [2, 4, 6]
|
|
|
|
*/
|
|
|
|
var filter = createIterator(baseIteratorOptions, filterIteratorOptions);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Examines each element in a `collection`, returning the first one the `callback`
|
|
|
|
* returns truthy for. The function returns as soon as it finds an acceptable
|
|
|
|
* element, and does not iterate over the entire `collection`. The `callback` is
|
|
|
|
* bound to `thisArg` and invoked with three arguments; (value, index|key, collection).
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @alias detect
|
|
|
|
* @category Collections
|
|
|
|
* @param {Array|Object|String} collection The collection to iterate over.
|
|
|
|
* @param {Function} callback The function called per iteration.
|
|
|
|
* @param {Mixed} [thisArg] The `this` binding of `callback`.
|
|
|
|
* @returns {Mixed} Returns the element that passed the callback check,
|
|
|
|
* else `undefined`.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* var even = _.find([1, 2, 3, 4, 5, 6], function(num) { return num % 2 == 0; });
|
|
|
|
* // => 2
|
|
|
|
*/
|
|
|
|
var find = createIterator(baseIteratorOptions, forEachIteratorOptions, {
|
|
|
|
'init': false,
|
|
|
|
'inLoop': 'if (callback(value, index, collection)) return value'
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Iterates over a `collection`, executing the `callback` for each element in
|
|
|
|
* the `collection`. The `callback` is bound to `thisArg` and invoked with three
|
|
|
|
* arguments; (value, index|key, collection). Callbacks may exit iteration early
|
|
|
|
* by explicitly returning `false`.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @alias each
|
|
|
|
* @category Collections
|
|
|
|
* @param {Array|Object|String} collection The collection to iterate over.
|
|
|
|
* @param {Function} callback The function called per iteration.
|
|
|
|
* @param {Mixed} [thisArg] The `this` binding of `callback`.
|
|
|
|
* @returns {Array|Object|String} Returns `collection`.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* _([1, 2, 3]).forEach(alert).join(',');
|
|
|
|
* // => alerts each number and returns '1,2,3'
|
|
|
|
*
|
|
|
|
* _.forEach({ 'one': 1, 'two': 2, 'three': 3 }, alert);
|
|
|
|
* // => alerts each number (order is not guaranteed)
|
|
|
|
*/
|
|
|
|
var forEach = createIterator(baseIteratorOptions, forEachIteratorOptions);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an object composed of keys returned from running each element of
|
|
|
|
* `collection` through a `callback`. The corresponding value of each key is an
|
|
|
|
* array of elements passed to `callback` that returned the key. The `callback`
|
|
|
|
* is bound to `thisArg` and invoked with three arguments; (value, index|key, collection).
|
|
|
|
* The `callback` argument may also be the name of a property to count by (e.g. 'length').
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Collections
|
|
|
|
* @param {Array|Object|String} collection The collection to iterate over.
|
|
|
|
* @param {Function|String} callback|property The function called per iteration
|
|
|
|
* or property name to group by.
|
|
|
|
* @param {Mixed} [thisArg] The `this` binding of `callback`.
|
|
|
|
* @returns {Object} Returns the composed aggregate object.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* _.groupBy([4.2, 6.1, 6.4], function(num) { return Math.floor(num); });
|
|
|
|
* // => { '4': [4.2], '6': [6.1, 6.4] }
|
|
|
|
*
|
|
|
|
* _.groupBy([4.2, 6.1, 6.4], function(num) { return this.floor(num); }, Math);
|
|
|
|
* // => { '4': [4.2], '6': [6.1, 6.4] }
|
|
|
|
*
|
|
|
|
* _.groupBy(['one', 'two', 'three'], 'length');
|
|
|
|
* // => { '3': ['one', 'two'], '5': ['three'] }
|
|
|
|
*/
|
|
|
|
var groupBy = createIterator(baseIteratorOptions, countByIteratorOptions, {
|
|
|
|
'inLoop':
|
|
|
|
'var prop = callback(value, index, collection);\n' +
|
|
|
|
'(hasOwnProperty.call(result, prop) ? result[prop] : result[prop] = []).push(value)'
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invokes the method named by `methodName` on each element in the `collection`,
|
|
|
|
* returning an array of the results of each invoked method. Additional arguments
|
|
|
|
* will be passed to each invoked method. If `methodName` is a function it will
|
|
|
|
* be invoked for, and `this` bound to, each element in the `collection`.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Collections
|
|
|
|
* @param {Array|Object|String} collection The collection to iterate over.
|
|
|
|
* @param {Function|String} methodName The name of the method to invoke or
|
|
|
|
* the function invoked per iteration.
|
|
|
|
* @param {Mixed} [arg1, arg2, ...] Arguments to invoke the method with.
|
|
|
|
* @returns {Array} Returns a new array of the results of each invoked method.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* _.invoke([[5, 1, 7], [3, 2, 1]], 'sort');
|
|
|
|
* // => [[1, 5, 7], [1, 2, 3]]
|
|
|
|
*
|
|
|
|
* _.invoke([123, 456], String.prototype.split, '');
|
|
|
|
* // => [['1', '2', '3'], ['4', '5', '6']]
|
|
|
|
*/
|
|
|
|
var invoke = createIterator(mapIteratorOptions, {
|
|
|
|
'args': 'collection, methodName',
|
|
|
|
'top':
|
|
|
|
'var args = slice.call(arguments, 2),\n' +
|
|
|
|
' isFunc = typeof methodName == \'function\'',
|
|
|
|
'inLoop': {
|
|
|
|
'array':
|
|
|
|
'result[index] = (isFunc ? methodName : value[methodName]).apply(value, args)',
|
|
|
|
'object':
|
|
|
|
'result' + (isKeysFast ? '[ownIndex] = ' : '.push') +
|
|
|
|
'((isFunc ? methodName : value[methodName]).apply(value, args))'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an array of values by running each element in the `collection`
|
|
|
|
* through a `callback`. The `callback` is bound to `thisArg` and invoked with
|
|
|
|
* three arguments; (value, index|key, collection).
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @alias collect
|
|
|
|
* @category Collections
|
|
|
|
* @param {Array|Object|String} collection The collection to iterate over.
|
|
|
|
* @param {Function} [callback=identity] The function called per iteration.
|
|
|
|
* @param {Mixed} [thisArg] The `this` binding of `callback`.
|
|
|
|
* @returns {Array} Returns a new array of the results of each `callback` execution.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* _.map([1, 2, 3], function(num) { return num * 3; });
|
|
|
|
* // => [3, 6, 9]
|
|
|
|
*
|
|
|
|
* _.map({ 'one': 1, 'two': 2, 'three': 3 }, function(num) { return num * 3; });
|
|
|
|
* // => [3, 6, 9] (order is not guaranteed)
|
|
|
|
*/
|
|
|
|
var map = createIterator(baseIteratorOptions, mapIteratorOptions);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves the value of a specified property from all elements in
|
|
|
|
* the `collection`.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Collections
|
|
|
|
* @param {Array|Object|String} collection The collection to iterate over.
|
|
|
|
* @param {String} property The property to pluck.
|
|
|
|
* @returns {Array} Returns a new array of property values.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* var stooges = [
|
|
|
|
* { 'name': 'moe', 'age': 40 },
|
|
|
|
* { 'name': 'larry', 'age': 50 },
|
|
|
|
* { 'name': 'curly', 'age': 60 }
|
|
|
|
* ];
|
|
|
|
*
|
|
|
|
* _.pluck(stooges, 'name');
|
|
|
|
* // => ['moe', 'larry', 'curly']
|
|
|
|
*/
|
|
|
|
var pluck = createIterator(mapIteratorOptions, {
|
|
|
|
'args': 'collection, property',
|
|
|
|
'inLoop': {
|
|
|
|
'array': 'result[index] = value[property]',
|
|
|
|
'object': 'result' + (isKeysFast ? '[ownIndex] = ' : '.push') + '(value[property])'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Boils down a `collection` to a single value. The initial state of the
|
|
|
|
* reduction is `accumulator` and each successive step of it should be returned
|
|
|
|
* by the `callback`. The `callback` is bound to `thisArg` and invoked with 4
|
|
|
|
* arguments; for arrays they are (accumulator, value, index|key, collection).
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @alias foldl, inject
|
|
|
|
* @category Collections
|
|
|
|
* @param {Array|Object|String} collection The collection to iterate over.
|
|
|
|
* @param {Function} callback The function called per iteration.
|
|
|
|
* @param {Mixed} [accumulator] Initial value of the accumulator.
|
|
|
|
* @param {Mixed} [thisArg] The `this` binding of `callback`.
|
|
|
|
* @returns {Mixed} Returns the accumulated value.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* var sum = _.reduce([1, 2, 3], function(memo, num) { return memo + num; });
|
|
|
|
* // => 6
|
|
|
|
*/
|
|
|
|
var reduce = createIterator({
|
|
|
|
'args': 'collection, callback, accumulator, thisArg',
|
|
|
|
'init': 'accumulator',
|
|
|
|
'top':
|
|
|
|
'var noaccum = arguments.length < 3;\n' +
|
|
|
|
'callback = createCallback(callback, thisArg)',
|
|
|
|
'beforeLoop': {
|
|
|
|
'array': 'if (noaccum) result = iteratee[++index]'
|
|
|
|
},
|
|
|
|
'inLoop': {
|
|
|
|
'array':
|
|
|
|
'result = callback(result, value, index, collection)',
|
|
|
|
'object':
|
|
|
|
'result = noaccum\n' +
|
|
|
|
' ? (noaccum = false, value)\n' +
|
|
|
|
' : callback(result, value, index, collection)'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The right-associative version of `_.reduce`.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @alias foldr
|
|
|
|
* @category Collections
|
|
|
|
* @param {Array|Object|String} collection The collection to iterate over.
|
|
|
|
* @param {Function} callback The function called per iteration.
|
|
|
|
* @param {Mixed} [accumulator] Initial value of the accumulator.
|
|
|
|
* @param {Mixed} [thisArg] The `this` binding of `callback`.
|
|
|
|
* @returns {Mixed} Returns the accumulated value.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* var list = [[0, 1], [2, 3], [4, 5]];
|
|
|
|
* var flat = _.reduceRight(list, function(a, b) { return a.concat(b); }, []);
|
|
|
|
* // => [4, 5, 2, 3, 0, 1]
|
|
|
|
*/
|
|
|
|
function reduceRight(collection, callback, accumulator, thisArg) {
|
|
|
|
var iteratee = collection,
|
|
|
|
length = collection ? collection.length : 0,
|
|
|
|
noaccum = arguments.length < 3;
|
|
|
|
|
|
|
|
if (length !== +length) {
|
|
|
|
var props = keys(collection);
|
|
|
|
length = props.length;
|
|
|
|
} else if (noCharByIndex && toString.call(collection) == stringClass) {
|
|
|
|
iteratee = collection.split('');
|
|
|
|
}
|
|
|
|
forEach(collection, function(value, index, object) {
|
|
|
|
index = props ? props[--length] : --length;
|
|
|
|
accumulator = noaccum
|
|
|
|
? (noaccum = false, iteratee[index])
|
|
|
|
: callback.call(thisArg, accumulator, iteratee[index], index, object);
|
|
|
|
});
|
|
|
|
return accumulator;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The opposite of `_.filter`, this method returns the values of a
|
|
|
|
* `collection` that `callback` does **not** return truthy for.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Collections
|
|
|
|
* @param {Array|Object|String} collection The collection to iterate over.
|
|
|
|
* @param {Function} [callback=identity] The function called per iteration.
|
|
|
|
* @param {Mixed} [thisArg] The `this` binding of `callback`.
|
|
|
|
* @returns {Array} Returns a new array of elements that did **not** pass the
|
|
|
|
* callback check.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* var odds = _.reject([1, 2, 3, 4, 5, 6], function(num) { return num % 2 == 0; });
|
|
|
|
* // => [1, 3, 5]
|
|
|
|
*/
|
|
|
|
var reject = createIterator(baseIteratorOptions, filterIteratorOptions, {
|
|
|
|
'inLoop': '!' + filterIteratorOptions.inLoop
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the size of the `collection` by returning `collection.length` for arrays
|
|
|
|
* and array-like objects or the number of own enumerable properties for objects.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Collections
|
|
|
|
* @param {Array|Object|String} collection The collection to inspect.
|
|
|
|
* @returns {Number} Returns `collection.length` or number of own enumerable properties.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* _.size([1, 2]);
|
|
|
|
* // => 2
|
|
|
|
*
|
|
|
|
* _.size({ 'one': 1, 'two': 2, 'three': 3 });
|
|
|
|
* // => 3
|
|
|
|
*
|
|
|
|
* _.size('curly');
|
|
|
|
* // => 5
|
|
|
|
*/
|
|
|
|
function size(collection) {
|
|
|
|
var length = collection ? collection.length : 0;
|
|
|
|
return length === +length ? length : keys(collection).length;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the `callback` returns a truthy value for **any** element of a
|
|
|
|
* `collection`. The function returns as soon as it finds passing value, and
|
|
|
|
* does not iterate over the entire `collection`. The `callback` is bound to
|
|
|
|
* `thisArg` and invoked with three arguments; (value, index|key, collection).
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @alias any
|
|
|
|
* @category Collections
|
|
|
|
* @param {Array|Object|String} collection The collection to iterate over.
|
|
|
|
* @param {Function} [callback=identity] The function called per iteration.
|
|
|
|
* @param {Mixed} [thisArg] The `this` binding of `callback`.
|
|
|
|
* @returns {Boolean} Returns `true` if any element passes the callback check,
|
|
|
|
* else `false`.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* _.some([null, 0, 'yes', false]);
|
|
|
|
* // => true
|
|
|
|
*/
|
|
|
|
var some = createIterator(baseIteratorOptions, everyIteratorOptions, {
|
|
|
|
'init': 'false',
|
|
|
|
'inLoop': everyIteratorOptions.inLoop.replace('!', '')
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an array, stable sorted in ascending order by the results of
|
|
|
|
* running each element of `collection` through a `callback`. The `callback`
|
|
|
|
* is bound to `thisArg` and invoked with three arguments; (value, index|key, collection).
|
|
|
|
* The `callback` argument may also be the name of a property to sort by (e.g. 'length').
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Collections
|
|
|
|
* @param {Array|Object|String} collection The collection to iterate over.
|
|
|
|
* @param {Function|String} callback|property The function called per iteration
|
|
|
|
* or property name to sort by.
|
|
|
|
* @param {Mixed} [thisArg] The `this` binding of `callback`.
|
|
|
|
* @returns {Array} Returns a new array of sorted elements.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* _.sortBy([1, 2, 3], function(num) { return Math.sin(num); });
|
|
|
|
* // => [3, 1, 2]
|
|
|
|
*
|
|
|
|
* _.sortBy([1, 2, 3], function(num) { return this.sin(num); }, Math);
|
|
|
|
* // => [3, 1, 2]
|
|
|
|
*
|
|
|
|
* _.sortBy(['larry', 'brendan', 'moe'], 'length');
|
|
|
|
* // => ['moe', 'larry', 'brendan']
|
|
|
|
*/
|
|
|
|
var sortBy = createIterator(baseIteratorOptions, countByIteratorOptions, mapIteratorOptions, {
|
|
|
|
'inLoop': {
|
|
|
|
'array':
|
|
|
|
'result[index] = {\n' +
|
|
|
|
' criteria: callback(value, index, collection),\n' +
|
|
|
|
' index: index,\n' +
|
|
|
|
' value: value\n' +
|
|
|
|
'}',
|
|
|
|
'object':
|
|
|
|
'result' + (isKeysFast ? '[ownIndex] = ' : '.push') + '({\n' +
|
|
|
|
' criteria: callback(value, index, collection),\n' +
|
|
|
|
' index: index,\n' +
|
|
|
|
' value: value\n' +
|
|
|
|
'})'
|
|
|
|
},
|
|
|
|
'bottom':
|
|
|
|
'result.sort(compareAscending);\n' +
|
|
|
|
'length = result.length;\n' +
|
|
|
|
'while (length--) {\n' +
|
|
|
|
' result[length] = result[length].value\n' +
|
|
|
|
'}'
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts the `collection`, to an array.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Collections
|
|
|
|
* @param {Array|Object|String} collection The collection to convert.
|
|
|
|
* @returns {Array} Returns the new converted array.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* (function() { return _.toArray(arguments).slice(1); })(1, 2, 3, 4);
|
|
|
|
* // => [2, 3, 4]
|
|
|
|
*/
|
|
|
|
function toArray(collection) {
|
|
|
|
if (!collection) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
var length = collection.length;
|
|
|
|
if (length === +length) {
|
|
|
|
return (noArraySliceOnStrings ? toString.call(collection) == stringClass : typeof collection == 'string')
|
|
|
|
? collection.split('')
|
|
|
|
: slice.call(collection);
|
|
|
|
}
|
|
|
|
return values(collection);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Examines each element in a `collection`, returning an array of all elements
|
|
|
|
* that contain the given `properties`.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Collections
|
|
|
|
* @param {Array|Object|String} collection The collection to iterate over.
|
|
|
|
* @param {Object} properties The object of properties/values to filter by.
|
|
|
|
* @returns {Array} Returns a new array of elements that contain the given `properties`.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* var stooges = [
|
|
|
|
* { 'name': 'moe', 'age': 40 },
|
|
|
|
* { 'name': 'larry', 'age': 50 },
|
|
|
|
* { 'name': 'curly', 'age': 60 }
|
|
|
|
* ];
|
|
|
|
*
|
|
|
|
* _.where(stooges, { 'age': 40 });
|
|
|
|
* // => [{ 'name': 'moe', 'age': 40 }]
|
|
|
|
*/
|
|
|
|
var where = createIterator(filterIteratorOptions, {
|
|
|
|
'args': 'collection, properties',
|
|
|
|
'top':
|
|
|
|
'var props = [];\n' +
|
|
|
|
'forIn(properties, function(value, prop) { props.push(prop) });\n' +
|
|
|
|
'var propsLength = props.length',
|
|
|
|
'inLoop':
|
|
|
|
'for (var prop, pass = true, propIndex = 0; propIndex < propsLength; propIndex++) {\n' +
|
|
|
|
' prop = props[propIndex];\n' +
|
|
|
|
' if (!(pass = value[prop] === properties[prop])) break\n' +
|
|
|
|
'}\n' +
|
|
|
|
'pass && result.push(value)'
|
|
|
|
});
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an array with all falsey values of `array` removed. The values
|
|
|
|
* `false`, `null`, `0`, `""`, `undefined` and `NaN` are all falsey.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Arrays
|
|
|
|
* @param {Array} array The array to compact.
|
|
|
|
* @returns {Array} Returns a new filtered array.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* _.compact([0, 1, false, 2, '', 3]);
|
|
|
|
* // => [1, 2, 3]
|
|
|
|
*/
|
|
|
|
function compact(array) {
|
|
|
|
var index = -1,
|
|
|
|
length = array ? array.length : 0,
|
|
|
|
result = [];
|
|
|
|
|
|
|
|
while (++index < length) {
|
|
|
|
if (array[index]) {
|
|
|
|
result.push(array[index]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an array of `array` elements not present in the other arrays
|
|
|
|
* using strict equality for comparisons, i.e. `===`.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Arrays
|
|
|
|
* @param {Array} array The array to process.
|
|
|
|
* @param {Array} [array1, array2, ...] Arrays to check.
|
|
|
|
* @returns {Array} Returns a new array of `array` elements not present in the
|
|
|
|
* other arrays.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* _.difference([1, 2, 3, 4, 5], [5, 2, 10]);
|
|
|
|
* // => [1, 3, 4]
|
|
|
|
*/
|
|
|
|
function difference(array) {
|
|
|
|
var result = [];
|
|
|
|
if (!array) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
var index = -1,
|
|
|
|
length = array.length,
|
|
|
|
flattened = concat.apply(ArrayProto, arguments),
|
|
|
|
contains = cachedContains(flattened, length);
|
|
|
|
|
|
|
|
while (++index < length) {
|
|
|
|
var value = array[index];
|
|
|
|
if (!contains(value)) {
|
|
|
|
result.push(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the first element of the `array`. Pass `n` to return the first `n`
|
|
|
|
* elements of the `array`.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @alias head, take
|
|
|
|
* @category Arrays
|
|
|
|
* @param {Array} array The array to query.
|
|
|
|
* @param {Number} [n] The number of elements to return.
|
|
|
|
* @param- {Object} [guard] Internally used to allow this method to work with
|
|
|
|
* others like `_.map` without using their callback `index` argument for `n`.
|
|
|
|
* @returns {Mixed} Returns the first element or an array of the first `n`
|
|
|
|
* elements of `array`.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* _.first([5, 4, 3, 2, 1]);
|
|
|
|
* // => 5
|
|
|
|
*/
|
|
|
|
function first(array, n, guard) {
|
|
|
|
if (array) {
|
|
|
|
return (n == null || guard) ? array[0] : slice.call(array, 0, n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Flattens a nested array (the nesting can be to any depth). If `shallow` is
|
|
|
|
* truthy, `array` will only be flattened a single level.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Arrays
|
|
|
|
* @param {Array} array The array to compact.
|
|
|
|
* @param {Boolean} shallow A flag to indicate only flattening a single level.
|
|
|
|
* @returns {Array} Returns a new flattened array.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* _.flatten([1, [2], [3, [[4]]]]);
|
|
|
|
* // => [1, 2, 3, 4];
|
|
|
|
*
|
|
|
|
* _.flatten([1, [2], [3, [[4]]]], true);
|
|
|
|
* // => [1, 2, 3, [[4]]];
|
|
|
|
*/
|
|
|
|
function flatten(array, shallow) {
|
|
|
|
var value,
|
|
|
|
index = -1,
|
|
|
|
length = array ? array.length : 0,
|
|
|
|
result = [];
|
|
|
|
|
|
|
|
while (++index < length) {
|
|
|
|
value = array[index];
|
|
|
|
|
|
|
|
// recursively flatten arrays (susceptible to call stack limits)
|
|
|
|
if (isArray(value)) {
|
|
|
|
push.apply(result, shallow ? value : flatten(value));
|
|
|
|
} else {
|
|
|
|
result.push(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the index at which the first occurrence of `value` is found using
|
|
|
|
* strict equality for comparisons, i.e. `===`. If the `array` is already
|
|
|
|
* sorted, passing `true` for `isSorted` will run a faster binary search.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Arrays
|
|
|
|
* @param {Array} array The array to search.
|
|
|
|
* @param {Mixed} value The value to search for.
|
|
|
|
* @param {Boolean|Number} [fromIndex=0] The index to start searching from or
|
|
|
|
* `true` to perform a binary search on a sorted `array`.
|
|
|
|
* @returns {Number} Returns the index of the matched value or `-1`.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* _.indexOf([1, 2, 3, 1, 2, 3], 2);
|
|
|
|
* // => 1
|
|
|
|
*
|
|
|
|
* _.indexOf([1, 2, 3, 1, 2, 3], 2, 3);
|
|
|
|
* // => 4
|
|
|
|
*
|
|
|
|
* _.indexOf([1, 1, 2, 2, 3, 3], 2, true);
|
|
|
|
* // => 2
|
|
|
|
*/
|
|
|
|
function indexOf(array, value, fromIndex) {
|
|
|
|
var index = -1,
|
|
|
|
length = array ? array.length : 0;
|
|
|
|
|
|
|
|
if (typeof fromIndex == 'number') {
|
|
|
|
index = (fromIndex < 0 ? nativeMax(0, length + fromIndex) : fromIndex || 0) - 1;
|
|
|
|
} else if (fromIndex) {
|
|
|
|
index = sortedIndex(array, value);
|
|
|
|
return array[index] === value ? index : -1;
|
|
|
|
}
|
|
|
|
while (++index < length) {
|
|
|
|
if (array[index] === value) {
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets all but the last element of `array`. Pass `n` to exclude the last `n`
|
|
|
|
* elements from the result.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Arrays
|
|
|
|
* @param {Array} array The array to query.
|
|
|
|
* @param {Number} [n] The number of elements to return.
|
|
|
|
* @param- {Object} [guard] Internally used to allow this method to work with
|
|
|
|
* others like `_.map` without using their callback `index` argument for `n`.
|
|
|
|
* @returns {Array} Returns all but the last element or `n` elements of `array`.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* _.initial([3, 2, 1]);
|
|
|
|
* // => [3, 2]
|
|
|
|
*/
|
|
|
|
function initial(array, n, guard) {
|
|
|
|
return array
|
|
|
|
? slice.call(array, 0, -((n == null || guard) ? 1 : n))
|
|
|
|
: [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Computes the intersection of all the passed-in arrays using strict equality
|
|
|
|
* for comparisons, i.e. `===`.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Arrays
|
|
|
|
* @param {Array} [array1, array2, ...] Arrays to process.
|
|
|
|
* @returns {Array} Returns a new array of unique elements, in order, that are
|
|
|
|
* present in **all** of the arrays.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* _.intersection([1, 2, 3], [101, 2, 1, 10], [2, 1]);
|
|
|
|
* // => [1, 2]
|
|
|
|
*/
|
|
|
|
function intersection(array) {
|
|
|
|
var argsLength = arguments.length,
|
|
|
|
cache = [],
|
|
|
|
index = -1,
|
|
|
|
length = array ? array.length : 0,
|
|
|
|
result = [];
|
|
|
|
|
|
|
|
array: while (++index < length) {
|
|
|
|
var value = array[index];
|
|
|
|
if (indexOf(result, value) < 0) {
|
|
|
|
for (var argsIndex = 1; argsIndex < argsLength; argsIndex++) {
|
|
|
|
if (!(cache[argsIndex] || (cache[argsIndex] = cachedContains(arguments[argsIndex])))(value)) {
|
|
|
|
continue array;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result.push(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the last element of the `array`. Pass `n` to return the last `n`
|
|
|
|
* elements of the `array`.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Arrays
|
|
|
|
* @param {Array} array The array to query.
|
|
|
|
* @param {Number} [n] The number of elements to return.
|
|
|
|
* @param- {Object} [guard] Internally used to allow this method to work with
|
|
|
|
* others like `_.map` without using their callback `index` argument for `n`.
|
|
|
|
* @returns {Mixed} Returns the last element or an array of the last `n`
|
|
|
|
* elements of `array`.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* _.last([3, 2, 1]);
|
|
|
|
* // => 1
|
|
|
|
*/
|
|
|
|
function last(array, n, guard) {
|
|
|
|
if (array) {
|
|
|
|
var length = array.length;
|
|
|
|
return (n == null || guard) ? array[length - 1] : slice.call(array, -n || length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the index at which the last occurrence of `value` is found using
|
|
|
|
* strict equality for comparisons, i.e. `===`.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Arrays
|
|
|
|
* @param {Array} array The array to search.
|
|
|
|
* @param {Mixed} value The value to search for.
|
|
|
|
* @param {Number} [fromIndex=array.length-1] The index to start searching from.
|
|
|
|
* @returns {Number} Returns the index of the matched value or `-1`.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* _.lastIndexOf([1, 2, 3, 1, 2, 3], 2);
|
|
|
|
* // => 4
|
|
|
|
*
|
|
|
|
* _.lastIndexOf([1, 2, 3, 1, 2, 3], 2, 3);
|
|
|
|
* // => 1
|
|
|
|
*/
|
|
|
|
function lastIndexOf(array, value, fromIndex) {
|
|
|
|
var index = array ? array.length : 0;
|
|
|
|
if (typeof fromIndex == 'number') {
|
|
|
|
index = (fromIndex < 0 ? nativeMax(0, index + fromIndex) : nativeMin(fromIndex, index - 1)) + 1;
|
|
|
|
}
|
|
|
|
while (index--) {
|
|
|
|
if (array[index] === value) {
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves the maximum value of an `array`. If `callback` is passed,
|
|
|
|
* it will be executed for each value in the `array` to generate the
|
|
|
|
* criterion by which the value is ranked. The `callback` is bound to
|
|
|
|
* `thisArg` and invoked with three arguments; (value, index, array).
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Arrays
|
|
|
|
* @param {Array} array The array to iterate over.
|
|
|
|
* @param {Function} [callback] The function called per iteration.
|
|
|
|
* @param {Mixed} [thisArg] The `this` binding of `callback`.
|
|
|
|
* @returns {Mixed} Returns the maximum value.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* var stooges = [
|
|
|
|
* { 'name': 'moe', 'age': 40 },
|
|
|
|
* { 'name': 'larry', 'age': 50 },
|
|
|
|
* { 'name': 'curly', 'age': 60 }
|
|
|
|
* ];
|
|
|
|
*
|
|
|
|
* _.max(stooges, function(stooge) { return stooge.age; });
|
|
|
|
* // => { 'name': 'curly', 'age': 60 };
|
|
|
|
*/
|
|
|
|
function max(array, callback, thisArg) {
|
|
|
|
var current,
|
|
|
|
computed = -Infinity,
|
|
|
|
index = -1,
|
|
|
|
length = array ? array.length : 0,
|
|
|
|
result = computed;
|
|
|
|
|
|
|
|
callback = createCallback(callback, thisArg);
|
|
|
|
while (++index < length) {
|
|
|
|
current = callback(array[index], index, array);
|
|
|
|
if (current > computed) {
|
|
|
|
computed = current;
|
|
|
|
result = array[index];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves the minimum value of an `array`. If `callback` is passed,
|
|
|
|
* it will be executed for each value in the `array` to generate the
|
|
|
|
* criterion by which the value is ranked. The `callback` is bound to `thisArg`
|
|
|
|
* and invoked with three arguments; (value, index, array).
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Arrays
|
|
|
|
* @param {Array} array The array to iterate over.
|
|
|
|
* @param {Function} [callback] The function called per iteration.
|
|
|
|
* @param {Mixed} [thisArg] The `this` binding of `callback`.
|
|
|
|
* @returns {Mixed} Returns the minimum value.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* _.min([10, 5, 100, 2, 1000]);
|
|
|
|
* // => 2
|
|
|
|
*/
|
|
|
|
function min(array, callback, thisArg) {
|
|
|
|
var current,
|
|
|
|
computed = Infinity,
|
|
|
|
index = -1,
|
|
|
|
length = array ? array.length : 0,
|
|
|
|
result = computed;
|
|
|
|
|
|
|
|
callback = createCallback(callback, thisArg);
|
|
|
|
while (++index < length) {
|
|
|
|
current = callback(array[index], index, array);
|
|
|
|
if (current < computed) {
|
|
|
|
computed = current;
|
|
|
|
result = array[index];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an object composed from arrays of `keys` and `values`. Pass either
|
|
|
|
* a single two dimensional array, i.e. `[[key1, value1], [key2, value2]]`, or
|
|
|
|
* two arrays, one of `keys` and one of corresponding `values`.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Arrays
|
|
|
|
* @param {Array} keys The array of keys.
|
|
|
|
* @param {Array} [values=[]] The array of values.
|
|
|
|
* @returns {Object} Returns an object composed of the given keys and
|
|
|
|
* corresponding values.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* _.object(['moe', 'larry', 'curly'], [30, 40, 50]);
|
|
|
|
* // => { 'moe': 30, 'larry': 40, 'curly': 50 }
|
|
|
|
*/
|
|
|
|
function object(keys, values) {
|
|
|
|
var index = -1,
|
|
|
|
length = keys ? keys.length : 0,
|
|
|
|
result = {};
|
|
|
|
|
|
|
|
while (++index < length) {
|
|
|
|
if (values) {
|
|
|
|
result[keys[index]] = values[index];
|
|
|
|
} else {
|
|
|
|
result[keys[index][0]] = keys[index][1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an array of numbers (positive and/or negative) progressing from
|
|
|
|
* `start` up to but not including `stop`. This method is a port of Python's
|
|
|
|
* `range()` function. See http://docs.python.org/library/functions.html#range.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Arrays
|
|
|
|
* @param {Number} [start=0] The start of the range.
|
|
|
|
* @param {Number} end The end of the range.
|
|
|
|
* @param {Number} [step=1] The value to increment or descrement by.
|
|
|
|
* @returns {Array} Returns a new range array.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* _.range(10);
|
|
|
|
* // => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
|
|
|
*
|
|
|
|
* _.range(1, 11);
|
|
|
|
* // => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
|
|
|
*
|
|
|
|
* _.range(0, 30, 5);
|
|
|
|
* // => [0, 5, 10, 15, 20, 25]
|
|
|
|
*
|
|
|
|
* _.range(0, -10, -1);
|
|
|
|
* // => [0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
|
|
|
|
*
|
|
|
|
* _.range(0);
|
|
|
|
* // => []
|
|
|
|
*/
|
|
|
|
function range(start, end, step) {
|
|
|
|
start = +start || 0;
|
|
|
|
step = +step || 1;
|
|
|
|
|
|
|
|
if (end == null) {
|
|
|
|
end = start;
|
|
|
|
start = 0;
|
|
|
|
}
|
|
|
|
// use `Array(length)` so V8 will avoid the slower "dictionary" mode
|
|
|
|
// http://www.youtube.com/watch?v=XAqIpGU8ZZk#t=16m27s
|
|
|
|
var index = -1,
|
|
|
|
length = nativeMax(0, Math.ceil((end - start) / step)),
|
|
|
|
result = Array(length);
|
|
|
|
|
|
|
|
while (++index < length) {
|
|
|
|
result[index] = start;
|
|
|
|
start += step;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The opposite of `_.initial`, this method gets all but the first value of
|
|
|
|
* `array`. Pass `n` to exclude the first `n` values from the result.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @alias drop, tail
|
|
|
|
* @category Arrays
|
|
|
|
* @param {Array} array The array to query.
|
|
|
|
* @param {Number} [n] The number of elements to return.
|
|
|
|
* @param- {Object} [guard] Internally used to allow this method to work with
|
|
|
|
* others like `_.map` without using their callback `index` argument for `n`.
|
|
|
|
* @returns {Array} Returns all but the first value or `n` values of `array`.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* _.rest([3, 2, 1]);
|
|
|
|
* // => [2, 1]
|
|
|
|
*/
|
|
|
|
function rest(array, n, guard) {
|
|
|
|
return array
|
|
|
|
? slice.call(array, (n == null || guard) ? 1 : n)
|
|
|
|
: [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an array of shuffled `array` values, using a version of the
|
|
|
|
* Fisher-Yates shuffle. See http://en.wikipedia.org/wiki/Fisher-Yates_shuffle.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Arrays
|
|
|
|
* @param {Array} array The array to shuffle.
|
|
|
|
* @returns {Array} Returns a new shuffled array.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* _.shuffle([1, 2, 3, 4, 5, 6]);
|
|
|
|
* // => [4, 1, 6, 3, 5, 2]
|
|
|
|
*/
|
|
|
|
function shuffle(array) {
|
|
|
|
var index = -1,
|
|
|
|
length = array ? array.length : 0,
|
|
|
|
result = Array(length);
|
|
|
|
|
|
|
|
while (++index < length) {
|
|
|
|
var rand = nativeFloor(nativeRandom() * (index + 1));
|
|
|
|
result[index] = result[rand];
|
|
|
|
result[rand] = array[index];
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Uses a binary search to determine the smallest index at which the `value`
|
|
|
|
* should be inserted into `array` in order to maintain the sort order of the
|
|
|
|
* sorted `array`. If `callback` is passed, it will be executed for `value` and
|
|
|
|
* each element in `array` to compute their sort ranking. The `callback` is
|
|
|
|
* bound to `thisArg` and invoked with one argument; (value). The `callback`
|
|
|
|
* argument may also be the name of a property to order by.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Arrays
|
|
|
|
* @param {Array} array The array to iterate over.
|
|
|
|
* @param {Mixed} value The value to evaluate.
|
|
|
|
* @param {Function|String} [callback=identity|property] The function called
|
|
|
|
* per iteration or property name to order by.
|
|
|
|
* @param {Mixed} [thisArg] The `this` binding of `callback`.
|
|
|
|
* @returns {Number} Returns the index at which the value should be inserted
|
|
|
|
* into `array`.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* _.sortedIndex([20, 30, 50], 40);
|
|
|
|
* // => 2
|
|
|
|
*
|
|
|
|
* _.sortedIndex([{ 'x': 20 }, { 'x': 30 }, { 'x': 50 }], { 'x': 40 }, 'x');
|
|
|
|
* // => 2
|
|
|
|
*
|
|
|
|
* var dict = {
|
|
|
|
* 'wordToNumber': { 'twenty': 20, 'thirty': 30, 'fourty': 40, 'fifty': 50 }
|
|
|
|
* };
|
|
|
|
*
|
|
|
|
* _.sortedIndex(['twenty', 'thirty', 'fifty'], 'fourty', function(word) {
|
|
|
|
* return dict.wordToNumber[word];
|
|
|
|
* });
|
|
|
|
* // => 2
|
|
|
|
*
|
|
|
|
* _.sortedIndex(['twenty', 'thirty', 'fifty'], 'fourty', function(word) {
|
|
|
|
* return this.wordToNumber[word];
|
|
|
|
* }, dict);
|
|
|
|
* // => 2
|
|
|
|
*/
|
|
|
|
function sortedIndex(array, value, callback, thisArg) {
|
|
|
|
var low = 0,
|
|
|
|
high = array ? array.length : low;
|
|
|
|
|
|
|
|
callback = createCallback(callback, thisArg);
|
|
|
|
value = callback(value);
|
|
|
|
while (low < high) {
|
|
|
|
var mid = (low + high) >>> 1;
|
|
|
|
callback(array[mid]) < value ? low = mid + 1 : high = mid;
|
|
|
|
}
|
|
|
|
return low;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Computes the union of the passed-in arrays using strict equality for
|
|
|
|
* comparisons, i.e. `===`.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Arrays
|
|
|
|
* @param {Array} [array1, array2, ...] Arrays to process.
|
|
|
|
* @returns {Array} Returns a new array of unique values, in order, that are
|
|
|
|
* present in one or more of the arrays.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* _.union([1, 2, 3], [101, 2, 1, 10], [2, 1]);
|
|
|
|
* // => [1, 2, 3, 101, 10]
|
|
|
|
*/
|
|
|
|
function union() {
|
|
|
|
var index = -1,
|
|
|
|
flattened = concat.apply(ArrayProto, arguments),
|
|
|
|
length = flattened.length,
|
|
|
|
result = [];
|
|
|
|
|
|
|
|
while (++index < length) {
|
|
|
|
if (indexOf(result, flattened[index]) < 0) {
|
|
|
|
result.push(flattened[index]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a duplicate-value-free version of the `array` using strict equality
|
|
|
|
* for comparisons, i.e. `===`. If the `array` is already sorted, passing `true`
|
|
|
|
* for `isSorted` will run a faster algorithm. If `callback` is passed, each
|
|
|
|
* element of `array` is passed through a callback` before uniqueness is computed.
|
|
|
|
* The `callback` is bound to `thisArg` and invoked with three arguments; (value, index, array).
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @alias unique
|
|
|
|
* @category Arrays
|
|
|
|
* @param {Array} array The array to process.
|
|
|
|
* @param {Boolean} [isSorted=false] A flag to indicate that the `array` is already sorted.
|
|
|
|
* @param {Function} [callback=identity] The function called per iteration.
|
|
|
|
* @param {Mixed} [thisArg] The `this` binding of `callback`.
|
|
|
|
* @returns {Array} Returns a duplicate-value-free array.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* _.uniq([1, 2, 1, 3, 1]);
|
|
|
|
* // => [1, 2, 3]
|
|
|
|
*
|
|
|
|
* _.uniq([1, 1, 2, 2, 3], true);
|
|
|
|
* // => [1, 2, 3]
|
|
|
|
*
|
|
|
|
* _.uniq([1, 2, 1.5, 3, 2.5], function(num) { return Math.floor(num); });
|
|
|
|
* // => [1, 2, 3]
|
|
|
|
*
|
|
|
|
* _.uniq([1, 2, 1.5, 3, 2.5], function(num) { return this.floor(num); }, Math);
|
|
|
|
* // => [1, 2, 3]
|
|
|
|
*/
|
|
|
|
function uniq(array, isSorted, callback, thisArg) {
|
|
|
|
var index = -1,
|
|
|
|
length = array ? array.length : 0,
|
|
|
|
result = [],
|
|
|
|
seen = [];
|
|
|
|
|
|
|
|
// juggle arguments
|
|
|
|
if (typeof isSorted == 'function') {
|
|
|
|
thisArg = callback;
|
|
|
|
callback = isSorted;
|
|
|
|
isSorted = false;
|
|
|
|
}
|
|
|
|
callback = createCallback(callback, thisArg);
|
|
|
|
while (++index < length) {
|
|
|
|
var computed = callback(array[index], index, array);
|
|
|
|
if (isSorted
|
|
|
|
? !index || seen[seen.length - 1] !== computed
|
|
|
|
: indexOf(seen, computed) < 0
|
|
|
|
) {
|
|
|
|
seen.push(computed);
|
|
|
|
result.push(array[index]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an array with all occurrences of the passed values removed using
|
|
|
|
* strict equality for comparisons, i.e. `===`.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Arrays
|
|
|
|
* @param {Array} array The array to filter.
|
|
|
|
* @param {Mixed} [value1, value2, ...] Values to remove.
|
|
|
|
* @returns {Array} Returns a new filtered array.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* _.without([1, 2, 1, 0, 3, 1, 4], 0, 1);
|
|
|
|
* // => [2, 3, 4]
|
|
|
|
*/
|
|
|
|
function without(array) {
|
|
|
|
var index = -1,
|
|
|
|
length = array ? array.length : 0,
|
|
|
|
contains = cachedContains(arguments, 1, 20),
|
|
|
|
result = [];
|
|
|
|
|
|
|
|
while (++index < length) {
|
|
|
|
var value = array[index];
|
|
|
|
if (!contains(value)) {
|
|
|
|
result.push(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Groups the elements of each array at their corresponding indexes. Useful for
|
|
|
|
* separate data sources that are coordinated through matching array indexes.
|
|
|
|
* For a matrix of nested arrays, `_.zip.apply(...)` can transpose the matrix
|
|
|
|
* in a similar fashion.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Arrays
|
|
|
|
* @param {Array} [array1, array2, ...] Arrays to process.
|
|
|
|
* @returns {Array} Returns a new array of grouped elements.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* _.zip(['moe', 'larry', 'curly'], [30, 40, 50], [true, false, false]);
|
|
|
|
* // => [['moe', 30, true], ['larry', 40, false], ['curly', 50, false]]
|
|
|
|
*/
|
|
|
|
function zip(array) {
|
|
|
|
var index = -1,
|
|
|
|
length = array ? max(pluck(arguments, 'length')) : 0,
|
|
|
|
result = Array(length);
|
|
|
|
|
|
|
|
while (++index < length) {
|
|
|
|
result[index] = pluck(arguments, index);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a function that is restricted to executing only after it is
|
|
|
|
* called `n` times.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Functions
|
|
|
|
* @param {Number} n The number of times the function must be called before
|
|
|
|
* it is executed.
|
|
|
|
* @param {Function} func The function to restrict.
|
|
|
|
* @returns {Function} Returns the new restricted function.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* var renderNotes = _.after(notes.length, render);
|
|
|
|
* _.forEach(notes, function(note) {
|
|
|
|
* note.asyncSave({ 'success': renderNotes });
|
|
|
|
* });
|
|
|
|
* // `renderNotes` is run once, after all notes have saved
|
|
|
|
*/
|
|
|
|
function after(n, func) {
|
|
|
|
if (n < 1) {
|
|
|
|
return func();
|
|
|
|
}
|
|
|
|
return function() {
|
|
|
|
if (--n < 1) {
|
|
|
|
return func.apply(this, arguments);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a function that, when called, invokes `func` with the `this`
|
|
|
|
* binding of `thisArg` and prepends any additional `bind` arguments to those
|
|
|
|
* passed to the bound function.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Functions
|
|
|
|
* @param {Function} func The function to bind.
|
|
|
|
* @param {Mixed} [thisArg] The `this` binding of `func`.
|
|
|
|
* @param {Mixed} [arg1, arg2, ...] Arguments to be partially applied.
|
|
|
|
* @returns {Function} Returns the new bound function.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* var func = function(greeting) {
|
|
|
|
* return greeting + ' ' + this.name;
|
|
|
|
* };
|
|
|
|
*
|
|
|
|
* func = _.bind(func, { 'name': 'moe' }, 'hi');
|
|
|
|
* func();
|
|
|
|
* // => 'hi moe'
|
|
|
|
*/
|
|
|
|
function bind(func, thisArg) {
|
|
|
|
// use `Function#bind` if it exists and is fast
|
|
|
|
// (in V8 `Function#bind` is slower except when partially applied)
|
|
|
|
return isBindFast || (nativeBind && arguments.length > 2)
|
|
|
|
? nativeBind.call.apply(nativeBind, arguments)
|
|
|
|
: createBound(func, thisArg, slice.call(arguments, 2));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Binds methods on `object` to `object`, overwriting the existing method.
|
|
|
|
* If no method names are provided, all the function properties of `object`
|
|
|
|
* will be bound.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Functions
|
|
|
|
* @param {Object} object The object to bind and assign the bound methods to.
|
|
|
|
* @param {String} [methodName1, methodName2, ...] Method names on the object to bind.
|
|
|
|
* @returns {Object} Returns `object`.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* var buttonView = {
|
|
|
|
* 'label': 'lodash',
|
|
|
|
* 'onClick': function() { alert('clicked: ' + this.label); }
|
|
|
|
* };
|
|
|
|
*
|
|
|
|
* _.bindAll(buttonView);
|
|
|
|
* jQuery('#lodash_button').on('click', buttonView.onClick);
|
|
|
|
* // => When the button is clicked, `this.label` will have the correct value
|
|
|
|
*/
|
|
|
|
var bindAll = createIterator({
|
|
|
|
'useHas': false,
|
|
|
|
'useStrict': false,
|
|
|
|
'args': 'object',
|
|
|
|
'top':
|
|
|
|
'var funcs = arguments,\n' +
|
|
|
|
' length = funcs.length;\n' +
|
|
|
|
'if (length > 1) {\n' +
|
|
|
|
' for (var index = 1; index < length; index++) {\n' +
|
|
|
|
' result[funcs[index]] = bind(result[funcs[index]], result)\n' +
|
|
|
|
' }\n' +
|
|
|
|
' return result\n' +
|
|
|
|
'}',
|
|
|
|
'inLoop':
|
|
|
|
'if (isFunction(value)) {\n' +
|
|
|
|
' result[index] = bind(value, result)\n' +
|
|
|
|
'}'
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a function that is the composition of the passed functions,
|
|
|
|
* where each function consumes the return value of the function that follows.
|
|
|
|
* In math terms, composing the functions `f()`, `g()`, and `h()` produces `f(g(h()))`.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Functions
|
|
|
|
* @param {Function} [func1, func2, ...] Functions to compose.
|
|
|
|
* @returns {Function} Returns the new composed function.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* var greet = function(name) { return 'hi: ' + name; };
|
|
|
|
* var exclaim = function(statement) { return statement + '!'; };
|
|
|
|
* var welcome = _.compose(exclaim, greet);
|
|
|
|
* welcome('moe');
|
|
|
|
* // => 'hi: moe!'
|
|
|
|
*/
|
|
|
|
function compose() {
|
|
|
|
var funcs = arguments;
|
|
|
|
return function() {
|
|
|
|
var args = arguments,
|
|
|
|
length = funcs.length;
|
|
|
|
|
|
|
|
while (length--) {
|
|
|
|
args = [funcs[length].apply(this, args)];
|
|
|
|
}
|
|
|
|
return args[0];
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a function that will delay the execution of `func` until after
|
|
|
|
* `wait` milliseconds have elapsed since the last time it was invoked. Pass
|
|
|
|
* `true` for `immediate` to cause debounce to invoke `func` on the leading,
|
|
|
|
* instead of the trailing, edge of the `wait` timeout. Subsequent calls to
|
|
|
|
* the debounced function will return the result of the last `func` call.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Functions
|
|
|
|
* @param {Function} func The function to debounce.
|
|
|
|
* @param {Number} wait The number of milliseconds to delay.
|
|
|
|
* @param {Boolean} immediate A flag to indicate execution is on the leading
|
|
|
|
* edge of the timeout.
|
|
|
|
* @returns {Function} Returns the new debounced function.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* var lazyLayout = _.debounce(calculateLayout, 300);
|
|
|
|
* jQuery(window).on('resize', lazyLayout);
|
|
|
|
*/
|
|
|
|
function debounce(func, wait, immediate) {
|
|
|
|
var args,
|
|
|
|
result,
|
|
|
|
thisArg,
|
|
|
|
timeoutId;
|
|
|
|
|
|
|
|
function delayed() {
|
|
|
|
timeoutId = null;
|
|
|
|
if (!immediate) {
|
|
|
|
result = func.apply(thisArg, args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return function() {
|
|
|
|
var isImmediate = immediate && !timeoutId;
|
|
|
|
args = arguments;
|
|
|
|
thisArg = this;
|
|
|
|
|
|
|
|
clearTimeout(timeoutId);
|
|
|
|
timeoutId = setTimeout(delayed, wait);
|
|
|
|
|
|
|
|
if (isImmediate) {
|
|
|
|
result = func.apply(thisArg, args);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Executes the `func` function after `wait` milliseconds. Additional arguments
|
|
|
|
* will be passed to `func` when it is invoked.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Functions
|
|
|
|
* @param {Function} func The function to delay.
|
|
|
|
* @param {Number} wait The number of milliseconds to delay execution.
|
|
|
|
* @param {Mixed} [arg1, arg2, ...] Arguments to invoke the function with.
|
|
|
|
* @returns {Number} Returns the `setTimeout` timeout id.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* var log = _.bind(console.log, console);
|
|
|
|
* _.delay(log, 1000, 'logged later');
|
|
|
|
* // => 'logged later' (Appears after one second.)
|
|
|
|
*/
|
|
|
|
function delay(func, wait) {
|
|
|
|
var args = slice.call(arguments, 2);
|
|
|
|
return setTimeout(function() { return func.apply(undefined, args); }, wait);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Defers executing the `func` function until the current call stack has cleared.
|
|
|
|
* Additional arguments will be passed to `func` when it is invoked.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Functions
|
|
|
|
* @param {Function} func The function to defer.
|
|
|
|
* @param {Mixed} [arg1, arg2, ...] Arguments to invoke the function with.
|
|
|
|
* @returns {Number} Returns the `setTimeout` timeout id.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* _.defer(function() { alert('deferred'); });
|
|
|
|
* // returns from the function before `alert` is called
|
|
|
|
*/
|
|
|
|
function defer(func) {
|
|
|
|
var args = slice.call(arguments, 1);
|
|
|
|
return setTimeout(function() { return func.apply(undefined, args); }, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a function that, when called, invokes `object[methodName]` and
|
|
|
|
* prepends any additional `lateBind` arguments to those passed to the bound
|
|
|
|
* function. This method differs from `_.bind` by allowing bound functions to
|
|
|
|
* reference methods that will be redefined or don't yet exist.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Functions
|
|
|
|
* @param {Object} object The object the method belongs to.
|
|
|
|
* @param {String} methodName The method name.
|
|
|
|
* @param {Mixed} [arg1, arg2, ...] Arguments to be partially applied.
|
|
|
|
* @returns {Function} Returns the new bound function.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* var object = {
|
|
|
|
* 'name': 'moe',
|
|
|
|
* 'greet': function(greeting) {
|
|
|
|
* return greeting + ' ' + this.name;
|
|
|
|
* }
|
|
|
|
* };
|
|
|
|
*
|
|
|
|
* var func = _.lateBind(object, 'greet', 'hi');
|
|
|
|
* func();
|
|
|
|
* // => 'hi moe'
|
|
|
|
*
|
|
|
|
* object.greet = function(greeting) {
|
|
|
|
* return greeting + ', ' + this.name + '!';
|
|
|
|
* };
|
|
|
|
*
|
|
|
|
* func();
|
|
|
|
* // => 'hi, moe!'
|
|
|
|
*/
|
|
|
|
function lateBind(object, methodName) {
|
|
|
|
return createBound(methodName, object, slice.call(arguments, 2));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a function that memoizes the result of `func`. If `resolver` is
|
|
|
|
* passed, it will be used to determine the cache key for storing the result
|
|
|
|
* based on the arguments passed to the memoized function. By default, the first
|
|
|
|
* argument passed to the memoized function is used as the cache key.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Functions
|
|
|
|
* @param {Function} func The function to have its output memoized.
|
|
|
|
* @param {Function} [resolver] A function used to resolve the cache key.
|
|
|
|
* @returns {Function} Returns the new memoizing function.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* var fibonacci = _.memoize(function(n) {
|
|
|
|
* return n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2);
|
|
|
|
* });
|
|
|
|
*/
|
|
|
|
function memoize(func, resolver) {
|
|
|
|
var cache = {};
|
|
|
|
return function() {
|
|
|
|
var prop = resolver ? resolver.apply(this, arguments) : arguments[0];
|
|
|
|
return hasOwnProperty.call(cache, prop)
|
|
|
|
? cache[prop]
|
|
|
|
: (cache[prop] = func.apply(this, arguments));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a function that is restricted to one execution. Repeat calls to
|
|
|
|
* the function will return the value of the first call.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Functions
|
|
|
|
* @param {Function} func The function to restrict.
|
|
|
|
* @returns {Function} Returns the new restricted function.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* var initialize = _.once(createApplication);
|
|
|
|
* initialize();
|
|
|
|
* initialize();
|
|
|
|
* // Application is only created once.
|
|
|
|
*/
|
|
|
|
function once(func) {
|
|
|
|
var result,
|
|
|
|
ran = false;
|
|
|
|
|
|
|
|
return function() {
|
|
|
|
if (ran) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
ran = true;
|
|
|
|
result = func.apply(this, arguments);
|
|
|
|
|
|
|
|
// clear the `func` variable so the function may be garbage collected
|
|
|
|
func = null;
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a function that, when called, invokes `func` with any additional
|
|
|
|
* `partial` arguments prepended to those passed to the new function. This method
|
|
|
|
* is similar to `bind`, except it does **not** alter the `this` binding.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Functions
|
|
|
|
* @param {Function} func The function to partially apply arguments to.
|
|
|
|
* @param {Mixed} [arg1, arg2, ...] Arguments to be partially applied.
|
|
|
|
* @returns {Function} Returns the new partially applied function.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* var greet = function(greeting, name) { return greeting + ': ' + name; };
|
|
|
|
* var hi = _.partial(greet, 'hi');
|
|
|
|
* hi('moe');
|
|
|
|
* // => 'hi: moe'
|
|
|
|
*/
|
|
|
|
function partial(func) {
|
|
|
|
return createBound(func, slice.call(arguments, 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a function that, when executed, will only call the `func`
|
|
|
|
* function at most once per every `wait` milliseconds. If the throttled
|
|
|
|
* function is invoked more than once during the `wait` timeout, `func` will
|
|
|
|
* also be called on the trailing edge of the timeout. Subsequent calls to the
|
|
|
|
* throttled function will return the result of the last `func` call.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Functions
|
|
|
|
* @param {Function} func The function to throttle.
|
|
|
|
* @param {Number} wait The number of milliseconds to throttle executions to.
|
|
|
|
* @returns {Function} Returns the new throttled function.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* var throttled = _.throttle(updatePosition, 100);
|
|
|
|
* jQuery(window).on('scroll', throttled);
|
|
|
|
*/
|
|
|
|
function throttle(func, wait) {
|
|
|
|
var args,
|
|
|
|
result,
|
|
|
|
thisArg,
|
|
|
|
timeoutId,
|
|
|
|
lastCalled = 0;
|
|
|
|
|
|
|
|
function trailingCall() {
|
|
|
|
lastCalled = new Date;
|
|
|
|
timeoutId = null;
|
|
|
|
result = func.apply(thisArg, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
return function() {
|
|
|
|
var now = new Date,
|
|
|
|
remain = wait - (now - lastCalled);
|
|
|
|
|
|
|
|
args = arguments;
|
|
|
|
thisArg = this;
|
|
|
|
|
|
|
|
if (remain <= 0) {
|
|
|
|
lastCalled = now;
|
|
|
|
result = func.apply(thisArg, args);
|
|
|
|
}
|
|
|
|
else if (!timeoutId) {
|
|
|
|
timeoutId = setTimeout(trailingCall, remain);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a function that passes `value` to the `wrapper` function as its
|
|
|
|
* first argument. Additional arguments passed to the new function are appended
|
|
|
|
* to those passed to the `wrapper` function.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Functions
|
|
|
|
* @param {Mixed} value The value to wrap.
|
|
|
|
* @param {Function} wrapper The wrapper function.
|
|
|
|
* @returns {Function} Returns the new function.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* var hello = function(name) { return 'hello: ' + name; };
|
|
|
|
* hello = _.wrap(hello, function(func) {
|
|
|
|
* return 'before, ' + func('moe') + ', after';
|
|
|
|
* });
|
|
|
|
* hello();
|
|
|
|
* // => 'before, hello: moe, after'
|
|
|
|
*/
|
|
|
|
function wrap(value, wrapper) {
|
|
|
|
return function() {
|
|
|
|
var args = [value];
|
|
|
|
if (arguments.length) {
|
|
|
|
push.apply(args, arguments);
|
|
|
|
}
|
|
|
|
return wrapper.apply(this, args);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts the characters `&`, `<`, `>`, `"`, and `'` in `string` to their
|
|
|
|
* corresponding HTML entities.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Utilities
|
|
|
|
* @param {String} string The string to escape.
|
|
|
|
* @returns {String} Returns the escaped string.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* _.escape('Moe, Larry & Curly');
|
|
|
|
* // => "Moe, Larry & Curly"
|
|
|
|
*/
|
|
|
|
function escape(string) {
|
|
|
|
return string == null ? '' : (string + '').replace(reUnescapedHtml, escapeHtmlChar);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function returns the first argument passed to it.
|
|
|
|
*
|
|
|
|
* Note: It is used throughout Lo-Dash as a default callback.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Utilities
|
|
|
|
* @param {Mixed} value Any value.
|
|
|
|
* @returns {Mixed} Returns `value`.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* var moe = { 'name': 'moe' };
|
|
|
|
* moe === _.identity(moe);
|
|
|
|
* // => true
|
|
|
|
*/
|
|
|
|
function identity(value) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds functions properties of `object` to the `lodash` function and chainable
|
|
|
|
* wrapper.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Utilities
|
|
|
|
* @param {Object} object The object of function properties to add to `lodash`.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* _.mixin({
|
|
|
|
* 'capitalize': function(string) {
|
|
|
|
* return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase();
|
|
|
|
* }
|
|
|
|
* });
|
|
|
|
*
|
|
|
|
* _.capitalize('larry');
|
|
|
|
* // => 'Larry'
|
|
|
|
*
|
|
|
|
* _('curly').capitalize();
|
|
|
|
* // => 'Curly'
|
|
|
|
*/
|
|
|
|
function mixin(object) {
|
|
|
|
forEach(functions(object), function(methodName) {
|
|
|
|
var func = lodash[methodName] = object[methodName];
|
|
|
|
|
|
|
|
lodash.prototype[methodName] = function() {
|
|
|
|
var args = [this.__wrapped__];
|
|
|
|
if (arguments.length) {
|
|
|
|
push.apply(args, arguments);
|
|
|
|
}
|
|
|
|
var result = func.apply(lodash, args);
|
|
|
|
if (this.__chain__) {
|
|
|
|
result = new lodash(result);
|
|
|
|
result.__chain__ = true;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reverts the '_' variable to its previous value and returns a reference to
|
|
|
|
* the `lodash` function.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Utilities
|
|
|
|
* @returns {Function} Returns the `lodash` function.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* var lodash = _.noConflict();
|
|
|
|
*/
|
|
|
|
function noConflict() {
|
|
|
|
window._ = oldDash;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Produces a random number between `min` and `max` (inclusive). If only one
|
|
|
|
* argument is passed, a number between `0` and the given number will be returned.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Utilities
|
|
|
|
* @param {Number} [min=0] The minimum possible value.
|
|
|
|
* @param {Number} [max=1] The maximum possible value.
|
|
|
|
* @returns {Number} Returns a random number.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* _.random(0, 5);
|
|
|
|
* // => a number between 1 and 5
|
|
|
|
*
|
|
|
|
* _.random(5);
|
|
|
|
* // => also a number between 1 and 5
|
|
|
|
*/
|
|
|
|
function random(min, max) {
|
|
|
|
if (min == null && max == null) {
|
|
|
|
max = 1;
|
|
|
|
}
|
|
|
|
min = +min || 0;
|
|
|
|
if (max == null) {
|
|
|
|
max = min;
|
|
|
|
min = 0;
|
|
|
|
}
|
|
|
|
return min + nativeFloor(nativeRandom() * ((+max || 0) - min + 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolves the value of `property` on `object`. If `property` is a function
|
|
|
|
* it will be invoked and its result returned, else the property value is
|
|
|
|
* returned. If `object` is falsey, then `null` is returned.
|
|
|
|
*
|
|
|
|
* @deprecated
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Utilities
|
|
|
|
* @param {Object} object The object to inspect.
|
|
|
|
* @param {String} property The property to get the value of.
|
|
|
|
* @returns {Mixed} Returns the resolved value.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* var object = {
|
|
|
|
* 'cheese': 'crumpets',
|
|
|
|
* 'stuff': function() {
|
|
|
|
* return 'nonsense';
|
|
|
|
* }
|
|
|
|
* };
|
|
|
|
*
|
|
|
|
* _.result(object, 'cheese');
|
|
|
|
* // => 'crumpets'
|
|
|
|
*
|
|
|
|
* _.result(object, 'stuff');
|
|
|
|
* // => 'nonsense'
|
|
|
|
*/
|
|
|
|
function result(object, property) {
|
|
|
|
// based on Backbone's private `getValue` function
|
|
|
|
// https://github.com/documentcloud/backbone/blob/0.9.2/backbone.js#L1419-1424
|
|
|
|
var value = object ? object[property] : null;
|
|
|
|
return isFunction(value) ? object[property]() : value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A micro-templating method that handles arbitrary delimiters, preserves
|
|
|
|
* whitespace, and correctly escapes quotes within interpolated code.
|
|
|
|
*
|
|
|
|
* Note: In the development build `_.template` utilizes sourceURLs for easier
|
|
|
|
* debugging. See http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/#toc-sourceurl
|
|
|
|
*
|
|
|
|
* Note: Lo-Dash may be used in Chrome extensions by either creating a `lodash csp`
|
|
|
|
* build and avoiding `_.template` use, or loading Lo-Dash in a sandboxed page.
|
|
|
|
* See http://developer.chrome.com/trunk/extensions/sandboxingEval.html
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Utilities
|
|
|
|
* @param {String} text The template text.
|
|
|
|
* @param {Obect} data The data object used to populate the text.
|
|
|
|
* @param {Object} options The options object.
|
|
|
|
* @returns {Function|String} Returns a compiled function when no `data` object
|
|
|
|
* is given, else it returns the interpolated text.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* // using a compiled template
|
|
|
|
* var compiled = _.template('hello: <%= name %>');
|
|
|
|
* compiled({ 'name': 'moe' });
|
|
|
|
* // => 'hello: moe'
|
|
|
|
*
|
|
|
|
* var list = '<% _.forEach(people, function(name) { %><li><%= name %></li><% }); %>';
|
|
|
|
* _.template(list, { 'people': ['moe', 'larry', 'curly'] });
|
|
|
|
* // => '<li>moe</li><li>larry</li><li>curly</li>'
|
|
|
|
*
|
|
|
|
* // using the "escape" delimiter to escape HTML in data property values
|
|
|
|
* _.template('<b><%- value %></b>', { 'value': '<script>' });
|
|
|
|
* // => '<b><script></b>'
|
|
|
|
*
|
|
|
|
* // using the internal `print` function in "evaluate" delimiters
|
|
|
|
* _.template('<% print("Hello " + epithet); %>.', { 'epithet': 'stooge' });
|
|
|
|
* // => 'Hello stooge.'
|
|
|
|
*
|
|
|
|
* // using custom template delimiter settings
|
|
|
|
* _.templateSettings = {
|
|
|
|
* 'interpolate': /\{\{([\s\S]+?)\}\}/g
|
|
|
|
* };
|
|
|
|
*
|
|
|
|
* _.template('Hello {{ name }}!', { 'name': 'Mustache' });
|
|
|
|
* // => 'Hello Mustache!'
|
|
|
|
*
|
|
|
|
* // using the `variable` option to ensure a with-statement isn't used in the compiled template
|
|
|
|
* var compiled = _.template('hello: <%= data.name %>', null, { 'variable': 'data' });
|
|
|
|
* compiled.source;
|
|
|
|
* // => function(data) {
|
|
|
|
* var __t, __p = '', __e = _.escape;
|
|
|
|
* __p += 'hello: ' + ((__t = ( data.name )) == null ? '' : __t);
|
|
|
|
* return __p;
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* // using the `source` property to inline compiled templates for meaningful
|
|
|
|
* // line numbers in error messages and a stack trace
|
|
|
|
* fs.writeFileSync(path.join(cwd, 'jst.js'), '\
|
|
|
|
* var JST = {\
|
|
|
|
* "main": ' + _.template(mainText).source + '\
|
|
|
|
* };\
|
|
|
|
* ');
|
|
|
|
*/
|
|
|
|
function template(text, data, options) {
|
|
|
|
// based on John Resig's `tmpl` implementation
|
|
|
|
// http://ejohn.org/blog/javascript-micro-templating/
|
|
|
|
// and Laura Doktorova's doT.js
|
|
|
|
// https://github.com/olado/doT
|
|
|
|
text += '';
|
|
|
|
options || (options = {});
|
|
|
|
|
|
|
|
var isEvaluating,
|
|
|
|
result,
|
|
|
|
index = 0,
|
|
|
|
settings = lodash.templateSettings,
|
|
|
|
source = "__p += '",
|
|
|
|
variable = options.variable || settings.variable,
|
|
|
|
hasVariable = variable;
|
|
|
|
|
|
|
|
// compile regexp to match each delimiter
|
|
|
|
var reDelimiters = RegExp(
|
|
|
|
(options.escape || settings.escape || reNoMatch).source + '|' +
|
|
|
|
(options.interpolate || settings.interpolate || reNoMatch).source + '|' +
|
|
|
|
(options.evaluate || settings.evaluate || reNoMatch).source + '|$'
|
|
|
|
, 'g');
|
|
|
|
|
|
|
|
text.replace(reDelimiters, function(match, escapeValue, interpolateValue, evaluateValue, offset) {
|
|
|
|
// escape characters that cannot be included in string literals
|
|
|
|
source += text.slice(index, offset).replace(reUnescapedString, escapeStringChar);
|
|
|
|
|
|
|
|
// replace delimiters with snippets
|
|
|
|
source +=
|
|
|
|
escapeValue ? "' +\n__e(" + escapeValue + ") +\n'" :
|
|
|
|
evaluateValue ? "';\n" + evaluateValue + ";\n__p += '" :
|
|
|
|
interpolateValue ? "' +\n((__t = (" + interpolateValue + ")) == null ? '' : __t) +\n'" : '';
|
|
|
|
|
|
|
|
isEvaluating || (isEvaluating = evaluateValue || reComplexDelimiter.test(escapeValue || interpolateValue));
|
|
|
|
index = offset + match.length;
|
|
|
|
});
|
|
|
|
|
|
|
|
source += "';\n";
|
|
|
|
|
|
|
|
// if `variable` is not specified and the template contains "evaluate"
|
|
|
|
// delimiters, wrap a with-statement around the generated code to add the
|
|
|
|
// data object to the top of the scope chain
|
|
|
|
if (!hasVariable) {
|
|
|
|
variable = 'obj';
|
|
|
|
if (isEvaluating) {
|
|
|
|
source = 'with (' + variable + ') {\n' + source + '\n}\n';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// avoid a with-statement by prepending data object references to property names
|
|
|
|
var reDoubleVariable = RegExp('(\\(\\s*)' + variable + '\\.' + variable + '\\b', 'g');
|
|
|
|
source = source
|
|
|
|
.replace(reInsertVariable, '$&' + variable + '.')
|
|
|
|
.replace(reDoubleVariable, '$1__d');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// cleanup code by stripping empty strings
|
|
|
|
source = (isEvaluating ? source.replace(reEmptyStringLeading, '') : source)
|
|
|
|
.replace(reEmptyStringMiddle, '$1')
|
|
|
|
.replace(reEmptyStringTrailing, '$1;');
|
|
|
|
|
|
|
|
// frame code as the function body
|
|
|
|
source = 'function(' + variable + ') {\n' +
|
|
|
|
(hasVariable ? '' : variable + ' || (' + variable + ' = {});\n') +
|
|
|
|
'var __t, __p = \'\', __e = _.escape' +
|
|
|
|
(isEvaluating
|
|
|
|
? ', __j = Array.prototype.join;\n' +
|
|
|
|
'function print() { __p += __j.call(arguments, \'\') }\n'
|
|
|
|
: (hasVariable ? '' : ', __d = ' + variable + '.' + variable + ' || ' + variable) + ';\n'
|
|
|
|
) +
|
|
|
|
source +
|
|
|
|
'return __p\n}';
|
|
|
|
|
|
|
|
// use a sourceURL for easier debugging
|
|
|
|
// http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/#toc-sourceurl
|
|
|
|
var sourceURL = useSourceURL
|
|
|
|
? '\n//@ sourceURL=/lodash/template/source[' + (templateCounter++) + ']'
|
|
|
|
: '';
|
|
|
|
|
|
|
|
try {
|
|
|
|
result = Function('_', 'return ' + source + sourceURL)(lodash);
|
|
|
|
} catch(e) {
|
|
|
|
e.source = source;
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data) {
|
|
|
|
return result(data);
|
|
|
|
}
|
|
|
|
// provide the compiled function's source via its `toString` method, in
|
|
|
|
// supported environments, or the `source` property as a convenience for
|
|
|
|
// inlining compiled templates during the build process
|
|
|
|
result.source = source;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Executes the `callback` function `n` times, returning an array of the results
|
|
|
|
* of each `callback` execution. The `callback` is bound to `thisArg` and invoked
|
|
|
|
* with one argument; (index).
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Utilities
|
|
|
|
* @param {Number} n The number of times to execute the callback.
|
|
|
|
* @param {Function} callback The function called per iteration.
|
|
|
|
* @param {Mixed} [thisArg] The `this` binding of `callback`.
|
|
|
|
* @returns {Array} Returns a new array of the results of each `callback` execution.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* var diceRolls = _.times(3, _.partial(_.random, 1, 6));
|
|
|
|
* // => [3, 6, 4]
|
|
|
|
*
|
|
|
|
* _.times(3, function(n) { mage.castSpell(n); });
|
|
|
|
* // => calls `mage.castSpell(n)` three times, passing `n` of `0`, `1`, and `2` respectively
|
|
|
|
*
|
|
|
|
* _.times(3, function(n) { this.cast(n); }, mage);
|
|
|
|
* // => also calls `mage.castSpell(n)` three times
|
|
|
|
*/
|
|
|
|
function times(n, callback, thisArg) {
|
|
|
|
n = +n || 0;
|
|
|
|
var index = -1,
|
|
|
|
result = Array(n);
|
|
|
|
|
|
|
|
while (++index < n) {
|
|
|
|
result[index] = callback.call(thisArg, index);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts the HTML entities `&`, `<`, `>`, `"`, and `'`
|
|
|
|
* in `string` to their corresponding characters.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Utilities
|
|
|
|
* @param {String} string The string to unescape.
|
|
|
|
* @returns {String} Returns the unescaped string.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* _.unescape('Moe, Larry & Curly');
|
|
|
|
* // => "Moe, Larry & Curly"
|
|
|
|
*/
|
|
|
|
function unescape(string) {
|
|
|
|
return string == null ? '' : (string + '').replace(reEscapedHtml, unescapeHtmlChar);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a unique id. If `prefix` is passed, the id will be appended to it.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Utilities
|
|
|
|
* @param {String} [prefix] The value to prefix the id with.
|
|
|
|
* @returns {Number|String} Returns a numeric id if no prefix is passed, else
|
|
|
|
* a string id may be returned.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* _.uniqueId('contact_');
|
|
|
|
* // => 'contact_104'
|
|
|
|
*/
|
|
|
|
function uniqueId(prefix) {
|
|
|
|
var id = idCounter++;
|
|
|
|
return prefix ? prefix + id : id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wraps the value in a `lodash` wrapper object.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Chaining
|
|
|
|
* @param {Mixed} value The value to wrap.
|
|
|
|
* @returns {Object} Returns the wrapper object.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* var stooges = [
|
|
|
|
* { 'name': 'moe', 'age': 40 },
|
|
|
|
* { 'name': 'larry', 'age': 50 },
|
|
|
|
* { 'name': 'curly', 'age': 60 }
|
|
|
|
* ];
|
|
|
|
*
|
|
|
|
* var youngest = _.chain(stooges)
|
|
|
|
* .sortBy(function(stooge) { return stooge.age; })
|
|
|
|
* .map(function(stooge) { return stooge.name + ' is ' + stooge.age; })
|
|
|
|
* .first()
|
|
|
|
* .value();
|
|
|
|
* // => 'moe is 40'
|
|
|
|
*/
|
|
|
|
function chain(value) {
|
|
|
|
value = new lodash(value);
|
|
|
|
value.__chain__ = true;
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invokes `interceptor` with the `value` as the first argument, and then
|
|
|
|
* returns `value`. The purpose of this method is to "tap into" a method chain,
|
|
|
|
* in order to perform operations on intermediate results within the chain.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @category Chaining
|
|
|
|
* @param {Mixed} value The value to pass to `interceptor`.
|
|
|
|
* @param {Function} interceptor The function to invoke.
|
|
|
|
* @returns {Mixed} Returns `value`.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* _.chain([1, 2, 3, 200])
|
|
|
|
* .filter(function(num) { return num % 2 == 0; })
|
|
|
|
* .tap(alert)
|
|
|
|
* .map(function(num) { return num * num })
|
|
|
|
* .value();
|
|
|
|
* // => // [2, 200] (alerted)
|
|
|
|
* // => [4, 40000]
|
|
|
|
*/
|
|
|
|
function tap(value, interceptor) {
|
|
|
|
interceptor(value);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enables method chaining on the wrapper object.
|
|
|
|
*
|
|
|
|
* @name chain
|
|
|
|
* @deprecated
|
|
|
|
* @memberOf _
|
|
|
|
* @category Chaining
|
|
|
|
* @returns {Mixed} Returns the wrapper object.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* _([1, 2, 3]).value();
|
|
|
|
* // => [1, 2, 3]
|
|
|
|
*/
|
|
|
|
function wrapperChain() {
|
|
|
|
this.__chain__ = true;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extracts the wrapped value.
|
|
|
|
*
|
|
|
|
* @name value
|
|
|
|
* @memberOf _
|
|
|
|
* @category Chaining
|
|
|
|
* @returns {Mixed} Returns the wrapped value.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* _([1, 2, 3]).value();
|
|
|
|
* // => [1, 2, 3]
|
|
|
|
*/
|
|
|
|
function wrapperValue() {
|
|
|
|
return this.__wrapped__;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The semantic version number.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @memberOf _
|
|
|
|
* @type String
|
|
|
|
*/
|
|
|
|
lodash.VERSION = '0.8.1';
|
|
|
|
|
|
|
|
// assign static methods
|
|
|
|
lodash.after = after;
|
|
|
|
lodash.bind = bind;
|
|
|
|
lodash.bindAll = bindAll;
|
|
|
|
lodash.chain = chain;
|
|
|
|
lodash.clone = clone;
|
|
|
|
lodash.compact = compact;
|
|
|
|
lodash.compose = compose;
|
|
|
|
lodash.contains = contains;
|
|
|
|
lodash.countBy = countBy;
|
|
|
|
lodash.debounce = debounce;
|
|
|
|
lodash.defaults = defaults;
|
|
|
|
lodash.defer = defer;
|
|
|
|
lodash.delay = delay;
|
|
|
|
lodash.difference = difference;
|
|
|
|
lodash.escape = escape;
|
|
|
|
lodash.every = every;
|
|
|
|
lodash.extend = extend;
|
|
|
|
lodash.filter = filter;
|
|
|
|
lodash.find = find;
|
|
|
|
lodash.first = first;
|
|
|
|
lodash.flatten = flatten;
|
|
|
|
lodash.forEach = forEach;
|
|
|
|
lodash.forIn = forIn;
|
|
|
|
lodash.forOwn = forOwn;
|
|
|
|
lodash.functions = functions;
|
|
|
|
lodash.groupBy = groupBy;
|
|
|
|
lodash.has = has;
|
|
|
|
lodash.identity = identity;
|
|
|
|
lodash.indexOf = indexOf;
|
|
|
|
lodash.initial = initial;
|
|
|
|
lodash.intersection = intersection;
|
|
|
|
lodash.invert = invert;
|
|
|
|
lodash.invoke = invoke;
|
|
|
|
lodash.isArguments = isArguments;
|
|
|
|
lodash.isArray = isArray;
|
|
|
|
lodash.isBoolean = isBoolean;
|
|
|
|
lodash.isDate = isDate;
|
|
|
|
lodash.isElement = isElement;
|
|
|
|
lodash.isEmpty = isEmpty;
|
|
|
|
lodash.isEqual = isEqual;
|
|
|
|
lodash.isFinite = isFinite;
|
|
|
|
lodash.isFunction = isFunction;
|
|
|
|
lodash.isNaN = isNaN;
|
|
|
|
lodash.isNull = isNull;
|
|
|
|
lodash.isNumber = isNumber;
|
|
|
|
lodash.isObject = isObject;
|
|
|
|
lodash.isPlainObject = isPlainObject;
|
|
|
|
lodash.isRegExp = isRegExp;
|
|
|
|
lodash.isString = isString;
|
|
|
|
lodash.isUndefined = isUndefined;
|
|
|
|
lodash.keys = keys;
|
|
|
|
lodash.last = last;
|
|
|
|
lodash.lastIndexOf = lastIndexOf;
|
|
|
|
lodash.lateBind = lateBind;
|
|
|
|
lodash.map = map;
|
|
|
|
lodash.max = max;
|
|
|
|
lodash.memoize = memoize;
|
|
|
|
lodash.merge = merge;
|
|
|
|
lodash.min = min;
|
|
|
|
lodash.mixin = mixin;
|
|
|
|
lodash.noConflict = noConflict;
|
|
|
|
lodash.object = object;
|
|
|
|
lodash.omit = omit;
|
|
|
|
lodash.once = once;
|
|
|
|
lodash.pairs = pairs;
|
|
|
|
lodash.partial = partial;
|
|
|
|
lodash.pick = pick;
|
|
|
|
lodash.pluck = pluck;
|
|
|
|
lodash.random = random;
|
|
|
|
lodash.range = range;
|
|
|
|
lodash.reduce = reduce;
|
|
|
|
lodash.reduceRight = reduceRight;
|
|
|
|
lodash.reject = reject;
|
|
|
|
lodash.rest = rest;
|
|
|
|
lodash.result = result;
|
|
|
|
lodash.shuffle = shuffle;
|
|
|
|
lodash.size = size;
|
|
|
|
lodash.some = some;
|
|
|
|
lodash.sortBy = sortBy;
|
|
|
|
lodash.sortedIndex = sortedIndex;
|
|
|
|
lodash.tap = tap;
|
|
|
|
lodash.template = template;
|
|
|
|
lodash.throttle = throttle;
|
|
|
|
lodash.times = times;
|
|
|
|
lodash.toArray = toArray;
|
|
|
|
lodash.unescape = unescape;
|
|
|
|
lodash.union = union;
|
|
|
|
lodash.uniq = uniq;
|
|
|
|
lodash.uniqueId = uniqueId;
|
|
|
|
lodash.values = values;
|
|
|
|
lodash.where = where;
|
|
|
|
lodash.without = without;
|
|
|
|
lodash.wrap = wrap;
|
|
|
|
lodash.zip = zip;
|
|
|
|
|
|
|
|
// assign aliases
|
|
|
|
lodash.all = every;
|
|
|
|
lodash.any = some;
|
|
|
|
lodash.collect = map;
|
|
|
|
lodash.detect = find;
|
|
|
|
lodash.drop = rest;
|
|
|
|
lodash.each = forEach;
|
|
|
|
lodash.foldl = reduce;
|
|
|
|
lodash.foldr = reduceRight;
|
|
|
|
lodash.head = first;
|
|
|
|
lodash.include = contains;
|
|
|
|
lodash.inject = reduce;
|
|
|
|
lodash.methods = functions;
|
|
|
|
lodash.select = filter;
|
|
|
|
lodash.tail = rest;
|
|
|
|
lodash.take = first;
|
|
|
|
lodash.unique = uniq;
|
|
|
|
|
|
|
|
// add pseudo private properties used and removed during the build process
|
|
|
|
lodash._iteratorTemplate = iteratorTemplate;
|
|
|
|
lodash._shimKeys = shimKeys;
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
// add all static functions to `lodash.prototype`
|
|
|
|
mixin(lodash);
|
|
|
|
|
|
|
|
// add `lodash.prototype.chain` after calling `mixin()` to avoid overwriting
|
|
|
|
// it with the wrapped `lodash.chain`
|
|
|
|
lodash.prototype.chain = wrapperChain;
|
|
|
|
lodash.prototype.value = wrapperValue;
|
|
|
|
|
|
|
|
// add all mutator Array functions to the wrapper.
|
|
|
|
forEach(['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'], function(methodName) {
|
|
|
|
var func = ArrayProto[methodName];
|
|
|
|
|
|
|
|
lodash.prototype[methodName] = function() {
|
|
|
|
var value = this.__wrapped__;
|
|
|
|
func.apply(value, arguments);
|
|
|
|
|
|
|
|
// avoid array-like object bugs with `Array#shift` and `Array#splice` in
|
|
|
|
// Firefox < 10 and IE < 9
|
|
|
|
if (hasObjectSpliceBug && value.length === 0) {
|
|
|
|
delete value[0];
|
|
|
|
}
|
|
|
|
if (this.__chain__) {
|
|
|
|
value = new lodash(value);
|
|
|
|
value.__chain__ = true;
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
// add all accessor Array functions to the wrapper.
|
|
|
|
forEach(['concat', 'join', 'slice'], function(methodName) {
|
|
|
|
var func = ArrayProto[methodName];
|
|
|
|
|
|
|
|
lodash.prototype[methodName] = function() {
|
|
|
|
var value = this.__wrapped__,
|
|
|
|
result = func.apply(value, arguments);
|
|
|
|
|
|
|
|
if (this.__chain__) {
|
|
|
|
result = new lodash(result);
|
|
|
|
result.__chain__ = true;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
// expose Lo-Dash
|
|
|
|
// some AMD build optimizers, like r.js, check for specific condition patterns like the following:
|
|
|
|
if (typeof define == 'function' && typeof define.amd == 'object' && define.amd) {
|
|
|
|
// Expose Lo-Dash to the global object even when an AMD loader is present in
|
|
|
|
// case Lo-Dash was injected by a third-party script and not intended to be
|
|
|
|
// loaded as a module. The global assignment can be reverted in the Lo-Dash
|
|
|
|
// module via its `noConflict()` method.
|
|
|
|
window._ = lodash;
|
|
|
|
|
|
|
|
// define as an anonymous module so, through path mapping, it can be
|
|
|
|
// referenced as the "underscore" module
|
|
|
|
define('lodash',[],function() {
|
|
|
|
return lodash;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
// check for `exports` after `define` in case a build optimizer adds an `exports` object
|
|
|
|
else if (freeExports) {
|
|
|
|
// in Node.js or RingoJS v0.8.0+
|
|
|
|
if (typeof module == 'object' && module && module.exports == freeExports) {
|
|
|
|
(module.exports = lodash)._ = lodash;
|
|
|
|
}
|
|
|
|
// in Narwhal or RingoJS v0.7.0-
|
|
|
|
else {
|
|
|
|
freeExports._ = lodash;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// in a browser or Rhino
|
|
|
|
window._ = lodash;
|
|
|
|
}
|
|
|
|
}(this));
|
|
|
|
|
2013-11-08 20:33:44 +00:00
|
|
|
/*!
|
|
|
|
* Shim implementation of the TextEncoder, TextDecoder spec:
|
|
|
|
* http://encoding.spec.whatwg.org/#interface-textencoder
|
|
|
|
*
|
|
|
|
* http://code.google.com/p/stringencoding/source/browse/encoding-indexes.js
|
|
|
|
* Sep 3, 2013 14070a3029e3
|
|
|
|
* Used under Apache License 2.0 - http://code.google.com/p/stringencoding/
|
|
|
|
*/
|
|
|
|
self["encoding-indexes"] = {
|
|
|
|
"big5":[null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null,
|
|
|
|
"euc-kr":[44034,44035,44037,44038,44043,44044,44045,44046,44047,44056,44062,44063,44065,44066,44067,44069,44070,44071,44072,44073,44074,44075,44078,44082,44083,44084,44085,44086,44087,44090,44091,44093,44094,44095,44097,44098,44099,44100,44101,44102,44103,44104,44105,44106,44108,44110,44111,44112,44113,44114,44115,44117,44118,44119,44121,44122,44123,44125,44126,44127,44128,44129,44130,44131,44132,44133,44134,44135,44136,44137,44138,44139,44140,44141,44142,44143,44146,44147,44149,44150,44153,44155,44156,44157,44158,44159,44162,44167,44168,44173,44174,44175,44177,44178,44179,44181,44182,44183,44184,44185,44186,44187,44190,44194,44195,44196,44197,44198,44199,44203,44205,44206,44209,44210,44211,44212,44213,44214,44215,44218,44222,44223,44224,44226,44227,44229,44230,44231,44233,44234,44235,44237,44238,44239,44240,44241,44242,44243,44244,44246,44248,44249,44250,44251,44252,44253,44254,44255,44258,44259,44261,44262,44265,44267,44269,44270,44274,44276,44279,44280,44281,44282,44283,44286,44287,44289,44290,44291,44293,44295,44296,44297,44298,44299,44302,44304,44306,44307,44308,44309,44310,44311,44313,44314,44315,44317,44318,44319,44321,44322,44323,44324,44325,44326,44327,44328,44330,44331,44334,44335,44336,44337,44338,44339,44342,44343,44345,44346,44347,44349,44350,44351,44352,44353,44354,44355,44358,44360,44362,44363,44364,44365,44366,44367,44369,44370,44371,44373,44374,44375,44377,44378,44379,44380,44381,44382,44383,44384,44386,44388,44389,44390,44391,44392,44393,44394,44395,44398,44399,44401,44402,44407,44408,44409,44410,44414,44416,44419,44420,44421,44422,44423,44426,44427,44429,44430,44431,44433,44434,44435,44436,44437,44438,44439,44440,44441,44442,44443,44446,44447,44448,44449,44450,44451,44453,44454,44455,44456,44457,44458,44459,44460,44461,44462,44463,44464,44465,44466,44467,44468,44469,44470,44472,44473,44474,44475,44476,44477,44478,44479,44482,44483,44485,44486,44487,44489,44490,44491,44492,44493,44494,44495,44498,44500,44501,44502,44503,44504,44505,44506,44507,44509,44510,44511,44513,44514,44515,44517,44518,44519,44520,44521,44522,44523,44524,44525,44526,44527,44528,44529,44530,44531,44532,44533,44534,44535,44538,44539,44541,44542,44546,44547,44548,44549,44550,44551,44554,44556,44558,44559,44560,44561,44562,44563,44565,44566,44567,44568,44569,44570,44571,44572,44573,44574,44575,44576,44577,44578,44579,44580,44581,44582,44583,44584,44585,44586,44587,44588,44589,44590,44591,44594,44595,44597,44598,44601,44603,44604,44605,44606,44607,44610,44612,44615,44616,44617,44619,44623,44625,44626,44627,44629,44631,44632,44633,44634,44635,44638,44642,44643,44644,44646,44647,44650,44651,44653,44654,44655,44657,44658,44659,44660,44661,44662,44663,44666,44670,44671,44672,44673,44674,44675,44678,44679,44680,44681,44682,44683,44685,44686,44687,44688,44689,44690,44691,44692,44693,44694,44695,44696,44697,44698,44699,44700,44701,44702,44703,44704,44705,44706,44707,44708,44709,44710,44711,44712,44713,44714,44715,44716,44717,44718,44719,44720,44721,44722,44723,44724,44725,44726,44727,44728,44729,44730,44731,44735,44737,44738,44739,44741,44742,44743,44744,44745,44746,44747,44750,44754,44755,44756,44757,44758,44759,44762,44763,44765,44766,44767,44768,44769,44770,44771,44772,44773,44774,44775,44777,44778,44780,44782,44783,44784,44785,44786,44787,44789,44790,44791,44793,44794,44795,44797,44798,44799,44800,44801,44802,44803,44804,44805,44806,44809,44810,44811,44812,44814,44815,44817,44818,44819,44820,44821,44822,44823,44824,44825,44826,44827,44828,44829,44830,44831,44832,44833,44834,44835,44836,44837,44838,44839,44840,44841,44842,44843,44846,44847,44849,44851,44853,44854,44855,44856,44857,44858,44859,44862,44864,44868,44869,44870,44871,44874,44875,44876,44877,44878,44879,44881,44882,44883,44884,44885,44886,44887,44888,44889,44890,44891,44894,44895,44896,44897,44898,44899,44902,44903,44904,44905,44906,44907,44908,44909,44910,44911,44912,44913,44914,44915,44916,44917,44918,44919,44920,44922,44923,44924,44925,44926,44927,44929,44930,44931,44933,44934,44935,44937,44938,44939,44940,44941,44942,44943,44946,44947,44948,44950,44951,44952,44953,44954,449
|
|
|
|
"gbk":[19970, 19972, 19973, 19974, 19983, 19986, 19991, 19999, 20000, 20001, 20003, 20006, 20009, 20014, 20015, 20017, 20019, 20021, 20023, 20028, 20032, 20033, 20034, 20036, 20038, 20042, 20049, 20053, 20055, 20058, 20059, 20066, 20067, 20068, 20069, 20071, 20072, 20074, 20075, 20076, 20077, 20078, 20079, 20082, 20084, 20085, 20086, 20087, 20088, 20089, 20090, 20091, 20092, 20093, 20095, 20096, 20097, 20098, 20099, 20100, 20101, 20103, 20106, 20112, 20118, 20119, 20121, 20124, 20125, 20126, 20131, 20138, 20143, 20144, 20145, 20148, 20150, 20151, 20152, 20153, 20156, 20157, 20158, 20168, 20172, 20175, 20176, 20178, 20186, 20187, 20188, 20192, 20194, 20198, 20199, 20201, 20205, 20206, 20207, 20209, 20212, 20216, 20217, 20218, 20220, 20222, 20224, 20226, 20227, 20228, 20229, 20230, 20231, 20232, 20235, 20236, 20242, 20243, 20244, 20245, 20246, 20252, 20253, 20257, 20259, 20264, 20265, 20268, 20269, 20270, 20273, 20275, 20277, 20279, 20281, 20283, 20286, 20287, 20288, 20289, 20290, 20292, 20293, 20295, 20296, 20297, 20298, 20299, 20300, 20306, 20308, 20310, 20321, 20322, 20326, 20328, 20330, 20331, 20333, 20334, 20337, 20338, 20341, 20343, 20344, 20345, 20346, 20349, 20352, 20353, 20354, 20357, 20358, 20359, 20362, 20364, 20366, 20368, 20370, 20371, 20373, 20374, 20376, 20377, 20378, 20380, 20382, 20383, 20385, 20386, 20388, 20395, 20397, 20400, 20401, 20402, 20403, 20404, 20406, 20407, 20408, 20409, 20410, 20411, 20412, 20413, 20414, 20416, 20417, 20418, 20422, 20423, 20424, 20425, 20427, 20428, 20429, 20434, 20435, 20436, 20437, 20438, 20441, 20443, 20448, 20450, 20452, 20453, 20455, 20459, 20460, 20464, 20466, 20468, 20469, 20470, 20471, 20473, 20475, 20476, 20477, 20479, 20480, 20481, 20482, 20483, 20484, 20485, 20486, 20487, 20488, 20489, 20490, 20491, 20494, 20496, 20497, 20499, 20501, 20502, 20503, 20507, 20509, 20510, 20512, 20514, 20515, 20516, 20519, 20523, 20527, 20528, 20529, 20530, 20531, 20532, 20533, 20534, 20535, 20536, 20537, 20539, 20541, 20543, 20544, 20545, 20546, 20548, 20549, 20550, 20553, 20554, 20555, 20557, 20560, 20561, 20562, 20563, 20564, 20566, 20567, 20568, 20569, 20571, 20573, 20574, 20575, 20576, 20577, 20578, 20579, 20580, 20582, 20583, 20584, 20585, 20586, 20587, 20589, 20590, 20591, 20592, 20593, 20594, 20595, 20596, 20597, 20600, 20601, 20602, 20604, 20605, 20609, 20610, 20611, 20612, 20614, 20615, 20617, 20618, 20619, 20620, 20622, 20623, 20624, 20625, 20626, 20627, 20628, 20629, 20630, 20631, 20632, 20633, 20634, 20635, 20636, 20637, 20638, 20639, 20640, 20641, 20642, 20644, 20646, 20650, 20651, 20653, 20654, 20655, 20656, 20657, 20659, 20660, 20661, 20662, 20663, 20664, 20665, 20668, 20669, 20670, 20671, 20672, 20673, 20674, 20675, 20676, 20677, 20678, 20679, 20680, 20681, 20682, 20683, 20684, 20685, 20686, 20688, 20689, 20690, 20691, 20692, 20693, 20695, 20696, 20697, 20699, 20700, 20701, 20702, 20703, 20704, 20705, 20706, 20707, 20708, 20709, 20712, 20713, 20714, 20715, 20719, 20720, 20721, 20722, 20724, 20726, 20727, 20728, 20729, 20730, 20732, 20733, 20734, 20735, 20736, 20737, 20738, 20739, 20740, 20741, 20744, 20745, 20746, 20748, 20749, 20750, 20751, 20752, 20753, 20755, 20756, 20757, 20758, 20759, 20760, 20761, 20762, 20763, 20764, 20765, 20766, 20767, 20768, 20770, 20771, 20772, 20773, 20774, 20775, 20776, 20777, 20778, 20779, 20780, 20781, 20782, 20783, 20784, 20785, 20786, 20787, 20788, 20789, 20790, 20791, 20792, 20793, 20794, 20795, 20796, 20797, 20798, 20802, 20807, 20810, 20812, 20814, 20815, 20816, 20818, 20819, 20823, 20824, 20825, 20827, 20829, 20830, 20831, 20832, 20833, 20835, 20836, 20838, 20839, 20841, 20842, 20847, 20850, 20858, 20862, 20863, 20867, 20868, 20870, 20871, 20874, 20875, 20878, 20879, 20880, 20881, 20883, 20884, 20888, 20890, 20893, 20894, 20895, 20897, 20899, 20902, 20903, 20904, 20905, 20906, 20909, 20910, 20916, 20920, 20921, 20922, 20926, 20927, 20929, 20930, 20931, 20933, 20936, 20938, 20941, 20942, 20944, 20946, 20947, 20948, 20949, 20950, 20951, 20952, 20953, 20954, 20956, 20958, 20959, 20962, 20963, 20965, 20966, 20967, 20968, 20969, 20970
|
|
|
|
"gb18030":[[0, 128], [36, 165], [38, 169], [45, 178], [50, 184], [81, 216], [89, 226], [95, 235], [96, 238], [100, 244], [103, 248], [104, 251], [105, 253], [109, 258], [126, 276], [133, 284], [148, 300], [172, 325], [175, 329], [179, 334], [208, 364], [306, 463], [307, 465], [308, 467], [309, 469], [310, 471], [311, 473], [312, 475], [313, 477], [341, 506], [428, 594], [443, 610], [544, 712], [545, 716], [558, 730], [741, 930], [742, 938], [749, 962], [750, 970], [805, 1026], [819, 1104], [820, 1106], [7922, 8209], [7924, 8215], [7925, 8218], [7927, 8222], [7934, 8231], [7943, 8241], [7944, 8244], [7945, 8246], [7950, 8252], [8062, 8365], [8148, 8452], [8149, 8454], [8152, 8458], [8164, 8471], [8174, 8482], [8236, 8556], [8240, 8570], [8262, 8596], [8264, 8602], [8374, 8713], [8380, 8720], [8381, 8722], [8384, 8726], [8388, 8731], [8390, 8737], [8392, 8740], [8393, 8742], [8394, 8748], [8396, 8751], [8401, 8760], [8406, 8766], [8416, 8777], [8419, 8781], [8424, 8787], [8437, 8802], [8439, 8808], [8445, 8816], [8482, 8854], [8485, 8858], [8496, 8870], [8521, 8896], [8603, 8979], [8936, 9322], [8946, 9372], [9046, 9548], [9050, 9588], [9063, 9616], [9066, 9622], [9076, 9634], [9092, 9652], [9100, 9662], [9108, 9672], [9111, 9676], [9113, 9680], [9131, 9702], [9162, 9735], [9164, 9738], [9218, 9793], [9219, 9795], [11329, 11906], [11331, 11909], [11334, 11913], [11336, 11917], [11346, 11928], [11361, 11944], [11363, 11947], [11366, 11951], [11370, 11956], [11372, 11960], [11375, 11964], [11389, 11979], [11682, 12284], [11686, 12292], [11687, 12312], [11692, 12319], [11694, 12330], [11714, 12351], [11716, 12436], [11723, 12447], [11725, 12535], [11730, 12543], [11736, 12586], [11982, 12842], [11989, 12850], [12102, 12964], [12336, 13200], [12348, 13215], [12350, 13218], [12384, 13253], [12393, 13263], [12395, 13267], [12397, 13270], [12510, 13384], [12553, 13428], [12851, 13727], [12962, 13839], [12973, 13851], [13738, 14617], [13823, 14703], [13919, 14801], [13933, 14816], [14080, 14964], [14298, 15183], [14585, 15471], [14698, 15585], [15583, 16471], [15847, 16736], [16318, 17208], [16434, 17325], [16438, 17330], [16481, 17374], [16729, 17623], [17102, 17997], [17122, 18018], [17315, 18212], [17320, 18218], [17402, 18301], [17418, 18318], [17859, 18760], [17909, 18811], [17911, 18814], [17915, 18820], [17916, 18823], [17936, 18844], [17939, 18848], [17961, 18872], [18664, 19576], [18703, 19620], [18814, 19738], [18962, 19887], [19043, 40870], [33469, 59244], [33470, 59336], [33471, 59367], [33484, 59413], [33485, 59417], [33490, 59423], [33497, 59431], [33501, 59437], [33505, 59443], [33513, 59452], [33520, 59460], [33536, 59478], [33550, 59493], [37845, 63789], [37921, 63866], [37948, 63894], [38029, 63976], [38038, 63986], [38064, 64016], [38065, 64018], [38066, 64021], [38069, 64025], [38075, 64034], [38076, 64037], [38078, 64042], [39108, 65074], [39109, 65093], [39113, 65107], [39114, 65112], [39115, 65127], [39116, 65132], [39265, 65375], [39394, 65510], [189000, 65536]],
|
|
|
|
"jis0208":[12288, 12289, 12290, 65292, 65294, 12539, 65306, 65307, 65311, 65281, 12443, 12444, 180, 65344, 168, 65342, 65507, 65343, 12541, 12542, 12445, 12446, 12291, 20189, 12293, 12294, 12295, 12540, 8213, 8208, 65295, 65340, 65374, 8741, 65372, 8230, 8229, 8216, 8217, 8220, 8221, 65288, 65289, 12308, 12309, 65339, 65341, 65371, 65373, 12296, 12297, 12298, 12299, 12300, 12301, 12302, 12303, 12304, 12305, 65291, 65293, 177, 215, 247, 65309, 8800, 65308, 65310, 8806, 8807, 8734, 8756, 9794, 9792, 176, 8242, 8243, 8451, 65509, 65284, 65504, 65505, 65285, 65283, 65286, 65290, 65312, 167, 9734, 9733, 9675, 9679, 9678, 9671, 9670, 9633, 9632, 9651, 9650, 9661, 9660, 8251, 12306, 8594, 8592, 8593, 8595, 12307, null, null, null, null, null, null, null, null, null, null, null, 8712, 8715, 8838, 8839, 8834, 8835, 8746, 8745, null, null, null, null, null, null, null, null, 8743, 8744, 65506, 8658, 8660, 8704, 8707, null, null, null, null, null, null, null, null, null, null, null, 8736, 8869, 8978, 8706, 8711, 8801, 8786, 8810, 8811, 8730, 8765, 8733, 8757, 8747, 8748, null, null, null, null, null, null, null, 8491, 8240, 9839, 9837, 9834, 8224, 8225, 182, null, null, null, null, 9711, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, 65296, 65297, 65298, 65299, 65300, 65301, 65302, 65303, 65304, 65305, null, null, null, null, null, null, null, 65313, 65314, 65315, 65316, 65317, 65318, 65319, 65320, 65321, 65322, 65323, 65324, 65325, 65326, 65327, 65328, 65329, 65330, 65331, 65332, 65333, 65334, 65335, 65336, 65337, 65338, null, null, null, null, null, null, 65345, 65346, 65347, 65348, 65349, 65350, 65351, 65352, 65353, 65354, 65355, 65356, 65357, 65358, 65359, 65360, 65361, 65362, 65363, 65364, 65365, 65366, 65367, 65368, 65369, 65370, null, null, null, null, 12353, 12354, 12355, 12356, 12357, 12358, 12359, 12360, 12361, 12362, 12363, 12364, 12365, 12366, 12367, 12368, 12369, 12370, 12371, 12372, 12373, 12374, 12375, 12376, 12377, 12378, 12379, 12380, 12381, 12382, 12383, 12384, 12385, 12386, 12387, 12388, 12389, 12390, 12391, 12392, 12393, 12394, 12395, 12396, 12397, 12398, 12399, 12400, 12401, 12402, 12403, 12404, 12405, 12406, 12407, 12408, 12409, 12410, 12411, 12412, 12413, 12414, 12415, 12416, 12417, 12418, 12419, 12420, 12421, 12422, 12423, 12424, 12425, 12426, 12427, 12428, 12429, 12430, 12431, 12432, 12433, 12434, 12435, null, null, null, null, null, null, null, null, null, null, null, 12449, 12450, 12451, 12452, 12453, 12454, 12455, 12456, 12457, 12458, 12459, 12460, 12461, 12462, 12463, 12464, 12465, 12466, 12467, 12468, 12469, 12470, 12471, 12472, 12473, 12474, 12475, 12476, 12477, 12478, 12479, 12480, 12481, 12482, 12483, 12484, 12485, 12486, 12487, 12488, 12489, 12490, 12491, 12492, 12493, 12494, 12495, 12496, 12497, 12498, 12499, 12500, 12501, 12502, 12503, 12504, 12505, 12506, 12507, 12508, 12509, 12510, 12511, 12512, 12513, 12514, 12515, 12516, 12517, 12518, 12519, 12520, 12521, 12522, 12523, 12524, 12525, 12526, 12527, 12528, 12529, 12530, 12531, 12532, 12533, 12534, null, null, null, null, null, null, null, null, 913, 914, 915, 916, 917, 918, 919, 920, 921, 922, 923, 924, 925, 926, 927, 928, 929, 931, 932, 933, 934, 935, 936, 937, null, null, null, null, null, null, null, null, 945, 946, 947, 948, 949, 950, 951, 952, 953, 954, 955, 956, 957, 958, 959, 960, 961, 963, 964, 965, 966, 967, 968, 969, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, 1040, 1041, 1042, 1043, 1044, 1045, 1025, 1046, 1047, 1048, 1049, 1050, 1051, 1052, 1053, 1054, 1055, 1056, 1057, 1058, 1059, 1060, 1061, 1062, 1063, 1064, 1065, 1066, 1067, 1068, 1069, 1070, 1071, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, 1072, 1073, 1074, 1075, 1076, 1077, 1105, 1078, 1079, 1080, 1081, 1082, 1083, 1084, 1085, 1086, 1087, 1088, 1089, 1090, 1091, 1092, 1093, 1094, 1095, 1096, 1097, 1098, 10
|
|
|
|
"jis0212":[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,728,711,184,729,733,175,731,730,65374,900,901,null,null,null,null,null,null,null,null,161,166,191,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,186,170,169,174,8482,164,8470,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,902,904,905,906,938,null,908,null,910,939,null,911,null,null,null,null,940,941,942,943,970,912,972,962,973,971,944,974,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,1026,1027,1028,1029,1030,1031,1032,1033,1034,1035,1036,1038,1039,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,1106,1107,1108,1109,1110,1111,1112,1113,1114,1115,1116,1118,1119,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,198,272,null,294,null,306,null,321,319,null,330,216,338,null,358,222,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,230,273,240,295,305,307,312,322,320,329,331,248,339,223,359,254,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,nu
|
|
|
|
"ibm866":[1040,1041,1042,1043,1044,1045,1046,1047,1048,1049,1050,1051,1052,1053,1054,1055,1056,1057,1058,1059,1060,1061,1062,1063,1064,1065,1066,1067,1068,1069,1070,1071,1072,1073,1074,1075,1076,1077,1078,1079,1080,1081,1082,1083,1084,1085,1086,1087,9617,9618,9619,9474,9508,9569,9570,9558,9557,9571,9553,9559,9565,9564,9563,9488,9492,9524,9516,9500,9472,9532,9566,9567,9562,9556,9577,9574,9568,9552,9580,9575,9576,9572,9573,9561,9560,9554,9555,9579,9578,9496,9484,9608,9604,9612,9616,9600,1088,1089,1090,1091,1092,1093,1094,1095,1096,1097,1098,1099,1100,1101,1102,1103,1025,1105,1028,1108,1031,1111,1038,1118,176,8729,183,8730,8470,164,9632,160],
|
|
|
|
"iso-8859-2":[128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,260,728,321,164,317,346,167,168,352,350,356,377,173,381,379,176,261,731,322,180,318,347,711,184,353,351,357,378,733,382,380,340,193,194,258,196,313,262,199,268,201,280,203,282,205,206,270,272,323,327,211,212,336,214,215,344,366,218,368,220,221,354,223,341,225,226,259,228,314,263,231,269,233,281,235,283,237,238,271,273,324,328,243,244,337,246,247,345,367,250,369,252,253,355,729],
|
|
|
|
"iso-8859-3":[128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,294,728,163,164,null,292,167,168,304,350,286,308,173,null,379,176,295,178,179,180,181,293,183,184,305,351,287,309,189,null,380,192,193,194,null,196,266,264,199,200,201,202,203,204,205,206,207,null,209,210,211,212,288,214,215,284,217,218,219,220,364,348,223,224,225,226,null,228,267,265,231,232,233,234,235,236,237,238,239,null,241,242,243,244,289,246,247,285,249,250,251,252,365,349,729],
|
|
|
|
"iso-8859-4":[128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,260,312,342,164,296,315,167,168,352,274,290,358,173,381,175,176,261,731,343,180,297,316,711,184,353,275,291,359,330,382,331,256,193,194,195,196,197,198,302,268,201,280,203,278,205,206,298,272,325,332,310,212,213,214,215,216,370,218,219,220,360,362,223,257,225,226,227,228,229,230,303,269,233,281,235,279,237,238,299,273,326,333,311,244,245,246,247,248,371,250,251,252,361,363,729],
|
|
|
|
"iso-8859-5":[128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,1025,1026,1027,1028,1029,1030,1031,1032,1033,1034,1035,1036,173,1038,1039,1040,1041,1042,1043,1044,1045,1046,1047,1048,1049,1050,1051,1052,1053,1054,1055,1056,1057,1058,1059,1060,1061,1062,1063,1064,1065,1066,1067,1068,1069,1070,1071,1072,1073,1074,1075,1076,1077,1078,1079,1080,1081,1082,1083,1084,1085,1086,1087,1088,1089,1090,1091,1092,1093,1094,1095,1096,1097,1098,1099,1100,1101,1102,1103,8470,1105,1106,1107,1108,1109,1110,1111,1112,1113,1114,1115,1116,167,1118,1119],
|
|
|
|
"iso-8859-6":[128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,null,null,null,164,null,null,null,null,null,null,null,1548,173,null,null,null,null,null,null,null,null,null,null,null,null,null,1563,null,null,null,1567,null,1569,1570,1571,1572,1573,1574,1575,1576,1577,1578,1579,1580,1581,1582,1583,1584,1585,1586,1587,1588,1589,1590,1591,1592,1593,1594,null,null,null,null,null,1600,1601,1602,1603,1604,1605,1606,1607,1608,1609,1610,1611,1612,1613,1614,1615,1616,1617,1618,null,null,null,null,null,null,null,null,null,null,null,null,null],
|
|
|
|
"iso-8859-7":[128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,8216,8217,163,8364,8367,166,167,168,169,890,171,172,173,null,8213,176,177,178,179,900,901,902,183,904,905,906,187,908,189,910,911,912,913,914,915,916,917,918,919,920,921,922,923,924,925,926,927,928,929,null,931,932,933,934,935,936,937,938,939,940,941,942,943,944,945,946,947,948,949,950,951,952,953,954,955,956,957,958,959,960,961,962,963,964,965,966,967,968,969,970,971,972,973,974,null],
|
|
|
|
"iso-8859-8":[128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,null,162,163,164,165,166,167,168,169,215,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,247,187,188,189,190,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,8215,1488,1489,1490,1491,1492,1493,1494,1495,1496,1497,1498,1499,1500,1501,1502,1503,1504,1505,1506,1507,1508,1509,1510,1511,1512,1513,1514,null,null,8206,8207,null],
|
|
|
|
"iso-8859-10":[128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,260,274,290,298,296,310,167,315,272,352,358,381,173,362,330,176,261,275,291,299,297,311,183,316,273,353,359,382,8213,363,331,256,193,194,195,196,197,198,302,268,201,280,203,278,205,206,207,208,325,332,211,212,213,214,360,216,370,218,219,220,221,222,223,257,225,226,227,228,229,230,303,269,233,281,235,279,237,238,239,240,326,333,243,244,245,246,361,248,371,250,251,252,253,254,312],
|
|
|
|
"iso-8859-13":[128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,8221,162,163,164,8222,166,167,216,169,342,171,172,173,174,198,176,177,178,179,8220,181,182,183,248,185,343,187,188,189,190,230,260,302,256,262,196,197,280,274,268,201,377,278,290,310,298,315,352,323,325,211,332,213,214,215,370,321,346,362,220,379,381,223,261,303,257,263,228,229,281,275,269,233,378,279,291,311,299,316,353,324,326,243,333,245,246,247,371,322,347,363,252,380,382,8217],
|
|
|
|
"iso-8859-14":[128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,7682,7683,163,266,267,7690,167,7808,169,7810,7691,7922,173,174,376,7710,7711,288,289,7744,7745,182,7766,7809,7767,7811,7776,7923,7812,7813,7777,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,372,209,210,211,212,213,214,7786,216,217,218,219,220,221,374,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,373,241,242,243,244,245,246,7787,248,249,250,251,252,253,375,255],
|
|
|
|
"iso-8859-15":[128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,8364,165,352,167,353,169,170,171,172,173,174,175,176,177,178,179,381,181,182,183,382,185,186,187,338,339,376,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255],
|
|
|
|
"iso-8859-16":[128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,260,261,321,8364,8222,352,167,353,169,536,171,377,173,378,379,176,177,268,322,381,8221,182,183,382,269,537,187,338,339,376,380,192,193,194,258,196,262,198,199,200,201,202,203,204,205,206,207,272,323,210,211,212,336,214,346,368,217,218,219,220,280,538,223,224,225,226,259,228,263,230,231,232,233,234,235,236,237,238,239,273,324,242,243,244,337,246,347,369,249,250,251,252,281,539,255],
|
|
|
|
"koi8-r":[9472,9474,9484,9488,9492,9496,9500,9508,9516,9524,9532,9600,9604,9608,9612,9616,9617,9618,9619,8992,9632,8729,8730,8776,8804,8805,160,8993,176,178,183,247,9552,9553,9554,1105,9555,9556,9557,9558,9559,9560,9561,9562,9563,9564,9565,9566,9567,9568,9569,1025,9570,9571,9572,9573,9574,9575,9576,9577,9578,9579,9580,169,1102,1072,1073,1094,1076,1077,1092,1075,1093,1080,1081,1082,1083,1084,1085,1086,1087,1103,1088,1089,1090,1091,1078,1074,1100,1099,1079,1096,1101,1097,1095,1098,1070,1040,1041,1062,1044,1045,1060,1043,1061,1048,1049,1050,1051,1052,1053,1054,1055,1071,1056,1057,1058,1059,1046,1042,1068,1067,1047,1064,1069,1065,1063,1066],
|
|
|
|
"koi8-u":[9472,9474,9484,9488,9492,9496,9500,9508,9516,9524,9532,9600,9604,9608,9612,9616,9617,9618,9619,8992,9632,8729,8730,8776,8804,8805,160,8993,176,178,183,247,9552,9553,9554,1105,1108,9556,1110,1111,9559,9560,9561,9562,9563,1169,9565,9566,9567,9568,9569,1025,1028,9571,1030,1031,9574,9575,9576,9577,9578,1168,9580,169,1102,1072,1073,1094,1076,1077,1092,1075,1093,1080,1081,1082,1083,1084,1085,1086,1087,1103,1088,1089,1090,1091,1078,1074,1100,1099,1079,1096,1101,1097,1095,1098,1070,1040,1041,1062,1044,1045,1060,1043,1061,1048,1049,1050,1051,1052,1053,1054,1055,1071,1056,1057,1058,1059,1046,1042,1068,1067,1047,1064,1069,1065,1063,1066],
|
|
|
|
"macintosh":[196,197,199,201,209,214,220,225,224,226,228,227,229,231,233,232,234,235,237,236,238,239,241,243,242,244,246,245,250,249,251,252,8224,176,162,163,167,8226,182,223,174,169,8482,180,168,8800,198,216,8734,177,8804,8805,165,181,8706,8721,8719,960,8747,170,186,937,230,248,191,161,172,8730,402,8776,8710,171,187,8230,160,192,195,213,338,339,8211,8212,8220,8221,8216,8217,247,9674,255,376,8260,8364,8249,8250,64257,64258,8225,183,8218,8222,8240,194,202,193,203,200,205,206,207,204,211,212,63743,210,218,219,217,305,710,732,175,728,729,730,184,733,731,711],
|
|
|
|
"windows-874":[8364,129,130,131,132,8230,134,135,136,137,138,139,140,141,142,143,144,8216,8217,8220,8221,8226,8211,8212,152,153,154,155,156,157,158,159,160,3585,3586,3587,3588,3589,3590,3591,3592,3593,3594,3595,3596,3597,3598,3599,3600,3601,3602,3603,3604,3605,3606,3607,3608,3609,3610,3611,3612,3613,3614,3615,3616,3617,3618,3619,3620,3621,3622,3623,3624,3625,3626,3627,3628,3629,3630,3631,3632,3633,3634,3635,3636,3637,3638,3639,3640,3641,3642,null,null,null,null,3647,3648,3649,3650,3651,3652,3653,3654,3655,3656,3657,3658,3659,3660,3661,3662,3663,3664,3665,3666,3667,3668,3669,3670,3671,3672,3673,3674,3675,null,null,null,null],
|
|
|
|
"windows-1250":[8364,129,8218,131,8222,8230,8224,8225,136,8240,352,8249,346,356,381,377,144,8216,8217,8220,8221,8226,8211,8212,152,8482,353,8250,347,357,382,378,160,711,728,321,164,260,166,167,168,169,350,171,172,173,174,379,176,177,731,322,180,181,182,183,184,261,351,187,317,733,318,380,340,193,194,258,196,313,262,199,268,201,280,203,282,205,206,270,272,323,327,211,212,336,214,215,344,366,218,368,220,221,354,223,341,225,226,259,228,314,263,231,269,233,281,235,283,237,238,271,273,324,328,243,244,337,246,247,345,367,250,369,252,253,355,729],
|
|
|
|
"windows-1251":[1026,1027,8218,1107,8222,8230,8224,8225,8364,8240,1033,8249,1034,1036,1035,1039,1106,8216,8217,8220,8221,8226,8211,8212,152,8482,1113,8250,1114,1116,1115,1119,160,1038,1118,1032,164,1168,166,167,1025,169,1028,171,172,173,174,1031,176,177,1030,1110,1169,181,182,183,1105,8470,1108,187,1112,1029,1109,1111,1040,1041,1042,1043,1044,1045,1046,1047,1048,1049,1050,1051,1052,1053,1054,1055,1056,1057,1058,1059,1060,1061,1062,1063,1064,1065,1066,1067,1068,1069,1070,1071,1072,1073,1074,1075,1076,1077,1078,1079,1080,1081,1082,1083,1084,1085,1086,1087,1088,1089,1090,1091,1092,1093,1094,1095,1096,1097,1098,1099,1100,1101,1102,1103],
|
|
|
|
"windows-1252":[8364,129,8218,402,8222,8230,8224,8225,710,8240,352,8249,338,141,381,143,144,8216,8217,8220,8221,8226,8211,8212,732,8482,353,8250,339,157,382,376,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255],
|
|
|
|
"windows-1253":[8364,129,8218,402,8222,8230,8224,8225,136,8240,138,8249,140,141,142,143,144,8216,8217,8220,8221,8226,8211,8212,152,8482,154,8250,156,157,158,159,160,901,902,163,164,165,166,167,168,169,null,171,172,173,174,8213,176,177,178,179,900,181,182,183,904,905,906,187,908,189,910,911,912,913,914,915,916,917,918,919,920,921,922,923,924,925,926,927,928,929,null,931,932,933,934,935,936,937,938,939,940,941,942,943,944,945,946,947,948,949,950,951,952,953,954,955,956,957,958,959,960,961,962,963,964,965,966,967,968,969,970,971,972,973,974,null],
|
|
|
|
"windows-1254":[8364,129,8218,402,8222,8230,8224,8225,710,8240,352,8249,338,141,142,143,144,8216,8217,8220,8221,8226,8211,8212,732,8482,353,8250,339,157,158,376,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,286,209,210,211,212,213,214,215,216,217,218,219,220,304,350,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,287,241,242,243,244,245,246,247,248,249,250,251,252,305,351,255],
|
|
|
|
"windows-1255":[8364,129,8218,402,8222,8230,8224,8225,710,8240,138,8249,140,141,142,143,144,8216,8217,8220,8221,8226,8211,8212,732,8482,154,8250,156,157,158,159,160,161,162,163,8362,165,166,167,168,169,215,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,247,187,188,189,190,191,1456,1457,1458,1459,1460,1461,1462,1463,1464,1465,null,1467,1468,1469,1470,1471,1472,1473,1474,1475,1520,1521,1522,1523,1524,null,null,null,null,null,null,null,1488,1489,1490,1491,1492,1493,1494,1495,1496,1497,1498,1499,1500,1501,1502,1503,1504,1505,1506,1507,1508,1509,1510,1511,1512,1513,1514,null,null,8206,8207,null],
|
|
|
|
"windows-1256":[8364,1662,8218,402,8222,8230,8224,8225,710,8240,1657,8249,338,1670,1688,1672,1711,8216,8217,8220,8221,8226,8211,8212,1705,8482,1681,8250,339,8204,8205,1722,160,1548,162,163,164,165,166,167,168,169,1726,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,1563,187,188,189,190,1567,1729,1569,1570,1571,1572,1573,1574,1575,1576,1577,1578,1579,1580,1581,1582,1583,1584,1585,1586,1587,1588,1589,1590,215,1591,1592,1593,1594,1600,1601,1602,1603,224,1604,226,1605,1606,1607,1608,231,232,233,234,235,1609,1610,238,239,1611,1612,1613,1614,244,1615,1616,247,1617,249,1618,251,252,8206,8207,1746],
|
|
|
|
"windows-1257":[8364,129,8218,131,8222,8230,8224,8225,136,8240,138,8249,140,168,711,184,144,8216,8217,8220,8221,8226,8211,8212,152,8482,154,8250,156,175,731,159,160,null,162,163,164,null,166,167,216,169,342,171,172,173,174,198,176,177,178,179,180,181,182,183,248,185,343,187,188,189,190,230,260,302,256,262,196,197,280,274,268,201,377,278,290,310,298,315,352,323,325,211,332,213,214,215,370,321,346,362,220,379,381,223,261,303,257,263,228,229,281,275,269,233,378,279,291,311,299,316,353,324,326,243,333,245,246,247,371,322,347,363,252,380,382,729],
|
|
|
|
"windows-1258":[8364,129,8218,402,8222,8230,8224,8225,710,8240,138,8249,338,141,142,143,144,8216,8217,8220,8221,8226,8211,8212,732,8482,154,8250,339,157,158,376,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,258,196,197,198,199,200,201,202,203,768,205,206,207,272,209,777,211,212,416,214,215,216,217,218,219,220,431,771,223,224,225,226,259,228,229,230,231,232,233,234,235,769,237,238,239,273,241,803,243,244,417,246,247,248,249,250,251,252,432,8363,255],
|
|
|
|
"x-mac-cyrillic":[1040,1041,1042,1043,1044,1045,1046,1047,1048,1049,1050,1051,1052,1053,1054,1055,1056,1057,1058,1059,1060,1061,1062,1063,1064,1065,1066,1067,1068,1069,1070,1071,8224,176,1168,163,167,8226,182,1030,174,169,8482,1026,1106,8800,1027,1107,8734,177,8804,8805,1110,181,1169,1032,1028,1108,1031,1111,1033,1113,1034,1114,1112,1029,172,8730,402,8776,8710,171,187,8230,160,1035,1115,1036,1116,1109,8211,8212,8220,8221,8216,8217,247,8222,1038,1118,1039,1119,8470,1025,1105,1103,1072,1073,1074,1075,1076,1077,1078,1079,1080,1081,1082,1083,1084,1085,1086,1087,1088,1089,1090,1091,1092,1093,1094,1095,1096,1097,1098,1099,1100,1101,1102,8364]
|
|
|
|
}
|
|
|
|
;
|
|
|
|
define("encoding-indexes", function(){});
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* Shim implementation of the TextEncoder, TextDecoder spec:
|
|
|
|
* http://encoding.spec.whatwg.org/#interface-textencoder
|
|
|
|
*
|
|
|
|
* http://code.google.com/p/stringencoding/source/browse/encoding.js
|
|
|
|
* 09b44d71759d on Sep 19, 2013
|
|
|
|
* Used under Apache License 2.0 - http://code.google.com/p/stringencoding/
|
|
|
|
*/
|
|
|
|
(function(global) {
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// Utilities
|
|
|
|
//
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {number} a The number to test.
|
|
|
|
* @param {number} min The minimum value in the range, inclusive.
|
|
|
|
* @param {number} max The maximum value in the range, inclusive.
|
|
|
|
* @return {boolean} True if a >= min and a <= max.
|
|
|
|
*/
|
|
|
|
function inRange(a, min, max) {
|
|
|
|
return min <= a && a <= max;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {number} n The numerator.
|
|
|
|
* @param {number} d The denominator.
|
|
|
|
* @return {number} The result of the integer division of n by d.
|
|
|
|
*/
|
|
|
|
function div(n, d) {
|
|
|
|
return Math.floor(n / d);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// Implementation of Encoding specification
|
|
|
|
// http://dvcs.w3.org/hg/encoding/raw-file/tip/Overview.html
|
|
|
|
//
|
|
|
|
|
|
|
|
//
|
|
|
|
// 3. Terminology
|
|
|
|
//
|
|
|
|
|
|
|
|
//
|
|
|
|
// 4. Encodings
|
|
|
|
//
|
|
|
|
|
|
|
|
/** @const */ var EOF_byte = -1;
|
|
|
|
/** @const */ var EOF_code_point = -1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @constructor
|
|
|
|
* @param {Uint8Array} bytes Array of bytes that provide the stream.
|
|
|
|
*/
|
|
|
|
function ByteInputStream(bytes) {
|
|
|
|
/** @type {number} */
|
|
|
|
var pos = 0;
|
|
|
|
|
|
|
|
/** @return {number} Get the next byte from the stream. */
|
|
|
|
this.get = function() {
|
|
|
|
return (pos >= bytes.length) ? EOF_byte : Number(bytes[pos]);
|
|
|
|
};
|
|
|
|
|
|
|
|
/** @param {number} n Number (positive or negative) by which to
|
|
|
|
* offset the byte pointer. */
|
|
|
|
this.offset = function(n) {
|
|
|
|
pos += n;
|
|
|
|
if (pos < 0) {
|
|
|
|
throw new Error('Seeking past start of the buffer');
|
|
|
|
}
|
|
|
|
if (pos > bytes.length) {
|
|
|
|
throw new Error('Seeking past EOF');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Array.<number>} test Array of bytes to compare against.
|
|
|
|
* @return {boolean} True if the start of the stream matches the test
|
|
|
|
* bytes.
|
|
|
|
*/
|
|
|
|
this.match = function(test) {
|
|
|
|
if (test.length > pos + bytes.length) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
var i;
|
|
|
|
for (i = 0; i < test.length; i += 1) {
|
|
|
|
if (Number(bytes[pos + i]) !== test[i]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @constructor
|
|
|
|
* @param {Array.<number>} bytes The array to write bytes into.
|
|
|
|
*/
|
|
|
|
function ByteOutputStream(bytes) {
|
|
|
|
/** @type {number} */
|
|
|
|
var pos = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {...number} var_args The byte or bytes to emit into the stream.
|
|
|
|
* @return {number} The last byte emitted.
|
|
|
|
*/
|
|
|
|
this.emit = function(var_args) {
|
|
|
|
/** @type {number} */
|
|
|
|
var last = EOF_byte;
|
|
|
|
var i;
|
|
|
|
for (i = 0; i < arguments.length; ++i) {
|
|
|
|
last = Number(arguments[i]);
|
|
|
|
bytes[pos++] = last;
|
|
|
|
}
|
|
|
|
return last;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @constructor
|
|
|
|
* @param {string} string The source of code units for the stream.
|
|
|
|
*/
|
|
|
|
function CodePointInputStream(string) {
|
|
|
|
/**
|
|
|
|
* @param {string} string Input string of UTF-16 code units.
|
|
|
|
* @return {Array.<number>} Code points.
|
|
|
|
*/
|
|
|
|
function stringToCodePoints(string) {
|
|
|
|
/** @type {Array.<number>} */
|
|
|
|
var cps = [];
|
|
|
|
// Based on http://www.w3.org/TR/WebIDL/#idl-DOMString
|
|
|
|
var i = 0, n = string.length;
|
|
|
|
while (i < string.length) {
|
|
|
|
var c = string.charCodeAt(i);
|
|
|
|
if (!inRange(c, 0xD800, 0xDFFF)) {
|
|
|
|
cps.push(c);
|
|
|
|
} else if (inRange(c, 0xDC00, 0xDFFF)) {
|
|
|
|
cps.push(0xFFFD);
|
|
|
|
} else { // (inRange(cu, 0xD800, 0xDBFF))
|
|
|
|
if (i === n - 1) {
|
|
|
|
cps.push(0xFFFD);
|
|
|
|
} else {
|
|
|
|
var d = string.charCodeAt(i + 1);
|
|
|
|
if (inRange(d, 0xDC00, 0xDFFF)) {
|
|
|
|
var a = c & 0x3FF;
|
|
|
|
var b = d & 0x3FF;
|
|
|
|
i += 1;
|
|
|
|
cps.push(0x10000 + (a << 10) + b);
|
|
|
|
} else {
|
|
|
|
cps.push(0xFFFD);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
return cps;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @type {number} */
|
|
|
|
var pos = 0;
|
|
|
|
/** @type {Array.<number>} */
|
|
|
|
var cps = stringToCodePoints(string);
|
|
|
|
|
|
|
|
/** @param {number} n The number of bytes (positive or negative)
|
|
|
|
* to advance the code point pointer by.*/
|
|
|
|
this.offset = function(n) {
|
|
|
|
pos += n;
|
|
|
|
if (pos < 0) {
|
|
|
|
throw new Error('Seeking past start of the buffer');
|
|
|
|
}
|
|
|
|
if (pos > cps.length) {
|
|
|
|
throw new Error('Seeking past EOF');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/** @return {number} Get the next code point from the stream. */
|
|
|
|
this.get = function() {
|
|
|
|
if (pos >= cps.length) {
|
|
|
|
return EOF_code_point;
|
|
|
|
}
|
|
|
|
return cps[pos];
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
function CodePointOutputStream() {
|
|
|
|
/** @type {string} */
|
|
|
|
var string = '';
|
|
|
|
|
|
|
|
/** @return {string} The accumulated string. */
|
|
|
|
this.string = function() {
|
|
|
|
return string;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** @param {number} c The code point to encode into the stream. */
|
|
|
|
this.emit = function(c) {
|
|
|
|
if (c <= 0xFFFF) {
|
|
|
|
string += String.fromCharCode(c);
|
|
|
|
} else {
|
|
|
|
c -= 0x10000;
|
|
|
|
string += String.fromCharCode(0xD800 + ((c >> 10) & 0x3ff));
|
|
|
|
string += String.fromCharCode(0xDC00 + (c & 0x3ff));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @constructor
|
|
|
|
* @param {string} message Description of the error.
|
|
|
|
*/
|
|
|
|
function EncodingError(message) {
|
|
|
|
this.name = 'EncodingError';
|
|
|
|
this.message = message;
|
|
|
|
this.code = 0;
|
|
|
|
}
|
|
|
|
EncodingError.prototype = Error.prototype;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {boolean} fatal If true, decoding errors raise an exception.
|
|
|
|
* @param {number=} opt_code_point Override the standard fallback code point.
|
|
|
|
* @return {number} The code point to insert on a decoding error.
|
|
|
|
*/
|
|
|
|
function decoderError(fatal, opt_code_point) {
|
|
|
|
if (fatal) {
|
|
|
|
throw new EncodingError('Decoder error');
|
|
|
|
}
|
|
|
|
return opt_code_point || 0xFFFD;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {number} code_point The code point that could not be encoded.
|
|
|
|
*/
|
|
|
|
function encoderError(code_point) {
|
|
|
|
throw new EncodingError('The code point ' + code_point +
|
|
|
|
' could not be encoded.');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} label The encoding label.
|
|
|
|
* @return {?{name:string,labels:Array.<string>}}
|
|
|
|
*/
|
|
|
|
function getEncoding(label) {
|
|
|
|
label = String(label).trim().toLowerCase();
|
|
|
|
if (Object.prototype.hasOwnProperty.call(label_to_encoding, label)) {
|
|
|
|
return label_to_encoding[label];
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @type {Array.<{encodings: Array.<{name:string,labels:Array.<string>}>,
|
|
|
|
* heading: string}>} */
|
|
|
|
var encodings = [
|
|
|
|
{
|
|
|
|
"encodings": [
|
|
|
|
{
|
|
|
|
"labels": [
|
|
|
|
"unicode-1-1-utf-8",
|
|
|
|
"utf-8",
|
|
|
|
"utf8"
|
|
|
|
],
|
|
|
|
"name": "utf-8"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"heading": "The Encoding"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"encodings": [
|
|
|
|
{
|
|
|
|
"labels": [
|
|
|
|
"866",
|
|
|
|
"cp866",
|
|
|
|
"csibm866",
|
|
|
|
"ibm866"
|
|
|
|
],
|
|
|
|
"name": "ibm866"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"labels": [
|
|
|
|
"csisolatin2",
|
|
|
|
"iso-8859-2",
|
|
|
|
"iso-ir-101",
|
|
|
|
"iso8859-2",
|
|
|
|
"iso88592",
|
|
|
|
"iso_8859-2",
|
|
|
|
"iso_8859-2:1987",
|
|
|
|
"l2",
|
|
|
|
"latin2"
|
|
|
|
],
|
|
|
|
"name": "iso-8859-2"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"labels": [
|
|
|
|
"csisolatin3",
|
|
|
|
"iso-8859-3",
|
|
|
|
"iso-ir-109",
|
|
|
|
"iso8859-3",
|
|
|
|
"iso88593",
|
|
|
|
"iso_8859-3",
|
|
|
|
"iso_8859-3:1988",
|
|
|
|
"l3",
|
|
|
|
"latin3"
|
|
|
|
],
|
|
|
|
"name": "iso-8859-3"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"labels": [
|
|
|
|
"csisolatin4",
|
|
|
|
"iso-8859-4",
|
|
|
|
"iso-ir-110",
|
|
|
|
"iso8859-4",
|
|
|
|
"iso88594",
|
|
|
|
"iso_8859-4",
|
|
|
|
"iso_8859-4:1988",
|
|
|
|
"l4",
|
|
|
|
"latin4"
|
|
|
|
],
|
|
|
|
"name": "iso-8859-4"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"labels": [
|
|
|
|
"csisolatincyrillic",
|
|
|
|
"cyrillic",
|
|
|
|
"iso-8859-5",
|
|
|
|
"iso-ir-144",
|
|
|
|
"iso8859-5",
|
|
|
|
"iso88595",
|
|
|
|
"iso_8859-5",
|
|
|
|
"iso_8859-5:1988"
|
|
|
|
],
|
|
|
|
"name": "iso-8859-5"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"labels": [
|
|
|
|
"arabic",
|
|
|
|
"asmo-708",
|
|
|
|
"csiso88596e",
|
|
|
|
"csiso88596i",
|
|
|
|
"csisolatinarabic",
|
|
|
|
"ecma-114",
|
|
|
|
"iso-8859-6",
|
|
|
|
"iso-8859-6-e",
|
|
|
|
"iso-8859-6-i",
|
|
|
|
"iso-ir-127",
|
|
|
|
"iso8859-6",
|
|
|
|
"iso88596",
|
|
|
|
"iso_8859-6",
|
|
|
|
"iso_8859-6:1987"
|
|
|
|
],
|
|
|
|
"name": "iso-8859-6"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"labels": [
|
|
|
|
"csisolatingreek",
|
|
|
|
"ecma-118",
|
|
|
|
"elot_928",
|
|
|
|
"greek",
|
|
|
|
"greek8",
|
|
|
|
"iso-8859-7",
|
|
|
|
"iso-ir-126",
|
|
|
|
"iso8859-7",
|
|
|
|
"iso88597",
|
|
|
|
"iso_8859-7",
|
|
|
|
"iso_8859-7:1987",
|
|
|
|
"sun_eu_greek"
|
|
|
|
],
|
|
|
|
"name": "iso-8859-7"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"labels": [
|
|
|
|
"csiso88598e",
|
|
|
|
"csisolatinhebrew",
|
|
|
|
"hebrew",
|
|
|
|
"iso-8859-8",
|
|
|
|
"iso-8859-8-e",
|
|
|
|
"iso-ir-138",
|
|
|
|
"iso8859-8",
|
|
|
|
"iso88598",
|
|
|
|
"iso_8859-8",
|
|
|
|
"iso_8859-8:1988",
|
|
|
|
"visual"
|
|
|
|
],
|
|
|
|
"name": "iso-8859-8"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"labels": [
|
|
|
|
"csiso88598i",
|
|
|
|
"iso-8859-8-i",
|
|
|
|
"logical"
|
|
|
|
],
|
|
|
|
"name": "iso-8859-8-i"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"labels": [
|
|
|
|
"csisolatin6",
|
|
|
|
"iso-8859-10",
|
|
|
|
"iso-ir-157",
|
|
|
|
"iso8859-10",
|
|
|
|
"iso885910",
|
|
|
|
"l6",
|
|
|
|
"latin6"
|
|
|
|
],
|
|
|
|
"name": "iso-8859-10"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"labels": [
|
|
|
|
"iso-8859-13",
|
|
|
|
"iso8859-13",
|
|
|
|
"iso885913"
|
|
|
|
],
|
|
|
|
"name": "iso-8859-13"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"labels": [
|
|
|
|
"iso-8859-14",
|
|
|
|
"iso8859-14",
|
|
|
|
"iso885914"
|
|
|
|
],
|
|
|
|
"name": "iso-8859-14"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"labels": [
|
|
|
|
"csisolatin9",
|
|
|
|
"iso-8859-15",
|
|
|
|
"iso8859-15",
|
|
|
|
"iso885915",
|
|
|
|
"iso_8859-15",
|
|
|
|
"l9"
|
|
|
|
],
|
|
|
|
"name": "iso-8859-15"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"labels": [
|
|
|
|
"iso-8859-16"
|
|
|
|
],
|
|
|
|
"name": "iso-8859-16"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"labels": [
|
|
|
|
"cskoi8r",
|
|
|
|
"koi",
|
|
|
|
"koi8",
|
|
|
|
"koi8-r",
|
|
|
|
"koi8_r"
|
|
|
|
],
|
|
|
|
"name": "koi8-r"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"labels": [
|
|
|
|
"koi8-u"
|
|
|
|
],
|
|
|
|
"name": "koi8-u"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"labels": [
|
|
|
|
"csmacintosh",
|
|
|
|
"mac",
|
|
|
|
"macintosh",
|
|
|
|
"x-mac-roman"
|
|
|
|
],
|
|
|
|
"name": "macintosh"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"labels": [
|
|
|
|
"dos-874",
|
|
|
|
"iso-8859-11",
|
|
|
|
"iso8859-11",
|
|
|
|
"iso885911",
|
|
|
|
"tis-620",
|
|
|
|
"windows-874"
|
|
|
|
],
|
|
|
|
"name": "windows-874"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"labels": [
|
|
|
|
"cp1250",
|
|
|
|
"windows-1250",
|
|
|
|
"x-cp1250"
|
|
|
|
],
|
|
|
|
"name": "windows-1250"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"labels": [
|
|
|
|
"cp1251",
|
|
|
|
"windows-1251",
|
|
|
|
"x-cp1251"
|
|
|
|
],
|
|
|
|
"name": "windows-1251"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"labels": [
|
|
|
|
"ansi_x3.4-1968",
|
|
|
|
"ascii",
|
|
|
|
"cp1252",
|
|
|
|
"cp819",
|
|
|
|
"csisolatin1",
|
|
|
|
"ibm819",
|
|
|
|
"iso-8859-1",
|
|
|
|
"iso-ir-100",
|
|
|
|
"iso8859-1",
|
|
|
|
"iso88591",
|
|
|
|
"iso_8859-1",
|
|
|
|
"iso_8859-1:1987",
|
|
|
|
"l1",
|
|
|
|
"latin1",
|
|
|
|
"us-ascii",
|
|
|
|
"windows-1252",
|
|
|
|
"x-cp1252"
|
|
|
|
],
|
|
|
|
"name": "windows-1252"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"labels": [
|
|
|
|
"cp1253",
|
|
|
|
"windows-1253",
|
|
|
|
"x-cp1253"
|
|
|
|
],
|
|
|
|
"name": "windows-1253"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"labels": [
|
|
|
|
"cp1254",
|
|
|
|
"csisolatin5",
|
|
|
|
"iso-8859-9",
|
|
|
|
"iso-ir-148",
|
|
|
|
"iso8859-9",
|
|
|
|
"iso88599",
|
|
|
|
"iso_8859-9",
|
|
|
|
"iso_8859-9:1989",
|
|
|
|
"l5",
|
|
|
|
"latin5",
|
|
|
|
"windows-1254",
|
|
|
|
"x-cp1254"
|
|
|
|
],
|
|
|
|
"name": "windows-1254"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"labels": [
|
|
|
|
"cp1255",
|
|
|
|
"windows-1255",
|
|
|
|
"x-cp1255"
|
|
|
|
],
|
|
|
|
"name": "windows-1255"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"labels": [
|
|
|
|
"cp1256",
|
|
|
|
"windows-1256",
|
|
|
|
"x-cp1256"
|
|
|
|
],
|
|
|
|
"name": "windows-1256"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"labels": [
|
|
|
|
"cp1257",
|
|
|
|
"windows-1257",
|
|
|
|
"x-cp1257"
|
|
|
|
],
|
|
|
|
"name": "windows-1257"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"labels": [
|
|
|
|
"cp1258",
|
|
|
|
"windows-1258",
|
|
|
|
"x-cp1258"
|
|
|
|
],
|
|
|
|
"name": "windows-1258"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"labels": [
|
|
|
|
"x-mac-cyrillic",
|
|
|
|
"x-mac-ukrainian"
|
|
|
|
],
|
|
|
|
"name": "x-mac-cyrillic"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"heading": "Legacy single-byte encodings"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"encodings": [
|
|
|
|
{
|
|
|
|
"labels": [
|
|
|
|
"chinese",
|
|
|
|
"csgb2312",
|
|
|
|
"csiso58gb231280",
|
|
|
|
"gb2312",
|
|
|
|
"gb_2312",
|
|
|
|
"gb_2312-80",
|
|
|
|
"gbk",
|
|
|
|
"iso-ir-58",
|
|
|
|
"x-gbk"
|
|
|
|
],
|
|
|
|
"name": "gbk"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"labels": [
|
|
|
|
"gb18030"
|
|
|
|
],
|
|
|
|
"name": "gb18030"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"labels": [
|
|
|
|
"hz-gb-2312"
|
|
|
|
],
|
|
|
|
"name": "hz-gb-2312"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"heading": "Legacy multi-byte Chinese (simplified) encodings"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"encodings": [
|
|
|
|
{
|
|
|
|
"labels": [
|
|
|
|
"big5",
|
|
|
|
"big5-hkscs",
|
|
|
|
"cn-big5",
|
|
|
|
"csbig5",
|
|
|
|
"x-x-big5"
|
|
|
|
],
|
|
|
|
"name": "big5"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"heading": "Legacy multi-byte Chinese (traditional) encodings"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"encodings": [
|
|
|
|
{
|
|
|
|
"labels": [
|
|
|
|
"cseucpkdfmtjapanese",
|
|
|
|
"euc-jp",
|
|
|
|
"x-euc-jp"
|
|
|
|
],
|
|
|
|
"name": "euc-jp"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"labels": [
|
|
|
|
"csiso2022jp",
|
|
|
|
"iso-2022-jp"
|
|
|
|
],
|
|
|
|
"name": "iso-2022-jp"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"labels": [
|
|
|
|
"csshiftjis",
|
|
|
|
"ms_kanji",
|
|
|
|
"shift-jis",
|
|
|
|
"shift_jis",
|
|
|
|
"sjis",
|
|
|
|
"windows-31j",
|
|
|
|
"x-sjis"
|
|
|
|
],
|
|
|
|
"name": "shift_jis"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"heading": "Legacy multi-byte Japanese encodings"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"encodings": [
|
|
|
|
{
|
|
|
|
"labels": [
|
|
|
|
"cseuckr",
|
|
|
|
"csksc56011987",
|
|
|
|
"euc-kr",
|
|
|
|
"iso-ir-149",
|
|
|
|
"korean",
|
|
|
|
"ks_c_5601-1987",
|
|
|
|
"ks_c_5601-1989",
|
|
|
|
"ksc5601",
|
|
|
|
"ksc_5601",
|
|
|
|
"windows-949"
|
|
|
|
],
|
|
|
|
"name": "euc-kr"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"heading": "Legacy multi-byte Korean encodings"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"encodings": [
|
|
|
|
{
|
|
|
|
"labels": [
|
|
|
|
"csiso2022kr",
|
|
|
|
"iso-2022-kr",
|
|
|
|
"iso-2022-cn",
|
|
|
|
"iso-2022-cn-ext"
|
|
|
|
],
|
|
|
|
"name": "replacement"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"labels": [
|
|
|
|
"utf-16be"
|
|
|
|
],
|
|
|
|
"name": "utf-16be"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"labels": [
|
|
|
|
"utf-16",
|
|
|
|
"utf-16le"
|
|
|
|
],
|
|
|
|
"name": "utf-16le"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"labels": [
|
|
|
|
"x-user-defined"
|
|
|
|
],
|
|
|
|
"name": "x-user-defined"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"heading": "Legacy miscellaneous encodings"
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
var name_to_encoding = {};
|
|
|
|
var label_to_encoding = {};
|
|
|
|
encodings.forEach(function(category) {
|
|
|
|
category.encodings.forEach(function(encoding) {
|
|
|
|
name_to_encoding[encoding.name] = encoding;
|
|
|
|
encoding.labels.forEach(function(label) {
|
|
|
|
label_to_encoding[label] = encoding;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
//
|
|
|
|
// 5. Indexes
|
|
|
|
//
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {number} pointer The |pointer| to search for.
|
|
|
|
* @param {Array.<?number>} index The |index| to search within.
|
|
|
|
* @return {?number} The code point corresponding to |pointer| in |index|,
|
|
|
|
* or null if |code point| is not in |index|.
|
|
|
|
*/
|
|
|
|
function indexCodePointFor(pointer, index) {
|
|
|
|
return (index || [])[pointer] || null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {number} code_point The |code point| to search for.
|
|
|
|
* @param {Array.<?number>} index The |index| to search within.
|
|
|
|
* @return {?number} The first pointer corresponding to |code point| in
|
|
|
|
* |index|, or null if |code point| is not in |index|.
|
|
|
|
*/
|
|
|
|
function indexPointerFor(code_point, index) {
|
|
|
|
var pointer = index.indexOf(code_point);
|
|
|
|
return pointer === -1 ? null : pointer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} name Name of the index.
|
|
|
|
* @return {(Array.<number>|Array.<Array.<number>>)}
|
|
|
|
* */
|
|
|
|
function index(name) {
|
|
|
|
if (!('encoding-indexes' in global))
|
|
|
|
throw new Error("Indexes missing. Did you forget to include encoding-indexes.js?");
|
|
|
|
return global['encoding-indexes'][name];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {number} pointer The |pointer| to search for in the gb18030 index.
|
|
|
|
* @return {?number} The code point corresponding to |pointer| in |index|,
|
|
|
|
* or null if |code point| is not in the gb18030 index.
|
|
|
|
*/
|
|
|
|
function indexGB18030CodePointFor(pointer) {
|
|
|
|
if ((pointer > 39419 && pointer < 189000) || (pointer > 1237575)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
var /** @type {number} */ offset = 0,
|
|
|
|
/** @type {number} */ code_point_offset = 0,
|
|
|
|
/** @type {Array.<Array.<number>>} */ idx = index('gb18030');
|
|
|
|
var i;
|
|
|
|
for (i = 0; i < idx.length; ++i) {
|
|
|
|
var entry = idx[i];
|
|
|
|
if (entry[0] <= pointer) {
|
|
|
|
offset = entry[0];
|
|
|
|
code_point_offset = entry[1];
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return code_point_offset + pointer - offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {number} code_point The |code point| to locate in the gb18030 index.
|
|
|
|
* @return {number} The first pointer corresponding to |code point| in the
|
|
|
|
* gb18030 index.
|
|
|
|
*/
|
|
|
|
function indexGB18030PointerFor(code_point) {
|
|
|
|
var /** @type {number} */ offset = 0,
|
|
|
|
/** @type {number} */ pointer_offset = 0,
|
|
|
|
/** @type {Array.<Array.<number>>} */ idx = index('gb18030');
|
|
|
|
var i;
|
|
|
|
for (i = 0; i < idx.length; ++i) {
|
|
|
|
var entry = idx[i];
|
|
|
|
if (entry[1] <= code_point) {
|
|
|
|
offset = entry[1];
|
|
|
|
pointer_offset = entry[0];
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pointer_offset + code_point - offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// 7. The encoding
|
|
|
|
//
|
|
|
|
|
|
|
|
// 7.1 utf-8
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @constructor
|
|
|
|
* @param {{fatal: boolean}} options
|
|
|
|
*/
|
|
|
|
function UTF8Decoder(options) {
|
|
|
|
var fatal = options.fatal;
|
|
|
|
var /** @type {number} */ utf8_code_point = 0,
|
|
|
|
/** @type {number} */ utf8_bytes_needed = 0,
|
|
|
|
/** @type {number} */ utf8_bytes_seen = 0,
|
|
|
|
/** @type {number} */ utf8_lower_boundary = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {ByteInputStream} byte_pointer The byte stream to decode.
|
|
|
|
* @return {?number} The next code point decoded, or null if not enough
|
|
|
|
* data exists in the input stream to decode a complete code point.
|
|
|
|
*/
|
|
|
|
this.decode = function(byte_pointer) {
|
|
|
|
var bite = byte_pointer.get();
|
|
|
|
if (bite === EOF_byte) {
|
|
|
|
if (utf8_bytes_needed !== 0) {
|
|
|
|
return decoderError(fatal);
|
|
|
|
}
|
|
|
|
return EOF_code_point;
|
|
|
|
}
|
|
|
|
byte_pointer.offset(1);
|
|
|
|
|
|
|
|
if (utf8_bytes_needed === 0) {
|
|
|
|
if (inRange(bite, 0x00, 0x7F)) {
|
|
|
|
return bite;
|
|
|
|
}
|
|
|
|
if (inRange(bite, 0xC2, 0xDF)) {
|
|
|
|
utf8_bytes_needed = 1;
|
|
|
|
utf8_lower_boundary = 0x80;
|
|
|
|
utf8_code_point = bite - 0xC0;
|
|
|
|
} else if (inRange(bite, 0xE0, 0xEF)) {
|
|
|
|
utf8_bytes_needed = 2;
|
|
|
|
utf8_lower_boundary = 0x800;
|
|
|
|
utf8_code_point = bite - 0xE0;
|
|
|
|
} else if (inRange(bite, 0xF0, 0xF4)) {
|
|
|
|
utf8_bytes_needed = 3;
|
|
|
|
utf8_lower_boundary = 0x10000;
|
|
|
|
utf8_code_point = bite - 0xF0;
|
|
|
|
} else {
|
|
|
|
return decoderError(fatal);
|
|
|
|
}
|
|
|
|
utf8_code_point = utf8_code_point * Math.pow(64, utf8_bytes_needed);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (!inRange(bite, 0x80, 0xBF)) {
|
|
|
|
utf8_code_point = 0;
|
|
|
|
utf8_bytes_needed = 0;
|
|
|
|
utf8_bytes_seen = 0;
|
|
|
|
utf8_lower_boundary = 0;
|
|
|
|
byte_pointer.offset(-1);
|
|
|
|
return decoderError(fatal);
|
|
|
|
}
|
|
|
|
utf8_bytes_seen += 1;
|
|
|
|
utf8_code_point = utf8_code_point + (bite - 0x80) *
|
|
|
|
Math.pow(64, utf8_bytes_needed - utf8_bytes_seen);
|
|
|
|
if (utf8_bytes_seen !== utf8_bytes_needed) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
var code_point = utf8_code_point;
|
|
|
|
var lower_boundary = utf8_lower_boundary;
|
|
|
|
utf8_code_point = 0;
|
|
|
|
utf8_bytes_needed = 0;
|
|
|
|
utf8_bytes_seen = 0;
|
|
|
|
utf8_lower_boundary = 0;
|
|
|
|
if (inRange(code_point, lower_boundary, 0x10FFFF) &&
|
|
|
|
!inRange(code_point, 0xD800, 0xDFFF)) {
|
|
|
|
return code_point;
|
|
|
|
}
|
|
|
|
return decoderError(fatal);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @constructor
|
|
|
|
* @param {{fatal: boolean}} options
|
|
|
|
*/
|
|
|
|
function UTF8Encoder(options) {
|
|
|
|
var fatal = options.fatal;
|
|
|
|
/**
|
|
|
|
* @param {ByteOutputStream} output_byte_stream Output byte stream.
|
|
|
|
* @param {CodePointInputStream} code_point_pointer Input stream.
|
|
|
|
* @return {number} The last byte emitted.
|
|
|
|
*/
|
|
|
|
this.encode = function(output_byte_stream, code_point_pointer) {
|
|
|
|
var code_point = code_point_pointer.get();
|
|
|
|
if (code_point === EOF_code_point) {
|
|
|
|
return EOF_byte;
|
|
|
|
}
|
|
|
|
code_point_pointer.offset(1);
|
|
|
|
if (inRange(code_point, 0xD800, 0xDFFF)) {
|
|
|
|
return encoderError(code_point);
|
|
|
|
}
|
|
|
|
if (inRange(code_point, 0x0000, 0x007f)) {
|
|
|
|
return output_byte_stream.emit(code_point);
|
|
|
|
}
|
|
|
|
var count, offset;
|
|
|
|
if (inRange(code_point, 0x0080, 0x07FF)) {
|
|
|
|
count = 1;
|
|
|
|
offset = 0xC0;
|
|
|
|
} else if (inRange(code_point, 0x0800, 0xFFFF)) {
|
|
|
|
count = 2;
|
|
|
|
offset = 0xE0;
|
|
|
|
} else if (inRange(code_point, 0x10000, 0x10FFFF)) {
|
|
|
|
count = 3;
|
|
|
|
offset = 0xF0;
|
|
|
|
}
|
|
|
|
var result = output_byte_stream.emit(
|
|
|
|
div(code_point, Math.pow(64, count)) + offset);
|
|
|
|
while (count > 0) {
|
|
|
|
var temp = div(code_point, Math.pow(64, count - 1));
|
|
|
|
result = output_byte_stream.emit(0x80 + (temp % 64));
|
|
|
|
count -= 1;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
name_to_encoding['utf-8'].getEncoder = function(options) {
|
|
|
|
return new UTF8Encoder(options);
|
|
|
|
};
|
|
|
|
name_to_encoding['utf-8'].getDecoder = function(options) {
|
|
|
|
return new UTF8Decoder(options);
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// 8. Legacy single-byte encodings
|
|
|
|
//
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @constructor
|
|
|
|
* @param {Array.<number>} index The encoding index.
|
|
|
|
* @param {{fatal: boolean}} options
|
|
|
|
*/
|
|
|
|
function SingleByteDecoder(index, options) {
|
|
|
|
var fatal = options.fatal;
|
|
|
|
/**
|
|
|
|
* @param {ByteInputStream} byte_pointer The byte stream to decode.
|
|
|
|
* @return {?number} The next code point decoded, or null if not enough
|
|
|
|
* data exists in the input stream to decode a complete code point.
|
|
|
|
*/
|
|
|
|
this.decode = function(byte_pointer) {
|
|
|
|
var bite = byte_pointer.get();
|
|
|
|
if (bite === EOF_byte) {
|
|
|
|
return EOF_code_point;
|
|
|
|
}
|
|
|
|
byte_pointer.offset(1);
|
|
|
|
if (inRange(bite, 0x00, 0x7F)) {
|
|
|
|
return bite;
|
|
|
|
}
|
|
|
|
var code_point = index[bite - 0x80];
|
|
|
|
if (code_point === null) {
|
|
|
|
return decoderError(fatal);
|
|
|
|
}
|
|
|
|
return code_point;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @constructor
|
|
|
|
* @param {Array.<?number>} index The encoding index.
|
|
|
|
* @param {{fatal: boolean}} options
|
|
|
|
*/
|
|
|
|
function SingleByteEncoder(index, options) {
|
|
|
|
var fatal = options.fatal;
|
|
|
|
/**
|
|
|
|
* @param {ByteOutputStream} output_byte_stream Output byte stream.
|
|
|
|
* @param {CodePointInputStream} code_point_pointer Input stream.
|
|
|
|
* @return {number} The last byte emitted.
|
|
|
|
*/
|
|
|
|
this.encode = function(output_byte_stream, code_point_pointer) {
|
|
|
|
var code_point = code_point_pointer.get();
|
|
|
|
if (code_point === EOF_code_point) {
|
|
|
|
return EOF_byte;
|
|
|
|
}
|
|
|
|
code_point_pointer.offset(1);
|
|
|
|
if (inRange(code_point, 0x0000, 0x007F)) {
|
|
|
|
return output_byte_stream.emit(code_point);
|
|
|
|
}
|
|
|
|
var pointer = indexPointerFor(code_point, index);
|
|
|
|
if (pointer === null) {
|
|
|
|
encoderError(code_point);
|
|
|
|
}
|
|
|
|
return output_byte_stream.emit(pointer + 0x80);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
encodings.forEach(function(category) {
|
|
|
|
if (category.heading !== 'Legacy single-byte encodings')
|
|
|
|
return;
|
|
|
|
category.encodings.forEach(function(encoding) {
|
|
|
|
var idx = index(encoding.name);
|
|
|
|
encoding.getDecoder = function(options) {
|
|
|
|
return new SingleByteDecoder(idx, options);
|
|
|
|
};
|
|
|
|
encoding.getEncoder = function(options) {
|
|
|
|
return new SingleByteEncoder(idx, options);
|
|
|
|
};
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}());
|
|
|
|
|
|
|
|
//
|
|
|
|
// 9. Legacy multi-byte Chinese (simplified) encodings
|
|
|
|
//
|
|
|
|
|
|
|
|
// 9.1 gbk
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @constructor
|
|
|
|
* @param {boolean} gb18030 True if decoding gb18030, false otherwise.
|
|
|
|
* @param {{fatal: boolean}} options
|
|
|
|
*/
|
|
|
|
function GBKDecoder(gb18030, options) {
|
|
|
|
var fatal = options.fatal;
|
|
|
|
var /** @type {number} */ gbk_first = 0x00,
|
|
|
|
/** @type {number} */ gbk_second = 0x00,
|
|
|
|
/** @type {number} */ gbk_third = 0x00;
|
|
|
|
/**
|
|
|
|
* @param {ByteInputStream} byte_pointer The byte stream to decode.
|
|
|
|
* @return {?number} The next code point decoded, or null if not enough
|
|
|
|
* data exists in the input stream to decode a complete code point.
|
|
|
|
*/
|
|
|
|
this.decode = function(byte_pointer) {
|
|
|
|
var bite = byte_pointer.get();
|
|
|
|
if (bite === EOF_byte && gbk_first === 0x00 &&
|
|
|
|
gbk_second === 0x00 && gbk_third === 0x00) {
|
|
|
|
return EOF_code_point;
|
|
|
|
}
|
|
|
|
if (bite === EOF_byte &&
|
|
|
|
(gbk_first !== 0x00 || gbk_second !== 0x00 || gbk_third !== 0x00)) {
|
|
|
|
gbk_first = 0x00;
|
|
|
|
gbk_second = 0x00;
|
|
|
|
gbk_third = 0x00;
|
|
|
|
decoderError(fatal);
|
|
|
|
}
|
|
|
|
byte_pointer.offset(1);
|
|
|
|
var code_point;
|
|
|
|
if (gbk_third !== 0x00) {
|
|
|
|
code_point = null;
|
|
|
|
if (inRange(bite, 0x30, 0x39)) {
|
|
|
|
code_point = indexGB18030CodePointFor(
|
|
|
|
(((gbk_first - 0x81) * 10 + (gbk_second - 0x30)) * 126 +
|
|
|
|
(gbk_third - 0x81)) * 10 + bite - 0x30);
|
|
|
|
}
|
|
|
|
gbk_first = 0x00;
|
|
|
|
gbk_second = 0x00;
|
|
|
|
gbk_third = 0x00;
|
|
|
|
if (code_point === null) {
|
|
|
|
byte_pointer.offset(-3);
|
|
|
|
return decoderError(fatal);
|
|
|
|
}
|
|
|
|
return code_point;
|
|
|
|
}
|
|
|
|
if (gbk_second !== 0x00) {
|
|
|
|
if (inRange(bite, 0x81, 0xFE)) {
|
|
|
|
gbk_third = bite;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
byte_pointer.offset(-2);
|
|
|
|
gbk_first = 0x00;
|
|
|
|
gbk_second = 0x00;
|
|
|
|
return decoderError(fatal);
|
|
|
|
}
|
|
|
|
if (gbk_first !== 0x00) {
|
|
|
|
if (inRange(bite, 0x30, 0x39) && gb18030) {
|
|
|
|
gbk_second = bite;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
var lead = gbk_first;
|
|
|
|
var pointer = null;
|
|
|
|
gbk_first = 0x00;
|
|
|
|
var offset = bite < 0x7F ? 0x40 : 0x41;
|
|
|
|
if (inRange(bite, 0x40, 0x7E) || inRange(bite, 0x80, 0xFE)) {
|
|
|
|
pointer = (lead - 0x81) * 190 + (bite - offset);
|
|
|
|
}
|
|
|
|
code_point = pointer === null ? null :
|
|
|
|
indexCodePointFor(pointer, index('gbk'));
|
|
|
|
if (pointer === null) {
|
|
|
|
byte_pointer.offset(-1);
|
|
|
|
}
|
|
|
|
if (code_point === null) {
|
|
|
|
return decoderError(fatal);
|
|
|
|
}
|
|
|
|
return code_point;
|
|
|
|
}
|
|
|
|
if (inRange(bite, 0x00, 0x7F)) {
|
|
|
|
return bite;
|
|
|
|
}
|
|
|
|
if (bite === 0x80) {
|
|
|
|
return 0x20AC;
|
|
|
|
}
|
|
|
|
if (inRange(bite, 0x81, 0xFE)) {
|
|
|
|
gbk_first = bite;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return decoderError(fatal);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @constructor
|
|
|
|
* @param {boolean} gb18030 True if decoding gb18030, false otherwise.
|
|
|
|
* @param {{fatal: boolean}} options
|
|
|
|
*/
|
|
|
|
function GBKEncoder(gb18030, options) {
|
|
|
|
var fatal = options.fatal;
|
|
|
|
/**
|
|
|
|
* @param {ByteOutputStream} output_byte_stream Output byte stream.
|
|
|
|
* @param {CodePointInputStream} code_point_pointer Input stream.
|
|
|
|
* @return {number} The last byte emitted.
|
|
|
|
*/
|
|
|
|
this.encode = function(output_byte_stream, code_point_pointer) {
|
|
|
|
var code_point = code_point_pointer.get();
|
|
|
|
if (code_point === EOF_code_point) {
|
|
|
|
return EOF_byte;
|
|
|
|
}
|
|
|
|
code_point_pointer.offset(1);
|
|
|
|
if (inRange(code_point, 0x0000, 0x007F)) {
|
|
|
|
return output_byte_stream.emit(code_point);
|
|
|
|
}
|
|
|
|
var pointer = indexPointerFor(code_point, index('gbk'));
|
|
|
|
if (pointer !== null) {
|
|
|
|
var lead = div(pointer, 190) + 0x81;
|
|
|
|
var trail = pointer % 190;
|
|
|
|
var offset = trail < 0x3F ? 0x40 : 0x41;
|
|
|
|
return output_byte_stream.emit(lead, trail + offset);
|
|
|
|
}
|
|
|
|
if (pointer === null && !gb18030) {
|
|
|
|
return encoderError(code_point);
|
|
|
|
}
|
|
|
|
pointer = indexGB18030PointerFor(code_point);
|
|
|
|
var byte1 = div(div(div(pointer, 10), 126), 10);
|
|
|
|
pointer = pointer - byte1 * 10 * 126 * 10;
|
|
|
|
var byte2 = div(div(pointer, 10), 126);
|
|
|
|
pointer = pointer - byte2 * 10 * 126;
|
|
|
|
var byte3 = div(pointer, 10);
|
|
|
|
var byte4 = pointer - byte3 * 10;
|
|
|
|
return output_byte_stream.emit(byte1 + 0x81,
|
|
|
|
byte2 + 0x30,
|
|
|
|
byte3 + 0x81,
|
|
|
|
byte4 + 0x30);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
name_to_encoding['gbk'].getEncoder = function(options) {
|
|
|
|
return new GBKEncoder(false, options);
|
|
|
|
};
|
|
|
|
name_to_encoding['gbk'].getDecoder = function(options) {
|
|
|
|
return new GBKDecoder(false, options);
|
|
|
|
};
|
|
|
|
|
|
|
|
// 9.2 gb18030
|
|
|
|
name_to_encoding['gb18030'].getEncoder = function(options) {
|
|
|
|
return new GBKEncoder(true, options);
|
|
|
|
};
|
|
|
|
name_to_encoding['gb18030'].getDecoder = function(options) {
|
|
|
|
return new GBKDecoder(true, options);
|
|
|
|
};
|
|
|
|
|
|
|
|
// 9.3 hz-gb-2312
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @constructor
|
|
|
|
* @param {{fatal: boolean}} options
|
|
|
|
*/
|
|
|
|
function HZGB2312Decoder(options) {
|
|
|
|
var fatal = options.fatal;
|
|
|
|
var /** @type {boolean} */ hzgb2312 = false,
|
|
|
|
/** @type {number} */ hzgb2312_lead = 0x00;
|
|
|
|
/**
|
|
|
|
* @param {ByteInputStream} byte_pointer The byte stream to decode.
|
|
|
|
* @return {?number} The next code point decoded, or null if not enough
|
|
|
|
* data exists in the input stream to decode a complete code point.
|
|
|
|
*/
|
|
|
|
this.decode = function(byte_pointer) {
|
|
|
|
var bite = byte_pointer.get();
|
|
|
|
if (bite === EOF_byte && hzgb2312_lead === 0x00) {
|
|
|
|
return EOF_code_point;
|
|
|
|
}
|
|
|
|
if (bite === EOF_byte && hzgb2312_lead !== 0x00) {
|
|
|
|
hzgb2312_lead = 0x00;
|
|
|
|
return decoderError(fatal);
|
|
|
|
}
|
|
|
|
byte_pointer.offset(1);
|
|
|
|
if (hzgb2312_lead === 0x7E) {
|
|
|
|
hzgb2312_lead = 0x00;
|
|
|
|
if (bite === 0x7B) {
|
|
|
|
hzgb2312 = true;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (bite === 0x7D) {
|
|
|
|
hzgb2312 = false;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (bite === 0x7E) {
|
|
|
|
return 0x007E;
|
|
|
|
}
|
|
|
|
if (bite === 0x0A) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
byte_pointer.offset(-1);
|
|
|
|
return decoderError(fatal);
|
|
|
|
}
|
|
|
|
if (hzgb2312_lead !== 0x00) {
|
|
|
|
var lead = hzgb2312_lead;
|
|
|
|
hzgb2312_lead = 0x00;
|
|
|
|
var code_point = null;
|
|
|
|
if (inRange(bite, 0x21, 0x7E)) {
|
|
|
|
code_point = indexCodePointFor((lead - 1) * 190 +
|
|
|
|
(bite + 0x3F), index('gbk'));
|
|
|
|
}
|
|
|
|
if (bite === 0x0A) {
|
|
|
|
hzgb2312 = false;
|
|
|
|
}
|
|
|
|
if (code_point === null) {
|
|
|
|
return decoderError(fatal);
|
|
|
|
}
|
|
|
|
return code_point;
|
|
|
|
}
|
|
|
|
if (bite === 0x7E) {
|
|
|
|
hzgb2312_lead = 0x7E;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (hzgb2312) {
|
|
|
|
if (inRange(bite, 0x20, 0x7F)) {
|
|
|
|
hzgb2312_lead = bite;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (bite === 0x0A) {
|
|
|
|
hzgb2312 = false;
|
|
|
|
}
|
|
|
|
return decoderError(fatal);
|
|
|
|
}
|
|
|
|
if (inRange(bite, 0x00, 0x7F)) {
|
|
|
|
return bite;
|
|
|
|
}
|
|
|
|
return decoderError(fatal);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @constructor
|
|
|
|
* @param {{fatal: boolean}} options
|
|
|
|
*/
|
|
|
|
function HZGB2312Encoder(options) {
|
|
|
|
var fatal = options.fatal;
|
|
|
|
var hzgb2312 = false;
|
|
|
|
/**
|
|
|
|
* @param {ByteOutputStream} output_byte_stream Output byte stream.
|
|
|
|
* @param {CodePointInputStream} code_point_pointer Input stream.
|
|
|
|
* @return {number} The last byte emitted.
|
|
|
|
*/
|
|
|
|
this.encode = function(output_byte_stream, code_point_pointer) {
|
|
|
|
var code_point = code_point_pointer.get();
|
|
|
|
if (code_point === EOF_code_point) {
|
|
|
|
return EOF_byte;
|
|
|
|
}
|
|
|
|
code_point_pointer.offset(1);
|
|
|
|
if (inRange(code_point, 0x0000, 0x007F) && hzgb2312) {
|
|
|
|
code_point_pointer.offset(-1);
|
|
|
|
hzgb2312 = false;
|
|
|
|
return output_byte_stream.emit(0x7E, 0x7D);
|
|
|
|
}
|
|
|
|
if (code_point === 0x007E) {
|
|
|
|
return output_byte_stream.emit(0x7E, 0x7E);
|
|
|
|
}
|
|
|
|
if (inRange(code_point, 0x0000, 0x007F)) {
|
|
|
|
return output_byte_stream.emit(code_point);
|
|
|
|
}
|
|
|
|
if (!hzgb2312) {
|
|
|
|
code_point_pointer.offset(-1);
|
|
|
|
hzgb2312 = true;
|
|
|
|
return output_byte_stream.emit(0x7E, 0x7B);
|
|
|
|
}
|
|
|
|
var pointer = indexPointerFor(code_point, index('gbk'));
|
|
|
|
if (pointer === null) {
|
|
|
|
return encoderError(code_point);
|
|
|
|
}
|
|
|
|
var lead = div(pointer, 190) + 1;
|
|
|
|
var trail = pointer % 190 - 0x3F;
|
|
|
|
if (!inRange(lead, 0x21, 0x7E) || !inRange(trail, 0x21, 0x7E)) {
|
|
|
|
return encoderError(code_point);
|
|
|
|
}
|
|
|
|
return output_byte_stream.emit(lead, trail);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
name_to_encoding['hz-gb-2312'].getEncoder = function(options) {
|
|
|
|
return new HZGB2312Encoder(options);
|
|
|
|
};
|
|
|
|
name_to_encoding['hz-gb-2312'].getDecoder = function(options) {
|
|
|
|
return new HZGB2312Decoder(options);
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// 10. Legacy multi-byte Chinese (traditional) encodings
|
|
|
|
//
|
|
|
|
|
|
|
|
// 10.1 big5
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @constructor
|
|
|
|
* @param {{fatal: boolean}} options
|
|
|
|
*/
|
|
|
|
function Big5Decoder(options) {
|
|
|
|
var fatal = options.fatal;
|
|
|
|
var /** @type {number} */ big5_lead = 0x00,
|
|
|
|
/** @type {?number} */ big5_pending = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {ByteInputStream} byte_pointer The byte steram to decode.
|
|
|
|
* @return {?number} The next code point decoded, or null if not enough
|
|
|
|
* data exists in the input stream to decode a complete code point.
|
|
|
|
*/
|
|
|
|
this.decode = function(byte_pointer) {
|
|
|
|
// NOTE: Hack to support emitting two code points
|
|
|
|
if (big5_pending !== null) {
|
|
|
|
var pending = big5_pending;
|
|
|
|
big5_pending = null;
|
|
|
|
return pending;
|
|
|
|
}
|
|
|
|
var bite = byte_pointer.get();
|
|
|
|
if (bite === EOF_byte && big5_lead === 0x00) {
|
|
|
|
return EOF_code_point;
|
|
|
|
}
|
|
|
|
if (bite === EOF_byte && big5_lead !== 0x00) {
|
|
|
|
big5_lead = 0x00;
|
|
|
|
return decoderError(fatal);
|
|
|
|
}
|
|
|
|
byte_pointer.offset(1);
|
|
|
|
if (big5_lead !== 0x00) {
|
|
|
|
var lead = big5_lead;
|
|
|
|
var pointer = null;
|
|
|
|
big5_lead = 0x00;
|
|
|
|
var offset = bite < 0x7F ? 0x40 : 0x62;
|
|
|
|
if (inRange(bite, 0x40, 0x7E) || inRange(bite, 0xA1, 0xFE)) {
|
|
|
|
pointer = (lead - 0x81) * 157 + (bite - offset);
|
|
|
|
}
|
|
|
|
if (pointer === 1133) {
|
|
|
|
big5_pending = 0x0304;
|
|
|
|
return 0x00CA;
|
|
|
|
}
|
|
|
|
if (pointer === 1135) {
|
|
|
|
big5_pending = 0x030C;
|
|
|
|
return 0x00CA;
|
|
|
|
}
|
|
|
|
if (pointer === 1164) {
|
|
|
|
big5_pending = 0x0304;
|
|
|
|
return 0x00EA;
|
|
|
|
}
|
|
|
|
if (pointer === 1166) {
|
|
|
|
big5_pending = 0x030C;
|
|
|
|
return 0x00EA;
|
|
|
|
}
|
|
|
|
var code_point = (pointer === null) ? null :
|
|
|
|
indexCodePointFor(pointer, index('big5'));
|
|
|
|
if (pointer === null) {
|
|
|
|
byte_pointer.offset(-1);
|
|
|
|
}
|
|
|
|
if (code_point === null) {
|
|
|
|
return decoderError(fatal);
|
|
|
|
}
|
|
|
|
return code_point;
|
|
|
|
}
|
|
|
|
if (inRange(bite, 0x00, 0x7F)) {
|
|
|
|
return bite;
|
|
|
|
}
|
|
|
|
if (inRange(bite, 0x81, 0xFE)) {
|
|
|
|
big5_lead = bite;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return decoderError(fatal);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @constructor
|
|
|
|
* @param {{fatal: boolean}} options
|
|
|
|
*/
|
|
|
|
function Big5Encoder(options) {
|
|
|
|
var fatal = options.fatal;
|
|
|
|
/**
|
|
|
|
* @param {ByteOutputStream} output_byte_stream Output byte stream.
|
|
|
|
* @param {CodePointInputStream} code_point_pointer Input stream.
|
|
|
|
* @return {number} The last byte emitted.
|
|
|
|
*/
|
|
|
|
this.encode = function(output_byte_stream, code_point_pointer) {
|
|
|
|
var code_point = code_point_pointer.get();
|
|
|
|
if (code_point === EOF_code_point) {
|
|
|
|
return EOF_byte;
|
|
|
|
}
|
|
|
|
code_point_pointer.offset(1);
|
|
|
|
if (inRange(code_point, 0x0000, 0x007F)) {
|
|
|
|
return output_byte_stream.emit(code_point);
|
|
|
|
}
|
|
|
|
var pointer = indexPointerFor(code_point, index('big5'));
|
|
|
|
if (pointer === null) {
|
|
|
|
return encoderError(code_point);
|
|
|
|
}
|
|
|
|
var lead = div(pointer, 157) + 0x81;
|
|
|
|
//if (lead < 0xA1) {
|
|
|
|
// return encoderError(code_point);
|
|
|
|
//}
|
|
|
|
var trail = pointer % 157;
|
|
|
|
var offset = trail < 0x3F ? 0x40 : 0x62;
|
|
|
|
return output_byte_stream.emit(lead, trail + offset);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
name_to_encoding['big5'].getEncoder = function(options) {
|
|
|
|
return new Big5Encoder(options);
|
|
|
|
};
|
|
|
|
name_to_encoding['big5'].getDecoder = function(options) {
|
|
|
|
return new Big5Decoder(options);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// 11. Legacy multi-byte Japanese encodings
|
|
|
|
//
|
|
|
|
|
|
|
|
// 11.1 euc.jp
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @constructor
|
|
|
|
* @param {{fatal: boolean}} options
|
|
|
|
*/
|
|
|
|
function EUCJPDecoder(options) {
|
|
|
|
var fatal = options.fatal;
|
|
|
|
var /** @type {number} */ eucjp_first = 0x00,
|
|
|
|
/** @type {number} */ eucjp_second = 0x00;
|
|
|
|
/**
|
|
|
|
* @param {ByteInputStream} byte_pointer The byte stream to decode.
|
|
|
|
* @return {?number} The next code point decoded, or null if not enough
|
|
|
|
* data exists in the input stream to decode a complete code point.
|
|
|
|
*/
|
|
|
|
this.decode = function(byte_pointer) {
|
|
|
|
var bite = byte_pointer.get();
|
|
|
|
if (bite === EOF_byte) {
|
|
|
|
if (eucjp_first === 0x00 && eucjp_second === 0x00) {
|
|
|
|
return EOF_code_point;
|
|
|
|
}
|
|
|
|
eucjp_first = 0x00;
|
|
|
|
eucjp_second = 0x00;
|
|
|
|
return decoderError(fatal);
|
|
|
|
}
|
|
|
|
byte_pointer.offset(1);
|
|
|
|
|
|
|
|
var lead, code_point;
|
|
|
|
if (eucjp_second !== 0x00) {
|
|
|
|
lead = eucjp_second;
|
|
|
|
eucjp_second = 0x00;
|
|
|
|
code_point = null;
|
|
|
|
if (inRange(lead, 0xA1, 0xFE) && inRange(bite, 0xA1, 0xFE)) {
|
|
|
|
code_point = indexCodePointFor((lead - 0xA1) * 94 + bite - 0xA1,
|
|
|
|
index('jis0212'));
|
|
|
|
}
|
|
|
|
if (!inRange(bite, 0xA1, 0xFE)) {
|
|
|
|
byte_pointer.offset(-1);
|
|
|
|
}
|
|
|
|
if (code_point === null) {
|
|
|
|
return decoderError(fatal);
|
|
|
|
}
|
|
|
|
return code_point;
|
|
|
|
}
|
|
|
|
if (eucjp_first === 0x8E && inRange(bite, 0xA1, 0xDF)) {
|
|
|
|
eucjp_first = 0x00;
|
|
|
|
return 0xFF61 + bite - 0xA1;
|
|
|
|
}
|
|
|
|
if (eucjp_first === 0x8F && inRange(bite, 0xA1, 0xFE)) {
|
|
|
|
eucjp_first = 0x00;
|
|
|
|
eucjp_second = bite;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (eucjp_first !== 0x00) {
|
|
|
|
lead = eucjp_first;
|
|
|
|
eucjp_first = 0x00;
|
|
|
|
code_point = null;
|
|
|
|
if (inRange(lead, 0xA1, 0xFE) && inRange(bite, 0xA1, 0xFE)) {
|
|
|
|
code_point = indexCodePointFor((lead - 0xA1) * 94 + bite - 0xA1,
|
|
|
|
index('jis0208'));
|
|
|
|
}
|
|
|
|
if (!inRange(bite, 0xA1, 0xFE)) {
|
|
|
|
byte_pointer.offset(-1);
|
|
|
|
}
|
|
|
|
if (code_point === null) {
|
|
|
|
return decoderError(fatal);
|
|
|
|
}
|
|
|
|
return code_point;
|
|
|
|
}
|
|
|
|
if (inRange(bite, 0x00, 0x7F)) {
|
|
|
|
return bite;
|
|
|
|
}
|
|
|
|
if (bite === 0x8E || bite === 0x8F || (inRange(bite, 0xA1, 0xFE))) {
|
|
|
|
eucjp_first = bite;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return decoderError(fatal);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @constructor
|
|
|
|
* @param {{fatal: boolean}} options
|
|
|
|
*/
|
|
|
|
function EUCJPEncoder(options) {
|
|
|
|
var fatal = options.fatal;
|
|
|
|
/**
|
|
|
|
* @param {ByteOutputStream} output_byte_stream Output byte stream.
|
|
|
|
* @param {CodePointInputStream} code_point_pointer Input stream.
|
|
|
|
* @return {number} The last byte emitted.
|
|
|
|
*/
|
|
|
|
this.encode = function(output_byte_stream, code_point_pointer) {
|
|
|
|
var code_point = code_point_pointer.get();
|
|
|
|
if (code_point === EOF_code_point) {
|
|
|
|
return EOF_byte;
|
|
|
|
}
|
|
|
|
code_point_pointer.offset(1);
|
|
|
|
if (inRange(code_point, 0x0000, 0x007F)) {
|
|
|
|
return output_byte_stream.emit(code_point);
|
|
|
|
}
|
|
|
|
if (code_point === 0x00A5) {
|
|
|
|
return output_byte_stream.emit(0x5C);
|
|
|
|
}
|
|
|
|
if (code_point === 0x203E) {
|
|
|
|
return output_byte_stream.emit(0x7E);
|
|
|
|
}
|
|
|
|
if (inRange(code_point, 0xFF61, 0xFF9F)) {
|
|
|
|
return output_byte_stream.emit(0x8E, code_point - 0xFF61 + 0xA1);
|
|
|
|
}
|
|
|
|
|
|
|
|
var pointer = indexPointerFor(code_point, index('jis0208'));
|
|
|
|
if (pointer === null) {
|
|
|
|
return encoderError(code_point);
|
|
|
|
}
|
|
|
|
var lead = div(pointer, 94) + 0xA1;
|
|
|
|
var trail = pointer % 94 + 0xA1;
|
|
|
|
return output_byte_stream.emit(lead, trail);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
name_to_encoding['euc-jp'].getEncoder = function(options) {
|
|
|
|
return new EUCJPEncoder(options);
|
|
|
|
};
|
|
|
|
name_to_encoding['euc-jp'].getDecoder = function(options) {
|
|
|
|
return new EUCJPDecoder(options);
|
|
|
|
};
|
|
|
|
|
|
|
|
// 11.2 iso-2022-jp
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @constructor
|
|
|
|
* @param {{fatal: boolean}} options
|
|
|
|
*/
|
|
|
|
function ISO2022JPDecoder(options) {
|
|
|
|
var fatal = options.fatal;
|
|
|
|
/** @enum */
|
|
|
|
var state = {
|
|
|
|
ASCII: 0,
|
|
|
|
escape_start: 1,
|
|
|
|
escape_middle: 2,
|
|
|
|
escape_final: 3,
|
|
|
|
lead: 4,
|
|
|
|
trail: 5,
|
|
|
|
Katakana: 6
|
|
|
|
};
|
|
|
|
var /** @type {number} */ iso2022jp_state = state.ASCII,
|
|
|
|
/** @type {boolean} */ iso2022jp_jis0212 = false,
|
|
|
|
/** @type {number} */ iso2022jp_lead = 0x00;
|
|
|
|
/**
|
|
|
|
* @param {ByteInputStream} byte_pointer The byte stream to decode.
|
|
|
|
* @return {?number} The next code point decoded, or null if not enough
|
|
|
|
* data exists in the input stream to decode a complete code point.
|
|
|
|
*/
|
|
|
|
this.decode = function(byte_pointer) {
|
|
|
|
var bite = byte_pointer.get();
|
|
|
|
if (bite !== EOF_byte) {
|
|
|
|
byte_pointer.offset(1);
|
|
|
|
}
|
|
|
|
switch (iso2022jp_state) {
|
|
|
|
default:
|
|
|
|
case state.ASCII:
|
|
|
|
if (bite === 0x1B) {
|
|
|
|
iso2022jp_state = state.escape_start;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (inRange(bite, 0x00, 0x7F)) {
|
|
|
|
return bite;
|
|
|
|
}
|
|
|
|
if (bite === EOF_byte) {
|
|
|
|
return EOF_code_point;
|
|
|
|
}
|
|
|
|
return decoderError(fatal);
|
|
|
|
|
|
|
|
case state.escape_start:
|
|
|
|
if (bite === 0x24 || bite === 0x28) {
|
|
|
|
iso2022jp_lead = bite;
|
|
|
|
iso2022jp_state = state.escape_middle;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (bite !== EOF_byte) {
|
|
|
|
byte_pointer.offset(-1);
|
|
|
|
}
|
|
|
|
iso2022jp_state = state.ASCII;
|
|
|
|
return decoderError(fatal);
|
|
|
|
|
|
|
|
case state.escape_middle:
|
|
|
|
var lead = iso2022jp_lead;
|
|
|
|
iso2022jp_lead = 0x00;
|
|
|
|
if (lead === 0x24 && (bite === 0x40 || bite === 0x42)) {
|
|
|
|
iso2022jp_jis0212 = false;
|
|
|
|
iso2022jp_state = state.lead;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (lead === 0x24 && bite === 0x28) {
|
|
|
|
iso2022jp_state = state.escape_final;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (lead === 0x28 && (bite === 0x42 || bite === 0x4A)) {
|
|
|
|
iso2022jp_state = state.ASCII;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (lead === 0x28 && bite === 0x49) {
|
|
|
|
iso2022jp_state = state.Katakana;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (bite === EOF_byte) {
|
|
|
|
byte_pointer.offset(-1);
|
|
|
|
} else {
|
|
|
|
byte_pointer.offset(-2);
|
|
|
|
}
|
|
|
|
iso2022jp_state = state.ASCII;
|
|
|
|
return decoderError(fatal);
|
|
|
|
|
|
|
|
case state.escape_final:
|
|
|
|
if (bite === 0x44) {
|
|
|
|
iso2022jp_jis0212 = true;
|
|
|
|
iso2022jp_state = state.lead;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (bite === EOF_byte) {
|
|
|
|
byte_pointer.offset(-2);
|
|
|
|
} else {
|
|
|
|
byte_pointer.offset(-3);
|
|
|
|
}
|
|
|
|
iso2022jp_state = state.ASCII;
|
|
|
|
return decoderError(fatal);
|
|
|
|
|
|
|
|
case state.lead:
|
|
|
|
if (bite === 0x0A) {
|
|
|
|
iso2022jp_state = state.ASCII;
|
|
|
|
return decoderError(fatal, 0x000A);
|
|
|
|
}
|
|
|
|
if (bite === 0x1B) {
|
|
|
|
iso2022jp_state = state.escape_start;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (bite === EOF_byte) {
|
|
|
|
return EOF_code_point;
|
|
|
|
}
|
|
|
|
iso2022jp_lead = bite;
|
|
|
|
iso2022jp_state = state.trail;
|
|
|
|
return null;
|
|
|
|
|
|
|
|
case state.trail:
|
|
|
|
iso2022jp_state = state.lead;
|
|
|
|
if (bite === EOF_byte) {
|
|
|
|
return decoderError(fatal);
|
|
|
|
}
|
|
|
|
var code_point = null;
|
|
|
|
var pointer = (iso2022jp_lead - 0x21) * 94 + bite - 0x21;
|
|
|
|
if (inRange(iso2022jp_lead, 0x21, 0x7E) &&
|
|
|
|
inRange(bite, 0x21, 0x7E)) {
|
|
|
|
code_point = (iso2022jp_jis0212 === false) ?
|
|
|
|
indexCodePointFor(pointer, index('jis0208')) :
|
|
|
|
indexCodePointFor(pointer, index('jis0212'));
|
|
|
|
}
|
|
|
|
if (code_point === null) {
|
|
|
|
return decoderError(fatal);
|
|
|
|
}
|
|
|
|
return code_point;
|
|
|
|
|
|
|
|
case state.Katakana:
|
|
|
|
if (bite === 0x1B) {
|
|
|
|
iso2022jp_state = state.escape_start;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (inRange(bite, 0x21, 0x5F)) {
|
|
|
|
return 0xFF61 + bite - 0x21;
|
|
|
|
}
|
|
|
|
if (bite === EOF_byte) {
|
|
|
|
return EOF_code_point;
|
|
|
|
}
|
|
|
|
return decoderError(fatal);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @constructor
|
|
|
|
* @param {{fatal: boolean}} options
|
|
|
|
*/
|
|
|
|
function ISO2022JPEncoder(options) {
|
|
|
|
var fatal = options.fatal;
|
|
|
|
/** @enum */
|
|
|
|
var state = {
|
|
|
|
ASCII: 0,
|
|
|
|
lead: 1,
|
|
|
|
Katakana: 2
|
|
|
|
};
|
|
|
|
var /** @type {number} */ iso2022jp_state = state.ASCII;
|
|
|
|
/**
|
|
|
|
* @param {ByteOutputStream} output_byte_stream Output byte stream.
|
|
|
|
* @param {CodePointInputStream} code_point_pointer Input stream.
|
|
|
|
* @return {number} The last byte emitted.
|
|
|
|
*/
|
|
|
|
this.encode = function(output_byte_stream, code_point_pointer) {
|
|
|
|
var code_point = code_point_pointer.get();
|
|
|
|
if (code_point === EOF_code_point) {
|
|
|
|
return EOF_byte;
|
|
|
|
}
|
|
|
|
code_point_pointer.offset(1);
|
|
|
|
if ((inRange(code_point, 0x0000, 0x007F) ||
|
|
|
|
code_point === 0x00A5 || code_point === 0x203E) &&
|
|
|
|
iso2022jp_state !== state.ASCII) {
|
|
|
|
code_point_pointer.offset(-1);
|
|
|
|
iso2022jp_state = state.ASCII;
|
|
|
|
return output_byte_stream.emit(0x1B, 0x28, 0x42);
|
|
|
|
}
|
|
|
|
if (inRange(code_point, 0x0000, 0x007F)) {
|
|
|
|
return output_byte_stream.emit(code_point);
|
|
|
|
}
|
|
|
|
if (code_point === 0x00A5) {
|
|
|
|
return output_byte_stream.emit(0x5C);
|
|
|
|
}
|
|
|
|
if (code_point === 0x203E) {
|
|
|
|
return output_byte_stream.emit(0x7E);
|
|
|
|
}
|
|
|
|
if (inRange(code_point, 0xFF61, 0xFF9F) &&
|
|
|
|
iso2022jp_state !== state.Katakana) {
|
|
|
|
code_point_pointer.offset(-1);
|
|
|
|
iso2022jp_state = state.Katakana;
|
|
|
|
return output_byte_stream.emit(0x1B, 0x28, 0x49);
|
|
|
|
}
|
|
|
|
if (inRange(code_point, 0xFF61, 0xFF9F)) {
|
|
|
|
return output_byte_stream.emit(code_point - 0xFF61 - 0x21);
|
|
|
|
}
|
|
|
|
if (iso2022jp_state !== state.lead) {
|
|
|
|
code_point_pointer.offset(-1);
|
|
|
|
iso2022jp_state = state.lead;
|
|
|
|
return output_byte_stream.emit(0x1B, 0x24, 0x42);
|
|
|
|
}
|
|
|
|
var pointer = indexPointerFor(code_point, index('jis0208'));
|
|
|
|
if (pointer === null) {
|
|
|
|
return encoderError(code_point);
|
|
|
|
}
|
|
|
|
var lead = div(pointer, 94) + 0x21;
|
|
|
|
var trail = pointer % 94 + 0x21;
|
|
|
|
return output_byte_stream.emit(lead, trail);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
name_to_encoding['iso-2022-jp'].getEncoder = function(options) {
|
|
|
|
return new ISO2022JPEncoder(options);
|
|
|
|
};
|
|
|
|
name_to_encoding['iso-2022-jp'].getDecoder = function(options) {
|
|
|
|
return new ISO2022JPDecoder(options);
|
|
|
|
};
|
|
|
|
|
|
|
|
// 11.3 shift_jis
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @constructor
|
|
|
|
* @param {{fatal: boolean}} options
|
|
|
|
*/
|
|
|
|
function ShiftJISDecoder(options) {
|
|
|
|
var fatal = options.fatal;
|
|
|
|
var /** @type {number} */ shiftjis_lead = 0x00;
|
|
|
|
/**
|
|
|
|
* @param {ByteInputStream} byte_pointer The byte stream to decode.
|
|
|
|
* @return {?number} The next code point decoded, or null if not enough
|
|
|
|
* data exists in the input stream to decode a complete code point.
|
|
|
|
*/
|
|
|
|
this.decode = function(byte_pointer) {
|
|
|
|
var bite = byte_pointer.get();
|
|
|
|
if (bite === EOF_byte && shiftjis_lead === 0x00) {
|
|
|
|
return EOF_code_point;
|
|
|
|
}
|
|
|
|
if (bite === EOF_byte && shiftjis_lead !== 0x00) {
|
|
|
|
shiftjis_lead = 0x00;
|
|
|
|
return decoderError(fatal);
|
|
|
|
}
|
|
|
|
byte_pointer.offset(1);
|
|
|
|
if (shiftjis_lead !== 0x00) {
|
|
|
|
var lead = shiftjis_lead;
|
|
|
|
shiftjis_lead = 0x00;
|
|
|
|
if (inRange(bite, 0x40, 0x7E) || inRange(bite, 0x80, 0xFC)) {
|
|
|
|
var offset = (bite < 0x7F) ? 0x40 : 0x41;
|
|
|
|
var lead_offset = (lead < 0xA0) ? 0x81 : 0xC1;
|
|
|
|
var code_point = indexCodePointFor((lead - lead_offset) * 188 +
|
|
|
|
bite - offset, index('jis0208'));
|
|
|
|
if (code_point === null) {
|
|
|
|
return decoderError(fatal);
|
|
|
|
}
|
|
|
|
return code_point;
|
|
|
|
}
|
|
|
|
byte_pointer.offset(-1);
|
|
|
|
return decoderError(fatal);
|
|
|
|
}
|
|
|
|
if (inRange(bite, 0x00, 0x80)) {
|
|
|
|
return bite;
|
|
|
|
}
|
|
|
|
if (inRange(bite, 0xA1, 0xDF)) {
|
|
|
|
return 0xFF61 + bite - 0xA1;
|
|
|
|
}
|
|
|
|
if (inRange(bite, 0x81, 0x9F) || inRange(bite, 0xE0, 0xFC)) {
|
|
|
|
shiftjis_lead = bite;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return decoderError(fatal);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @constructor
|
|
|
|
* @param {{fatal: boolean}} options
|
|
|
|
*/
|
|
|
|
function ShiftJISEncoder(options) {
|
|
|
|
var fatal = options.fatal;
|
|
|
|
/**
|
|
|
|
* @param {ByteOutputStream} output_byte_stream Output byte stream.
|
|
|
|
* @param {CodePointInputStream} code_point_pointer Input stream.
|
|
|
|
* @return {number} The last byte emitted.
|
|
|
|
*/
|
|
|
|
this.encode = function(output_byte_stream, code_point_pointer) {
|
|
|
|
var code_point = code_point_pointer.get();
|
|
|
|
if (code_point === EOF_code_point) {
|
|
|
|
return EOF_byte;
|
|
|
|
}
|
|
|
|
code_point_pointer.offset(1);
|
|
|
|
if (inRange(code_point, 0x0000, 0x0080)) {
|
|
|
|
return output_byte_stream.emit(code_point);
|
|
|
|
}
|
|
|
|
if (code_point === 0x00A5) {
|
|
|
|
return output_byte_stream.emit(0x5C);
|
|
|
|
}
|
|
|
|
if (code_point === 0x203E) {
|
|
|
|
return output_byte_stream.emit(0x7E);
|
|
|
|
}
|
|
|
|
if (inRange(code_point, 0xFF61, 0xFF9F)) {
|
|
|
|
return output_byte_stream.emit(code_point - 0xFF61 + 0xA1);
|
|
|
|
}
|
|
|
|
var pointer = indexPointerFor(code_point, index('jis0208'));
|
|
|
|
if (pointer === null) {
|
|
|
|
return encoderError(code_point);
|
|
|
|
}
|
|
|
|
var lead = div(pointer, 188);
|
|
|
|
var lead_offset = lead < 0x1F ? 0x81 : 0xC1;
|
|
|
|
var trail = pointer % 188;
|
|
|
|
var offset = trail < 0x3F ? 0x40 : 0x41;
|
|
|
|
return output_byte_stream.emit(lead + lead_offset, trail + offset);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
name_to_encoding['shift_jis'].getEncoder = function(options) {
|
|
|
|
return new ShiftJISEncoder(options);
|
|
|
|
};
|
|
|
|
name_to_encoding['shift_jis'].getDecoder = function(options) {
|
|
|
|
return new ShiftJISDecoder(options);
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// 12. Legacy multi-byte Korean encodings
|
|
|
|
//
|
|
|
|
|
|
|
|
// 12.1 euc-kr
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @constructor
|
|
|
|
* @param {{fatal: boolean}} options
|
|
|
|
*/
|
|
|
|
function EUCKRDecoder(options) {
|
|
|
|
var fatal = options.fatal;
|
|
|
|
var /** @type {number} */ euckr_lead = 0x00;
|
|
|
|
/**
|
|
|
|
* @param {ByteInputStream} byte_pointer The byte stream to decode.
|
|
|
|
* @return {?number} The next code point decoded, or null if not enough
|
|
|
|
* data exists in the input stream to decode a complete code point.
|
|
|
|
*/
|
|
|
|
this.decode = function(byte_pointer) {
|
|
|
|
var bite = byte_pointer.get();
|
|
|
|
if (bite === EOF_byte && euckr_lead === 0) {
|
|
|
|
return EOF_code_point;
|
|
|
|
}
|
|
|
|
if (bite === EOF_byte && euckr_lead !== 0) {
|
|
|
|
euckr_lead = 0x00;
|
|
|
|
return decoderError(fatal);
|
|
|
|
}
|
|
|
|
byte_pointer.offset(1);
|
|
|
|
if (euckr_lead !== 0x00) {
|
|
|
|
var lead = euckr_lead;
|
|
|
|
var pointer = null;
|
|
|
|
euckr_lead = 0x00;
|
|
|
|
|
|
|
|
if (inRange(lead, 0x81, 0xC6)) {
|
|
|
|
var temp = (26 + 26 + 126) * (lead - 0x81);
|
|
|
|
if (inRange(bite, 0x41, 0x5A)) {
|
|
|
|
pointer = temp + bite - 0x41;
|
|
|
|
} else if (inRange(bite, 0x61, 0x7A)) {
|
|
|
|
pointer = temp + 26 + bite - 0x61;
|
|
|
|
} else if (inRange(bite, 0x81, 0xFE)) {
|
|
|
|
pointer = temp + 26 + 26 + bite - 0x81;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inRange(lead, 0xC7, 0xFD) && inRange(bite, 0xA1, 0xFE)) {
|
|
|
|
pointer = (26 + 26 + 126) * (0xC7 - 0x81) + (lead - 0xC7) * 94 +
|
|
|
|
(bite - 0xA1);
|
|
|
|
}
|
|
|
|
|
|
|
|
var code_point = (pointer === null) ? null :
|
|
|
|
indexCodePointFor(pointer, index('euc-kr'));
|
|
|
|
if (pointer === null) {
|
|
|
|
byte_pointer.offset(-1);
|
|
|
|
}
|
|
|
|
if (code_point === null) {
|
|
|
|
return decoderError(fatal);
|
|
|
|
}
|
|
|
|
return code_point;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inRange(bite, 0x00, 0x7F)) {
|
|
|
|
return bite;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inRange(bite, 0x81, 0xFD)) {
|
|
|
|
euckr_lead = bite;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return decoderError(fatal);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @constructor
|
|
|
|
* @param {{fatal: boolean}} options
|
|
|
|
*/
|
|
|
|
function EUCKREncoder(options) {
|
|
|
|
var fatal = options.fatal;
|
|
|
|
/**
|
|
|
|
* @param {ByteOutputStream} output_byte_stream Output byte stream.
|
|
|
|
* @param {CodePointInputStream} code_point_pointer Input stream.
|
|
|
|
* @return {number} The last byte emitted.
|
|
|
|
*/
|
|
|
|
this.encode = function(output_byte_stream, code_point_pointer) {
|
|
|
|
var code_point = code_point_pointer.get();
|
|
|
|
if (code_point === EOF_code_point) {
|
|
|
|
return EOF_byte;
|
|
|
|
}
|
|
|
|
code_point_pointer.offset(1);
|
|
|
|
if (inRange(code_point, 0x0000, 0x007F)) {
|
|
|
|
return output_byte_stream.emit(code_point);
|
|
|
|
}
|
|
|
|
var pointer = indexPointerFor(code_point, index('euc-kr'));
|
|
|
|
if (pointer === null) {
|
|
|
|
return encoderError(code_point);
|
|
|
|
}
|
|
|
|
var lead, trail;
|
|
|
|
if (pointer < ((26 + 26 + 126) * (0xC7 - 0x81))) {
|
|
|
|
lead = div(pointer, (26 + 26 + 126)) + 0x81;
|
|
|
|
trail = pointer % (26 + 26 + 126);
|
|
|
|
var offset = trail < 26 ? 0x41 : trail < 26 + 26 ? 0x47 : 0x4D;
|
|
|
|
return output_byte_stream.emit(lead, trail + offset);
|
|
|
|
}
|
|
|
|
pointer = pointer - (26 + 26 + 126) * (0xC7 - 0x81);
|
|
|
|
lead = div(pointer, 94) + 0xC7;
|
|
|
|
trail = pointer % 94 + 0xA1;
|
|
|
|
return output_byte_stream.emit(lead, trail);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
name_to_encoding['euc-kr'].getEncoder = function(options) {
|
|
|
|
return new EUCKREncoder(options);
|
|
|
|
};
|
|
|
|
name_to_encoding['euc-kr'].getDecoder = function(options) {
|
|
|
|
return new EUCKRDecoder(options);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// 13. Legacy utf-16 encodings
|
|
|
|
//
|
|
|
|
|
|
|
|
// 13.1 utf-16
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @constructor
|
|
|
|
* @param {boolean} utf16_be True if big-endian, false if little-endian.
|
|
|
|
* @param {{fatal: boolean}} options
|
|
|
|
*/
|
|
|
|
function UTF16Decoder(utf16_be, options) {
|
|
|
|
var fatal = options.fatal;
|
|
|
|
var /** @type {?number} */ utf16_lead_byte = null,
|
|
|
|
/** @type {?number} */ utf16_lead_surrogate = null;
|
|
|
|
/**
|
|
|
|
* @param {ByteInputStream} byte_pointer The byte stream to decode.
|
|
|
|
* @return {?number} The next code point decoded, or null if not enough
|
|
|
|
* data exists in the input stream to decode a complete code point.
|
|
|
|
*/
|
|
|
|
this.decode = function(byte_pointer) {
|
|
|
|
var bite = byte_pointer.get();
|
|
|
|
if (bite === EOF_byte && utf16_lead_byte === null &&
|
|
|
|
utf16_lead_surrogate === null) {
|
|
|
|
return EOF_code_point;
|
|
|
|
}
|
|
|
|
if (bite === EOF_byte && (utf16_lead_byte !== null ||
|
|
|
|
utf16_lead_surrogate !== null)) {
|
|
|
|
return decoderError(fatal);
|
|
|
|
}
|
|
|
|
byte_pointer.offset(1);
|
|
|
|
if (utf16_lead_byte === null) {
|
|
|
|
utf16_lead_byte = bite;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
var code_point;
|
|
|
|
if (utf16_be) {
|
|
|
|
code_point = (utf16_lead_byte << 8) + bite;
|
|
|
|
} else {
|
|
|
|
code_point = (bite << 8) + utf16_lead_byte;
|
|
|
|
}
|
|
|
|
utf16_lead_byte = null;
|
|
|
|
if (utf16_lead_surrogate !== null) {
|
|
|
|
var lead_surrogate = utf16_lead_surrogate;
|
|
|
|
utf16_lead_surrogate = null;
|
|
|
|
if (inRange(code_point, 0xDC00, 0xDFFF)) {
|
|
|
|
return 0x10000 + (lead_surrogate - 0xD800) * 0x400 +
|
|
|
|
(code_point - 0xDC00);
|
|
|
|
}
|
|
|
|
byte_pointer.offset(-2);
|
|
|
|
return decoderError(fatal);
|
|
|
|
}
|
|
|
|
if (inRange(code_point, 0xD800, 0xDBFF)) {
|
|
|
|
utf16_lead_surrogate = code_point;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (inRange(code_point, 0xDC00, 0xDFFF)) {
|
|
|
|
return decoderError(fatal);
|
|
|
|
}
|
|
|
|
return code_point;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @constructor
|
|
|
|
* @param {boolean} utf16_be True if big-endian, false if little-endian.
|
|
|
|
* @param {{fatal: boolean}} options
|
|
|
|
*/
|
|
|
|
function UTF16Encoder(utf16_be, options) {
|
|
|
|
var fatal = options.fatal;
|
|
|
|
/**
|
|
|
|
* @param {ByteOutputStream} output_byte_stream Output byte stream.
|
|
|
|
* @param {CodePointInputStream} code_point_pointer Input stream.
|
|
|
|
* @return {number} The last byte emitted.
|
|
|
|
*/
|
|
|
|
this.encode = function(output_byte_stream, code_point_pointer) {
|
|
|
|
function convert_to_bytes(code_unit) {
|
|
|
|
var byte1 = code_unit >> 8;
|
|
|
|
var byte2 = code_unit & 0x00FF;
|
|
|
|
if (utf16_be) {
|
|
|
|
return output_byte_stream.emit(byte1, byte2);
|
|
|
|
}
|
|
|
|
return output_byte_stream.emit(byte2, byte1);
|
|
|
|
}
|
|
|
|
var code_point = code_point_pointer.get();
|
|
|
|
if (code_point === EOF_code_point) {
|
|
|
|
return EOF_byte;
|
|
|
|
}
|
|
|
|
code_point_pointer.offset(1);
|
|
|
|
if (inRange(code_point, 0xD800, 0xDFFF)) {
|
|
|
|
encoderError(code_point);
|
|
|
|
}
|
|
|
|
if (code_point <= 0xFFFF) {
|
|
|
|
return convert_to_bytes(code_point);
|
|
|
|
}
|
|
|
|
var lead = div((code_point - 0x10000), 0x400) + 0xD800;
|
|
|
|
var trail = ((code_point - 0x10000) % 0x400) + 0xDC00;
|
|
|
|
convert_to_bytes(lead);
|
|
|
|
return convert_to_bytes(trail);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
name_to_encoding['utf-16le'].getEncoder = function(options) {
|
|
|
|
return new UTF16Encoder(false, options);
|
|
|
|
};
|
|
|
|
name_to_encoding['utf-16le'].getDecoder = function(options) {
|
|
|
|
return new UTF16Decoder(false, options);
|
|
|
|
};
|
|
|
|
|
|
|
|
// 13.2 utf-16be
|
|
|
|
name_to_encoding['utf-16be'].getEncoder = function(options) {
|
|
|
|
return new UTF16Encoder(true, options);
|
|
|
|
};
|
|
|
|
name_to_encoding['utf-16be'].getDecoder = function(options) {
|
|
|
|
return new UTF16Decoder(true, options);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// NOTE: currently unused
|
|
|
|
/**
|
|
|
|
* @param {string} label The encoding label.
|
|
|
|
* @param {ByteInputStream} input_stream The byte stream to test.
|
|
|
|
*/
|
|
|
|
function detectEncoding(label, input_stream) {
|
|
|
|
if (input_stream.match([0xFF, 0xFE])) {
|
|
|
|
input_stream.offset(2);
|
|
|
|
return 'utf-16le';
|
|
|
|
}
|
|
|
|
if (input_stream.match([0xFE, 0xFF])) {
|
|
|
|
input_stream.offset(2);
|
|
|
|
return 'utf-16be';
|
|
|
|
}
|
|
|
|
if (input_stream.match([0xEF, 0xBB, 0xBF])) {
|
|
|
|
input_stream.offset(3);
|
|
|
|
return 'utf-8';
|
|
|
|
}
|
|
|
|
return label;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Implementation of Text Encoding Web API
|
|
|
|
//
|
|
|
|
|
|
|
|
/** @const */ var DEFAULT_ENCODING = 'utf-8';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @constructor
|
|
|
|
* @param {string=} opt_encoding The label of the encoding;
|
|
|
|
* defaults to 'utf-8'.
|
|
|
|
* @param {{fatal: boolean}=} options
|
|
|
|
*/
|
|
|
|
function TextEncoder(opt_encoding, options) {
|
|
|
|
if (!(this instanceof TextEncoder)) {
|
|
|
|
throw new TypeError('Constructor cannot be called as a function');
|
|
|
|
}
|
|
|
|
opt_encoding = opt_encoding ? String(opt_encoding) : DEFAULT_ENCODING;
|
|
|
|
options = Object(options);
|
|
|
|
/** @private */
|
|
|
|
this._encoding = getEncoding(opt_encoding);
|
|
|
|
if (this._encoding === null || (this._encoding.name !== 'utf-8' &&
|
|
|
|
this._encoding.name !== 'utf-16le' &&
|
|
|
|
this._encoding.name !== 'utf-16be'))
|
|
|
|
throw new TypeError('Unknown encoding: ' + opt_encoding);
|
|
|
|
/** @private @type {boolean} */
|
|
|
|
this._streaming = false;
|
|
|
|
/** @private */
|
|
|
|
this._encoder = null;
|
|
|
|
/** @private @type {{fatal: boolean}=} */
|
|
|
|
this._options = { fatal: Boolean(options.fatal) };
|
|
|
|
|
|
|
|
if (Object.defineProperty) {
|
|
|
|
Object.defineProperty(
|
|
|
|
this, 'encoding',
|
|
|
|
{ get: function() { return this._encoding.name; } });
|
|
|
|
} else {
|
|
|
|
this.encoding = this._encoding.name;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
TextEncoder.prototype = {
|
|
|
|
/**
|
|
|
|
* @param {string=} opt_string The string to encode.
|
|
|
|
* @param {{stream: boolean}=} options
|
|
|
|
*/
|
|
|
|
encode: function encode(opt_string, options) {
|
|
|
|
opt_string = opt_string ? String(opt_string) : '';
|
|
|
|
options = Object(options);
|
|
|
|
// TODO: any options?
|
|
|
|
if (!this._streaming) {
|
|
|
|
this._encoder = this._encoding.getEncoder(this._options);
|
|
|
|
}
|
|
|
|
this._streaming = Boolean(options.stream);
|
|
|
|
|
|
|
|
var bytes = [];
|
|
|
|
var output_stream = new ByteOutputStream(bytes);
|
|
|
|
var input_stream = new CodePointInputStream(opt_string);
|
|
|
|
while (input_stream.get() !== EOF_code_point) {
|
|
|
|
this._encoder.encode(output_stream, input_stream);
|
|
|
|
}
|
|
|
|
if (!this._streaming) {
|
|
|
|
var last_byte;
|
|
|
|
do {
|
|
|
|
last_byte = this._encoder.encode(output_stream, input_stream);
|
|
|
|
} while (last_byte !== EOF_byte);
|
|
|
|
this._encoder = null;
|
|
|
|
}
|
|
|
|
return new Uint8Array(bytes);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @constructor
|
|
|
|
* @param {string=} opt_encoding The label of the encoding;
|
|
|
|
* defaults to 'utf-8'.
|
|
|
|
* @param {{fatal: boolean}=} options
|
|
|
|
*/
|
|
|
|
function TextDecoder(opt_encoding, options) {
|
|
|
|
if (!(this instanceof TextDecoder)) {
|
|
|
|
throw new TypeError('Constructor cannot be called as a function');
|
|
|
|
}
|
|
|
|
opt_encoding = opt_encoding ? String(opt_encoding) : DEFAULT_ENCODING;
|
|
|
|
options = Object(options);
|
|
|
|
/** @private */
|
|
|
|
this._encoding = getEncoding(opt_encoding);
|
|
|
|
if (this._encoding === null)
|
|
|
|
throw new TypeError('Unknown encoding: ' + opt_encoding);
|
|
|
|
|
|
|
|
/** @private @type {boolean} */
|
|
|
|
this._streaming = false;
|
|
|
|
/** @private */
|
|
|
|
this._decoder = null;
|
|
|
|
/** @private @type {{fatal: boolean}=} */
|
|
|
|
this._options = { fatal: Boolean(options.fatal) };
|
|
|
|
|
|
|
|
if (Object.defineProperty) {
|
|
|
|
Object.defineProperty(
|
|
|
|
this, 'encoding',
|
|
|
|
{ get: function() { return this._encoding.name; } });
|
|
|
|
} else {
|
|
|
|
this.encoding = this._encoding.name;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Issue if input byte stream is offset by decoder
|
|
|
|
// TODO: BOM detection will not work if stream header spans multiple calls
|
|
|
|
// (last N bytes of previous stream may need to be retained?)
|
|
|
|
TextDecoder.prototype = {
|
|
|
|
/**
|
|
|
|
* @param {ArrayBufferView=} opt_view The buffer of bytes to decode.
|
|
|
|
* @param {{stream: boolean}=} options
|
|
|
|
*/
|
|
|
|
decode: function decode(opt_view, options) {
|
|
|
|
if (opt_view && !('buffer' in opt_view && 'byteOffset' in opt_view &&
|
|
|
|
'byteLength' in opt_view)) {
|
|
|
|
throw new TypeError('Expected ArrayBufferView');
|
|
|
|
} else if (!opt_view) {
|
|
|
|
opt_view = new Uint8Array(0);
|
|
|
|
}
|
|
|
|
options = Object(options);
|
|
|
|
|
|
|
|
if (!this._streaming) {
|
|
|
|
this._decoder = this._encoding.getDecoder(this._options);
|
|
|
|
this._BOMseen = false;
|
|
|
|
}
|
|
|
|
this._streaming = Boolean(options.stream);
|
|
|
|
|
|
|
|
var bytes = new Uint8Array(opt_view.buffer,
|
|
|
|
opt_view.byteOffset,
|
|
|
|
opt_view.byteLength);
|
|
|
|
var input_stream = new ByteInputStream(bytes);
|
|
|
|
|
|
|
|
var output_stream = new CodePointOutputStream(), code_point;
|
|
|
|
while (input_stream.get() !== EOF_byte) {
|
|
|
|
code_point = this._decoder.decode(input_stream);
|
|
|
|
if (code_point !== null && code_point !== EOF_code_point) {
|
|
|
|
output_stream.emit(code_point);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!this._streaming) {
|
|
|
|
do {
|
|
|
|
code_point = this._decoder.decode(input_stream);
|
|
|
|
if (code_point !== null && code_point !== EOF_code_point) {
|
|
|
|
output_stream.emit(code_point);
|
|
|
|
}
|
|
|
|
} while (code_point !== EOF_code_point &&
|
|
|
|
input_stream.get() != EOF_byte);
|
|
|
|
this._decoder = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
var result = output_stream.string();
|
|
|
|
if (!this._BOMseen && result.length) {
|
|
|
|
this._BOMseen = true;
|
|
|
|
if (['utf-8', 'utf-16le', 'utf-16be'].indexOf(this.encoding) !== -1 &&
|
|
|
|
result.charCodeAt(0) === 0xFEFF) {
|
|
|
|
result = result.substring(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
global['TextEncoder'] = global['TextEncoder'] || TextEncoder;
|
|
|
|
global['TextDecoder'] = global['TextDecoder'] || TextDecoder;
|
|
|
|
}(this));
|
|
|
|
|
|
|
|
define("encoding", function(){});
|
|
|
|
|
2013-07-12 15:11:05 +00:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
|
|
|
// https://github.com/joyent/node/blob/41e53e557992a7d552a8e23de035f9463da25c99/lib/path.js
|
|
|
|
|
|
|
|
define('src/path',['require'],function(require) {
|
|
|
|
|
|
|
|
// Split a filename into [root, dir, basename, ext], unix version
|
|
|
|
// 'root' is just a slash, or nothing.
|
|
|
|
var splitPathRe =
|
|
|
|
/^(\/?)([\s\S]+\/(?!$)|\/)?((?:\.{1,2}$|[\s\S]+?)?(\.[^.\/]*)?)$/;
|
|
|
|
var splitPath = function(filename) {
|
|
|
|
var result = splitPathRe.exec(filename);
|
|
|
|
return [result[1] || '', result[2] || '', result[3] || '', result[4] || ''];
|
|
|
|
};
|
|
|
|
|
|
|
|
// resolves . and .. elements in a path array with directory names there
|
|
|
|
// must be no slashes, empty elements, or device names (c:\) in the array
|
|
|
|
// (so also no leading and trailing slashes - it does not distinguish
|
|
|
|
// relative and absolute paths)
|
|
|
|
function normalizeArray(parts, allowAboveRoot) {
|
|
|
|
// if the path tries to go above the root, `up` ends up > 0
|
|
|
|
var up = 0;
|
|
|
|
for (var i = parts.length - 1; i >= 0; i--) {
|
|
|
|
var last = parts[i];
|
|
|
|
if (last === '.') {
|
|
|
|
parts.splice(i, 1);
|
|
|
|
} else if (last === '..') {
|
|
|
|
parts.splice(i, 1);
|
|
|
|
up++;
|
|
|
|
} else if (up) {
|
|
|
|
parts.splice(i, 1);
|
|
|
|
up--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the path is allowed to go above the root, restore leading ..s
|
|
|
|
if (allowAboveRoot) {
|
|
|
|
for (; up--; up) {
|
|
|
|
parts.unshift('..');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return parts;
|
|
|
|
}
|
|
|
|
|
|
|
|
// path.normalize(path)
|
|
|
|
// posix version
|
|
|
|
function normalize(path) {
|
|
|
|
var isAbsolute = path.charAt(0) === '/';
|
|
|
|
// var trailingSlash = path.substr(-1) === '/';
|
|
|
|
|
|
|
|
// Normalize the path
|
|
|
|
path = normalizeArray(path.split('/').filter(function(p) {
|
|
|
|
return !!p;
|
|
|
|
}), !isAbsolute).join('/');
|
|
|
|
|
|
|
|
if (!path && !isAbsolute) {
|
|
|
|
path = '.';
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
if (path && trailingSlash) {
|
|
|
|
path += '/';
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
return (isAbsolute ? '/' : '') + path;
|
|
|
|
};
|
|
|
|
|
|
|
|
function dirname(path) {
|
|
|
|
var result = splitPath(path),
|
|
|
|
root = result[0],
|
|
|
|
dir = result[1];
|
|
|
|
|
|
|
|
if (!root && !dir) {
|
|
|
|
// No dirname whatsoever
|
|
|
|
return '.';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dir) {
|
|
|
|
// It has a dirname, strip trailing slash
|
|
|
|
dir = dir.substr(0, dir.length - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return root + dir;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
function basename(path, ext) {
|
|
|
|
var f = splitPath(path)[2];
|
|
|
|
// TODO: make this comparison case-insensitive on windows?
|
|
|
|
if (ext && f.substr(-1 * ext.length) === ext) {
|
|
|
|
f = f.substr(0, f.length - ext.length);
|
|
|
|
}
|
|
|
|
return f === "" ? "/" : f;
|
|
|
|
};
|
|
|
|
|
|
|
|
function isAbsolute(path) {
|
|
|
|
return path.charAt(0) === '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
normalize: normalize,
|
|
|
|
dirname: dirname,
|
|
|
|
basename: basename,
|
|
|
|
isAbsolute: isAbsolute
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
2013-05-31 01:44:32 +00:00
|
|
|
/*
|
|
|
|
CryptoJS v3.0.2
|
|
|
|
code.google.com/p/crypto-js
|
|
|
|
(c) 2009-2012 by Jeff Mott. All rights reserved.
|
|
|
|
code.google.com/p/crypto-js/wiki/License
|
|
|
|
*/
|
|
|
|
var CryptoJS=CryptoJS||function(i,p){var f={},q=f.lib={},j=q.Base=function(){function a(){}return{extend:function(h){a.prototype=this;var d=new a;h&&d.mixIn(h);d.$super=this;return d},create:function(){var a=this.extend();a.init.apply(a,arguments);return a},init:function(){},mixIn:function(a){for(var d in a)a.hasOwnProperty(d)&&(this[d]=a[d]);a.hasOwnProperty("toString")&&(this.toString=a.toString)},clone:function(){return this.$super.extend(this)}}}(),k=q.WordArray=j.extend({init:function(a,h){a=
|
|
|
|
this.words=a||[];this.sigBytes=h!=p?h:4*a.length},toString:function(a){return(a||m).stringify(this)},concat:function(a){var h=this.words,d=a.words,c=this.sigBytes,a=a.sigBytes;this.clamp();if(c%4)for(var b=0;b<a;b++)h[c+b>>>2]|=(d[b>>>2]>>>24-8*(b%4)&255)<<24-8*((c+b)%4);else if(65535<d.length)for(b=0;b<a;b+=4)h[c+b>>>2]=d[b>>>2];else h.push.apply(h,d);this.sigBytes+=a;return this},clamp:function(){var a=this.words,b=this.sigBytes;a[b>>>2]&=4294967295<<32-8*(b%4);a.length=i.ceil(b/4)},clone:function(){var a=
|
|
|
|
j.clone.call(this);a.words=this.words.slice(0);return a},random:function(a){for(var b=[],d=0;d<a;d+=4)b.push(4294967296*i.random()|0);return k.create(b,a)}}),r=f.enc={},m=r.Hex={stringify:function(a){for(var b=a.words,a=a.sigBytes,d=[],c=0;c<a;c++){var e=b[c>>>2]>>>24-8*(c%4)&255;d.push((e>>>4).toString(16));d.push((e&15).toString(16))}return d.join("")},parse:function(a){for(var b=a.length,d=[],c=0;c<b;c+=2)d[c>>>3]|=parseInt(a.substr(c,2),16)<<24-4*(c%8);return k.create(d,b/2)}},s=r.Latin1={stringify:function(a){for(var b=
|
|
|
|
a.words,a=a.sigBytes,d=[],c=0;c<a;c++)d.push(String.fromCharCode(b[c>>>2]>>>24-8*(c%4)&255));return d.join("")},parse:function(a){for(var b=a.length,d=[],c=0;c<b;c++)d[c>>>2]|=(a.charCodeAt(c)&255)<<24-8*(c%4);return k.create(d,b)}},g=r.Utf8={stringify:function(a){try{return decodeURIComponent(escape(s.stringify(a)))}catch(b){throw Error("Malformed UTF-8 data");}},parse:function(a){return s.parse(unescape(encodeURIComponent(a)))}},b=q.BufferedBlockAlgorithm=j.extend({reset:function(){this._data=k.create();
|
|
|
|
this._nDataBytes=0},_append:function(a){"string"==typeof a&&(a=g.parse(a));this._data.concat(a);this._nDataBytes+=a.sigBytes},_process:function(a){var b=this._data,d=b.words,c=b.sigBytes,e=this.blockSize,f=c/(4*e),f=a?i.ceil(f):i.max((f|0)-this._minBufferSize,0),a=f*e,c=i.min(4*a,c);if(a){for(var g=0;g<a;g+=e)this._doProcessBlock(d,g);g=d.splice(0,a);b.sigBytes-=c}return k.create(g,c)},clone:function(){var a=j.clone.call(this);a._data=this._data.clone();return a},_minBufferSize:0});q.Hasher=b.extend({init:function(){this.reset()},
|
|
|
|
reset:function(){b.reset.call(this);this._doReset()},update:function(a){this._append(a);this._process();return this},finalize:function(a){a&&this._append(a);this._doFinalize();return this._hash},clone:function(){var a=b.clone.call(this);a._hash=this._hash.clone();return a},blockSize:16,_createHelper:function(a){return function(b,d){return a.create(d).finalize(b)}},_createHmacHelper:function(a){return function(b,d){return e.HMAC.create(a,d).finalize(b)}}});var e=f.algo={};return f}(Math);
|
|
|
|
(function(i){var p=CryptoJS,f=p.lib,q=f.WordArray,f=f.Hasher,j=p.algo,k=[],r=[];(function(){function f(a){for(var b=i.sqrt(a),d=2;d<=b;d++)if(!(a%d))return!1;return!0}function g(a){return 4294967296*(a-(a|0))|0}for(var b=2,e=0;64>e;)f(b)&&(8>e&&(k[e]=g(i.pow(b,0.5))),r[e]=g(i.pow(b,1/3)),e++),b++})();var m=[],j=j.SHA256=f.extend({_doReset:function(){this._hash=q.create(k.slice(0))},_doProcessBlock:function(f,g){for(var b=this._hash.words,e=b[0],a=b[1],h=b[2],d=b[3],c=b[4],i=b[5],j=b[6],k=b[7],l=0;64>
|
|
|
|
l;l++){if(16>l)m[l]=f[g+l]|0;else{var n=m[l-15],o=m[l-2];m[l]=((n<<25|n>>>7)^(n<<14|n>>>18)^n>>>3)+m[l-7]+((o<<15|o>>>17)^(o<<13|o>>>19)^o>>>10)+m[l-16]}n=k+((c<<26|c>>>6)^(c<<21|c>>>11)^(c<<7|c>>>25))+(c&i^~c&j)+r[l]+m[l];o=((e<<30|e>>>2)^(e<<19|e>>>13)^(e<<10|e>>>22))+(e&a^e&h^a&h);k=j;j=i;i=c;c=d+n|0;d=h;h=a;a=e;e=n+o|0}b[0]=b[0]+e|0;b[1]=b[1]+a|0;b[2]=b[2]+h|0;b[3]=b[3]+d|0;b[4]=b[4]+c|0;b[5]=b[5]+i|0;b[6]=b[6]+j|0;b[7]=b[7]+k|0},_doFinalize:function(){var f=this._data,g=f.words,b=8*this._nDataBytes,
|
|
|
|
e=8*f.sigBytes;g[e>>>5]|=128<<24-e%32;g[(e+64>>>9<<4)+15]=b;f.sigBytes=4*g.length;this._process()}});p.SHA256=f._createHelper(j);p.HmacSHA256=f._createHmacHelper(j)})(Math);
|
2013-03-11 19:14:26 +00:00
|
|
|
|
2013-05-31 01:44:32 +00:00
|
|
|
define("crypto-js/rollups/sha256", function(){});
|
2013-03-11 19:14:26 +00:00
|
|
|
|
2013-05-31 01:44:32 +00:00
|
|
|
define('src/shared',['require','crypto-js/rollups/sha256'],function(require) {
|
2013-03-11 19:14:26 +00:00
|
|
|
|
2013-05-31 01:44:32 +00:00
|
|
|
require("crypto-js/rollups/sha256"); var Crypto = CryptoJS;
|
2013-03-11 19:14:26 +00:00
|
|
|
|
|
|
|
function guid() {
|
|
|
|
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
|
|
|
|
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
|
|
|
|
return v.toString(16);
|
|
|
|
}).toUpperCase();
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-03-11 19:14:26 +00:00
|
|
|
|
2013-05-31 01:44:32 +00:00
|
|
|
function hash(string) {
|
|
|
|
return Crypto.SHA256(string).toString(Crypto.enc.hex);
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-03-11 19:14:26 +00:00
|
|
|
|
2013-11-08 20:33:44 +00:00
|
|
|
function nop() {}
|
2013-03-11 19:14:26 +00:00
|
|
|
|
2013-05-31 01:44:32 +00:00
|
|
|
return {
|
|
|
|
guid: guid,
|
|
|
|
hash: hash,
|
2013-11-08 20:33:44 +00:00
|
|
|
nop: nop
|
2013-05-31 01:44:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
});
|
2013-11-08 20:33:44 +00:00
|
|
|
|
2013-03-11 19:14:26 +00:00
|
|
|
/*
|
|
|
|
Copyright (c) 2012, Alan Kligman
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
|
|
|
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
|
|
|
Neither the name of the Mozilla Foundation nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
define('src/error',['require'],function(require) {
|
|
|
|
//
|
|
|
|
|
2013-07-12 15:11:05 +00:00
|
|
|
function EExists(message){
|
|
|
|
this.message = message || '';
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-07-12 15:11:05 +00:00
|
|
|
EExists.prototype = new Error();
|
|
|
|
EExists.prototype.name = "EExists";
|
|
|
|
EExists.prototype.constructor = EExists;
|
2013-03-11 19:14:26 +00:00
|
|
|
|
2013-07-12 15:11:05 +00:00
|
|
|
function EIsDirectory(message){
|
|
|
|
this.message = message || '';
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-03-11 19:14:26 +00:00
|
|
|
EIsDirectory.prototype = new Error();
|
|
|
|
EIsDirectory.prototype.name = "EIsDirectory";
|
|
|
|
EIsDirectory.prototype.constructor = EIsDirectory;
|
|
|
|
|
2013-07-12 15:11:05 +00:00
|
|
|
function ENoEntry(message){
|
|
|
|
this.message = message || '';
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-03-11 19:14:26 +00:00
|
|
|
ENoEntry.prototype = new Error();
|
|
|
|
ENoEntry.prototype.name = "ENoEntry";
|
|
|
|
ENoEntry.prototype.constructor = ENoEntry;
|
|
|
|
|
2013-07-12 15:11:05 +00:00
|
|
|
function EBusy(message){
|
|
|
|
this.message = message || '';
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-03-11 19:14:26 +00:00
|
|
|
EBusy.prototype = new Error();
|
|
|
|
EBusy.prototype.name = "EBusy";
|
|
|
|
EBusy.prototype.constructor = EBusy;
|
|
|
|
|
2013-07-12 15:11:05 +00:00
|
|
|
function ENotEmpty(message){
|
|
|
|
this.message = message || '';
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-03-11 19:14:26 +00:00
|
|
|
ENotEmpty.prototype = new Error();
|
|
|
|
ENotEmpty.prototype.name = "ENotEmpty";
|
|
|
|
ENotEmpty.prototype.constructor = ENotEmpty;
|
|
|
|
|
2013-07-12 15:11:05 +00:00
|
|
|
function ENotDirectory(message){
|
|
|
|
this.message = message || '';
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-03-11 19:14:26 +00:00
|
|
|
ENotDirectory.prototype = new Error();
|
2013-07-12 15:11:05 +00:00
|
|
|
ENotDirectory.prototype.name = "ENotDirectory";
|
2013-03-11 19:14:26 +00:00
|
|
|
ENotDirectory.prototype.constructor = ENotDirectory;
|
|
|
|
|
2013-07-12 15:11:05 +00:00
|
|
|
function EBadFileDescriptor(message){
|
|
|
|
this.message = message || '';
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-03-11 19:14:26 +00:00
|
|
|
EBadFileDescriptor.prototype = new Error();
|
|
|
|
EBadFileDescriptor.prototype.name = "EBadFileDescriptor";
|
|
|
|
EBadFileDescriptor.prototype.constructor = EBadFileDescriptor;
|
|
|
|
|
2013-07-12 15:11:05 +00:00
|
|
|
function ENotImplemented(message){
|
|
|
|
this.message = message || '';
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-03-11 19:14:26 +00:00
|
|
|
ENotImplemented.prototype = new Error();
|
|
|
|
ENotImplemented.prototype.name = "ENotImplemented";
|
|
|
|
ENotImplemented.prototype.constructor = ENotImplemented;
|
|
|
|
|
2013-07-12 15:11:05 +00:00
|
|
|
function ENotMounted(message){
|
|
|
|
this.message = message || '';
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-03-11 19:14:26 +00:00
|
|
|
ENotMounted.prototype = new Error();
|
|
|
|
ENotMounted.prototype.name = "ENotMounted";
|
|
|
|
ENotMounted.prototype.constructor = ENotMounted;
|
|
|
|
|
2013-07-12 15:11:05 +00:00
|
|
|
function EInvalid(message){
|
|
|
|
this.message = message || '';
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-07-12 15:11:05 +00:00
|
|
|
EInvalid.prototype = new Error();
|
|
|
|
EInvalid.prototype.name = "EInvalid";
|
|
|
|
EInvalid.prototype.constructor = EInvalid;
|
|
|
|
|
|
|
|
function EIO(message){
|
|
|
|
this.message = message || '';
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-07-12 15:11:05 +00:00
|
|
|
EIO.prototype = new Error();
|
|
|
|
EIO.prototype.name = "EIO";
|
|
|
|
EIO.prototype.constructor = EIO;
|
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
function ELoop(message){
|
|
|
|
this.message = message || '';
|
|
|
|
}
|
|
|
|
ELoop.prototype = new Error();
|
|
|
|
ELoop.prototype.name = "ELoop";
|
|
|
|
ELoop.prototype.constructor = ELoop;
|
|
|
|
|
2013-07-12 15:11:05 +00:00
|
|
|
function EFileSystemError(message){
|
|
|
|
this.message = message || '';
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-07-12 15:11:05 +00:00
|
|
|
EFileSystemError.prototype = new Error();
|
|
|
|
EFileSystemError.prototype.name = "EFileSystemError";
|
|
|
|
EFileSystemError.prototype.constructor = EFileSystemError;
|
2013-03-11 19:14:26 +00:00
|
|
|
|
|
|
|
return {
|
2013-07-12 15:11:05 +00:00
|
|
|
EExists: EExists,
|
2013-03-11 19:14:26 +00:00
|
|
|
EIsDirectory: EIsDirectory,
|
|
|
|
ENoEntry: ENoEntry,
|
|
|
|
EBusy: EBusy,
|
|
|
|
ENotEmpty: ENotEmpty,
|
|
|
|
ENotDirectory: ENotDirectory,
|
|
|
|
EBadFileDescriptor: EBadFileDescriptor,
|
|
|
|
ENotImplemented: ENotImplemented,
|
|
|
|
ENotMounted: ENotMounted,
|
2013-07-12 15:11:05 +00:00
|
|
|
EInvalid: EInvalid,
|
2013-11-22 21:12:31 +00:00
|
|
|
EIO: EIO,
|
|
|
|
ELoop: ELoop
|
2013-03-11 19:14:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
});
|
2013-11-08 20:33:44 +00:00
|
|
|
|
2013-05-31 01:44:32 +00:00
|
|
|
define('src/constants',['require'],function(require) {
|
2013-03-11 19:14:26 +00:00
|
|
|
|
2013-07-12 15:11:05 +00:00
|
|
|
var O_READ = 'READ';
|
|
|
|
var O_WRITE = 'WRITE';
|
|
|
|
var O_CREATE = 'CREATE';
|
|
|
|
var O_EXCLUSIVE = 'EXCLUSIVE';
|
|
|
|
var O_TRUNCATE = 'TRUNCATE';
|
|
|
|
var O_APPEND = 'APPEND';
|
|
|
|
|
2013-05-31 01:44:32 +00:00
|
|
|
return {
|
2013-11-27 00:12:25 +00:00
|
|
|
FILE_SYSTEM_NAME: 'local',
|
|
|
|
|
2013-05-31 01:44:32 +00:00
|
|
|
FILE_STORE_NAME: 'files',
|
2013-03-11 19:14:26 +00:00
|
|
|
|
2013-05-31 01:44:32 +00:00
|
|
|
IDB_RO: 'readonly',
|
|
|
|
IDB_RW: 'readwrite',
|
2013-03-11 19:14:26 +00:00
|
|
|
|
2013-05-31 01:44:32 +00:00
|
|
|
MODE_FILE: 'FILE',
|
|
|
|
MODE_DIRECTORY: 'DIRECTORY',
|
|
|
|
MODE_SYMBOLIC_LINK: 'SYMLINK',
|
2013-03-11 19:14:26 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
SYMLOOP_MAX: 10,
|
|
|
|
|
2013-05-31 01:44:32 +00:00
|
|
|
BINARY_MIME_TYPE: 'application/octet-stream',
|
|
|
|
JSON_MIME_TYPE: 'application/json',
|
2013-03-11 19:14:26 +00:00
|
|
|
|
2013-05-31 01:44:32 +00:00
|
|
|
ROOT_DIRECTORY_NAME: '/', // basename(normalize(path))
|
|
|
|
ROOT_NODE_ID: '8a5edab282632443219e051e4ade2d1d5bbc671c781051bf1437897cbdfea0f1', // sha256(ROOT_DIRECTORY_NAME)
|
2013-03-11 19:14:26 +00:00
|
|
|
|
2013-05-31 01:44:32 +00:00
|
|
|
FS_FORMAT: 'FORMAT',
|
2013-03-11 19:14:26 +00:00
|
|
|
|
2013-07-12 15:11:05 +00:00
|
|
|
O_READ: O_READ,
|
|
|
|
O_WRITE: O_WRITE,
|
|
|
|
O_CREATE: O_CREATE,
|
|
|
|
O_EXCLUSIVE: O_EXCLUSIVE,
|
|
|
|
O_TRUNCATE: O_TRUNCATE,
|
|
|
|
O_APPEND: O_APPEND,
|
|
|
|
|
|
|
|
O_FLAGS: {
|
|
|
|
'r': [O_READ],
|
|
|
|
'r+': [O_READ, O_WRITE],
|
|
|
|
'w': [O_WRITE, O_CREATE, O_TRUNCATE],
|
|
|
|
'w+': [O_WRITE, O_READ, O_CREATE, O_TRUNCATE],
|
|
|
|
'wx': [O_WRITE, O_CREATE, O_EXCLUSIVE, O_TRUNCATE],
|
|
|
|
'wx+': [O_WRITE, O_READ, O_CREATE, O_EXCLUSIVE, O_TRUNCATE],
|
|
|
|
'a': [O_WRITE, O_CREATE, O_APPEND],
|
|
|
|
'a+': [O_WRITE, O_READ, O_CREATE, O_APPEND],
|
|
|
|
'ax': [O_WRITE, O_CREATE, O_EXCLUSIVE, O_APPEND],
|
2013-11-22 21:12:31 +00:00
|
|
|
'ax+': [O_WRITE, O_READ, O_CREATE, O_EXCLUSIVE, O_APPEND]
|
2013-07-12 15:11:05 +00:00
|
|
|
},
|
2013-03-11 19:14:26 +00:00
|
|
|
|
2013-05-31 01:44:32 +00:00
|
|
|
FS_READY: 'READY',
|
|
|
|
FS_PENDING: 'PENDING',
|
2013-11-09 19:51:21 +00:00
|
|
|
FS_ERROR: 'ERROR'
|
2013-05-31 01:44:32 +00:00
|
|
|
};
|
2013-03-11 19:14:26 +00:00
|
|
|
|
2013-05-31 01:44:32 +00:00
|
|
|
});
|
2013-11-27 00:12:25 +00:00
|
|
|
define('src/providers/indexeddb',['require','src/constants','src/constants','src/constants','src/constants'],function(require) {
|
|
|
|
var FILE_SYSTEM_NAME = require('src/constants').FILE_SYSTEM_NAME;
|
2013-11-22 21:12:31 +00:00
|
|
|
var FILE_STORE_NAME = require('src/constants').FILE_STORE_NAME;
|
|
|
|
|
|
|
|
var indexedDB = window.indexedDB ||
|
|
|
|
window.mozIndexedDB ||
|
|
|
|
window.webkitIndexedDB ||
|
|
|
|
window.msIndexedDB;
|
|
|
|
|
|
|
|
|
|
|
|
var IDB_RW = require('src/constants').IDB_RW;
|
|
|
|
var IDB_RO = require('src/constants').IDB_RO;
|
|
|
|
|
|
|
|
function IndexedDBContext(db, mode) {
|
|
|
|
var transaction = db.transaction(FILE_STORE_NAME, mode);
|
|
|
|
this.objectStore = transaction.objectStore(FILE_STORE_NAME);
|
|
|
|
}
|
|
|
|
IndexedDBContext.prototype.clear = function(callback) {
|
|
|
|
try {
|
|
|
|
var request = this.objectStore.clear();
|
|
|
|
request.onsuccess = function(event) {
|
|
|
|
callback();
|
|
|
|
};
|
|
|
|
request.onerror = function(error) {
|
|
|
|
callback(error);
|
|
|
|
};
|
|
|
|
} catch(e) {
|
|
|
|
callback(e);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
IndexedDBContext.prototype.get = function(key, callback) {
|
|
|
|
try {
|
|
|
|
var request = this.objectStore.get(key);
|
|
|
|
request.onsuccess = function onsuccess(event) {
|
|
|
|
var result = event.target.result;
|
|
|
|
callback(null, result);
|
|
|
|
};
|
|
|
|
request.onerror = function onerror(error) {
|
|
|
|
callback(error);
|
|
|
|
};
|
|
|
|
} catch(e) {
|
|
|
|
callback(e);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
IndexedDBContext.prototype.put = function(key, value, callback) {
|
|
|
|
try {
|
|
|
|
var request = this.objectStore.put(value, key);
|
|
|
|
request.onsuccess = function onsuccess(event) {
|
|
|
|
var result = event.target.result;
|
|
|
|
callback(null, result);
|
|
|
|
};
|
|
|
|
request.onerror = function onerror(error) {
|
|
|
|
callback(error);
|
|
|
|
};
|
|
|
|
} catch(e) {
|
|
|
|
callback(e);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
IndexedDBContext.prototype.delete = function(key, callback) {
|
|
|
|
try {
|
|
|
|
var request = this.objectStore.delete(key);
|
|
|
|
request.onsuccess = function onsuccess(event) {
|
|
|
|
var result = event.target.result;
|
|
|
|
callback(null, result);
|
|
|
|
};
|
|
|
|
request.onerror = function(error) {
|
|
|
|
callback(error);
|
|
|
|
};
|
|
|
|
} catch(e) {
|
|
|
|
callback(e);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
function IndexedDB(name) {
|
2013-11-27 00:12:25 +00:00
|
|
|
this.name = name || FILE_SYSTEM_NAME;
|
2013-11-22 21:12:31 +00:00
|
|
|
this.db = null;
|
|
|
|
}
|
|
|
|
IndexedDB.isSupported = function() {
|
|
|
|
return !!indexedDB;
|
|
|
|
};
|
|
|
|
|
|
|
|
IndexedDB.prototype.open = function(callback) {
|
|
|
|
var that = this;
|
|
|
|
|
|
|
|
// Bail if we already have a db open
|
|
|
|
if( that.db ) {
|
|
|
|
callback(null, false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keep track of whether we're accessing this db for the first time
|
|
|
|
// and therefore needs to get formatted.
|
|
|
|
var firstAccess = false;
|
|
|
|
|
|
|
|
// NOTE: we're not using versioned databases.
|
|
|
|
var openRequest = indexedDB.open(that.name);
|
|
|
|
|
|
|
|
// If the db doesn't exist, we'll create it
|
|
|
|
openRequest.onupgradeneeded = function onupgradeneeded(event) {
|
|
|
|
var db = event.target.result;
|
|
|
|
|
|
|
|
if(db.objectStoreNames.contains(FILE_STORE_NAME)) {
|
|
|
|
db.deleteObjectStore(FILE_STORE_NAME);
|
|
|
|
}
|
|
|
|
db.createObjectStore(FILE_STORE_NAME);
|
|
|
|
|
|
|
|
firstAccess = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
openRequest.onsuccess = function onsuccess(event) {
|
|
|
|
that.db = event.target.result;
|
|
|
|
callback(null, firstAccess);
|
|
|
|
};
|
|
|
|
openRequest.onerror = function onerror(error) {
|
|
|
|
callback(error);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
IndexedDB.prototype.getReadOnlyContext = function() {
|
|
|
|
return new IndexedDBContext(this.db, IDB_RO);
|
|
|
|
};
|
|
|
|
IndexedDB.prototype.getReadWriteContext = function() {
|
|
|
|
return new IndexedDBContext(this.db, IDB_RW);
|
|
|
|
};
|
|
|
|
|
|
|
|
return IndexedDB;
|
|
|
|
});
|
|
|
|
|
2013-11-27 00:12:25 +00:00
|
|
|
define('src/providers/memory',['require','src/constants'],function(require) {
|
|
|
|
var FILE_SYSTEM_NAME = require('src/constants').FILE_SYSTEM_NAME;
|
2013-11-22 21:12:31 +00:00
|
|
|
|
|
|
|
function MemoryContext(db, readOnly) {
|
|
|
|
this.readOnly = readOnly;
|
|
|
|
this.objectStore = db;
|
|
|
|
}
|
|
|
|
MemoryContext.prototype.clear = function(callback) {
|
|
|
|
if(this.readOnly) {
|
|
|
|
return callback("[MemoryContext] Error: write operation on read only context");
|
|
|
|
}
|
|
|
|
var objectStore = this.objectStore;
|
|
|
|
Object.keys(objectStore).forEach(function(key){
|
|
|
|
delete objectStore[key];
|
|
|
|
});
|
|
|
|
callback(null);
|
|
|
|
};
|
|
|
|
MemoryContext.prototype.get = function(key, callback) {
|
|
|
|
callback(null, this.objectStore[key]);
|
|
|
|
};
|
|
|
|
MemoryContext.prototype.put = function(key, value, callback) {
|
|
|
|
if(this.readOnly) {
|
|
|
|
return callback("[MemoryContext] Error: write operation on read only context");
|
|
|
|
}
|
|
|
|
this.objectStore[key] = value;
|
|
|
|
callback(null);
|
|
|
|
};
|
|
|
|
MemoryContext.prototype.delete = function(key, callback) {
|
|
|
|
if(this.readOnly) {
|
|
|
|
return callback("[MemoryContext] Error: write operation on read only context");
|
|
|
|
}
|
|
|
|
delete this.objectStore[key];
|
|
|
|
callback(null);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
function Memory(name) {
|
2013-11-27 00:12:25 +00:00
|
|
|
this.name = name || FILE_SYSTEM_NAME;
|
2013-11-22 21:12:31 +00:00
|
|
|
this.db = {};
|
|
|
|
}
|
|
|
|
Memory.isSupported = function() {
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
Memory.prototype.open = function(callback) {
|
|
|
|
callback(null, true);
|
|
|
|
};
|
|
|
|
Memory.prototype.getReadOnlyContext = function() {
|
|
|
|
return new MemoryContext(this.db, true);
|
|
|
|
};
|
|
|
|
Memory.prototype.getReadWriteContext = function() {
|
|
|
|
return new MemoryContext(this.db, false);
|
|
|
|
};
|
|
|
|
|
|
|
|
return Memory;
|
|
|
|
});
|
|
|
|
|
|
|
|
define('src/providers/providers',['require','src/providers/indexeddb','src/providers/memory','src/providers/indexeddb'],function(require) {
|
|
|
|
return {
|
|
|
|
IndexedDB: require('src/providers/indexeddb'),
|
|
|
|
Memory: require('src/providers/memory'),
|
|
|
|
Default: require('src/providers/indexeddb')
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2013-11-27 00:12:25 +00:00
|
|
|
define('src/fs',['require','lodash','encoding-indexes','encoding','src/path','src/path','src/path','src/shared','src/shared','src/shared','src/error','src/error','src/error','src/error','src/error','src/error','src/error','src/error','src/error','src/error','src/error','src/error','src/error','src/constants','src/constants','src/constants','src/constants','src/constants','src/constants','src/constants','src/constants','src/constants','src/constants','src/constants','src/constants','src/constants','src/constants','src/constants','src/constants','src/constants','src/constants','src/providers/providers'],function(require) {
|
2013-05-31 01:44:32 +00:00
|
|
|
|
2013-07-12 15:11:05 +00:00
|
|
|
var _ = require('lodash');
|
2013-05-31 01:44:32 +00:00
|
|
|
|
2013-11-08 20:33:44 +00:00
|
|
|
// TextEncoder and TextDecoder will either already be present, or use this shim.
|
|
|
|
// Because of the way the spec is defined, we need to get them off the global.
|
|
|
|
require('encoding-indexes');
|
|
|
|
require('encoding');
|
|
|
|
|
2013-07-12 15:11:05 +00:00
|
|
|
var normalize = require('src/path').normalize;
|
|
|
|
var dirname = require('src/path').dirname;
|
|
|
|
var basename = require('src/path').basename;
|
2013-03-11 19:14:26 +00:00
|
|
|
|
2013-07-12 15:11:05 +00:00
|
|
|
var guid = require('src/shared').guid;
|
|
|
|
var hash = require('src/shared').hash;
|
|
|
|
var nop = require('src/shared').nop;
|
2013-03-11 19:14:26 +00:00
|
|
|
|
2013-07-12 15:11:05 +00:00
|
|
|
var EExists = require('src/error').EExists;
|
|
|
|
var EIsDirectory = require('src/error').EIsDirectory;
|
|
|
|
var ENoEntry = require('src/error').ENoEntry;
|
|
|
|
var EBusy = require('src/error').EBusy;
|
|
|
|
var ENotEmpty = require('src/error').ENotEmpty;
|
|
|
|
var ENotDirectory = require('src/error').ENotDirectory;
|
|
|
|
var EBadFileDescriptor = require('src/error').EBadFileDescriptor;
|
|
|
|
var ENotImplemented = require('src/error').ENotImplemented;
|
|
|
|
var ENotMounted = require('src/error').ENotMounted;
|
|
|
|
var EInvalid = require('src/error').EInvalid;
|
|
|
|
var EIO = require('src/error').EIO;
|
2013-11-22 21:12:31 +00:00
|
|
|
var ELoop = require('src/error').ELoop;
|
2013-07-12 15:11:05 +00:00
|
|
|
var EFileSystemError = require('src/error').EFileSystemError;
|
2013-03-11 19:14:26 +00:00
|
|
|
|
2013-11-27 00:12:25 +00:00
|
|
|
var FILE_SYSTEM_NAME = require('src/constants').FILE_SYSTEM_NAME;
|
2013-07-12 15:11:05 +00:00
|
|
|
var FS_FORMAT = require('src/constants').FS_FORMAT;
|
|
|
|
var MODE_FILE = require('src/constants').MODE_FILE;
|
|
|
|
var MODE_DIRECTORY = require('src/constants').MODE_DIRECTORY;
|
2013-11-22 21:12:31 +00:00
|
|
|
var MODE_SYMBOLIC_LINK = require('src/constants').MODE_SYMBOLIC_LINK;
|
2013-07-12 15:11:05 +00:00
|
|
|
var ROOT_DIRECTORY_NAME = require('src/constants').ROOT_DIRECTORY_NAME;
|
|
|
|
var ROOT_NODE_ID = require('src/constants').ROOT_NODE_ID;
|
2013-11-22 21:12:31 +00:00
|
|
|
var SYMLOOP_MAX = require('src/constants').SYMLOOP_MAX;
|
2013-07-12 15:11:05 +00:00
|
|
|
var FS_READY = require('src/constants').FS_READY;
|
|
|
|
var FS_PENDING = require('src/constants').FS_PENDING;
|
|
|
|
var FS_ERROR = require('src/constants').FS_ERROR;
|
|
|
|
var O_READ = require('src/constants').O_READ;
|
|
|
|
var O_WRITE = require('src/constants').O_WRITE;
|
|
|
|
var O_CREATE = require('src/constants').O_CREATE;
|
|
|
|
var O_EXCLUSIVE = require('src/constants').O_EXCLUSIVE;
|
|
|
|
var O_TRUNCATE = require('src/constants').O_TRUNCATE;
|
|
|
|
var O_APPEND = require('src/constants').O_APPEND;
|
|
|
|
var O_FLAGS = require('src/constants').O_FLAGS;
|
2013-03-11 19:14:26 +00:00
|
|
|
|
2013-11-27 00:12:25 +00:00
|
|
|
var providers = require('src/providers/providers');
|
2013-11-22 21:12:31 +00:00
|
|
|
|
2013-07-12 15:11:05 +00:00
|
|
|
/*
|
|
|
|
* DirectoryEntry
|
|
|
|
*/
|
2013-05-31 01:44:32 +00:00
|
|
|
|
2013-07-12 15:11:05 +00:00
|
|
|
function DirectoryEntry(id, type) {
|
|
|
|
this.id = id;
|
|
|
|
this.type = type || MODE_FILE;
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-03-11 19:14:26 +00:00
|
|
|
|
2013-07-12 15:11:05 +00:00
|
|
|
/*
|
|
|
|
* OpenFileDescription
|
|
|
|
*/
|
2013-05-31 01:44:32 +00:00
|
|
|
|
2013-07-12 15:11:05 +00:00
|
|
|
function OpenFileDescription(id, flags, position) {
|
|
|
|
this.id = id;
|
|
|
|
this.flags = flags;
|
|
|
|
this.position = position;
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-05-31 01:44:32 +00:00
|
|
|
|
2013-07-12 15:11:05 +00:00
|
|
|
/*
|
|
|
|
* Node
|
|
|
|
*/
|
2013-03-11 19:14:26 +00:00
|
|
|
|
2013-07-12 15:11:05 +00:00
|
|
|
function Node(id, mode, size, atime, ctime, mtime, flags, xattrs, nlinks, version) {
|
|
|
|
var now = Date.now();
|
2013-05-31 01:44:32 +00:00
|
|
|
|
2013-11-08 20:33:44 +00:00
|
|
|
this.id = id || hash(guid());
|
2013-07-12 15:11:05 +00:00
|
|
|
this.mode = mode || MODE_FILE; // node type (file, directory, etc)
|
|
|
|
this.size = size || 0; // size (bytes for files, entries for directories)
|
|
|
|
this.atime = atime || now; // access time
|
|
|
|
this.ctime = ctime || now; // creation time
|
|
|
|
this.mtime = mtime || now; // modified time
|
|
|
|
this.flags = flags || []; // file flags
|
|
|
|
this.xattrs = xattrs || {}; // extended attributes
|
|
|
|
this.nlinks = nlinks || 0; // links count
|
|
|
|
this.version = version || 0; // node version
|
|
|
|
this.blksize = undefined; // block size
|
|
|
|
this.nblocks = 1; // blocks count
|
|
|
|
this.data = hash(guid()); // id for data object
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-05-31 07:52:04 +00:00
|
|
|
|
2013-07-29 20:30:24 +00:00
|
|
|
/*
|
|
|
|
* Stats
|
|
|
|
*/
|
|
|
|
|
|
|
|
function Stats(fileNode, devName) {
|
|
|
|
this.node = fileNode.id;
|
|
|
|
this.dev = devName;
|
|
|
|
this.size = fileNode.size;
|
|
|
|
this.nlinks = fileNode.nlinks;
|
|
|
|
this.atime = fileNode.atime;
|
|
|
|
this.mtime = fileNode.mtime;
|
|
|
|
this.ctime = fileNode.ctime;
|
|
|
|
this.type = fileNode.mode;
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-07-29 20:30:24 +00:00
|
|
|
|
2013-07-12 15:11:05 +00:00
|
|
|
/*
|
|
|
|
* find_node
|
|
|
|
*/
|
2013-03-11 19:14:26 +00:00
|
|
|
|
2013-05-31 01:44:32 +00:00
|
|
|
// in: file or directory path
|
|
|
|
// out: node structure, or error
|
2013-11-22 21:12:31 +00:00
|
|
|
function find_node(context, path, callback) {
|
2013-05-31 01:44:32 +00:00
|
|
|
path = normalize(path);
|
2013-07-12 15:11:05 +00:00
|
|
|
if(!path) {
|
|
|
|
return callback(new ENoEntry('path is an empty string'));
|
|
|
|
}
|
2013-05-31 01:44:32 +00:00
|
|
|
var name = basename(path);
|
2013-07-12 15:11:05 +00:00
|
|
|
var parentPath = dirname(path);
|
2013-11-22 21:12:31 +00:00
|
|
|
var followedCount = 0;
|
2013-05-31 01:44:32 +00:00
|
|
|
|
2013-11-12 18:39:11 +00:00
|
|
|
function check_root_directory_node(error, rootDirectoryNode) {
|
|
|
|
if(error) {
|
|
|
|
callback(error);
|
|
|
|
} else if(!rootDirectoryNode) {
|
|
|
|
callback(new ENoEntry('path does not exist'));
|
|
|
|
} else {
|
2013-11-27 00:12:25 +00:00
|
|
|
callback(null, rootDirectoryNode);
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-11-12 18:39:11 +00:00
|
|
|
}
|
2013-05-31 01:44:32 +00:00
|
|
|
|
2013-11-12 18:39:11 +00:00
|
|
|
// in: parent directory node
|
|
|
|
// out: parent directory data
|
|
|
|
function read_parent_directory_data(error, parentDirectoryNode) {
|
|
|
|
if(error) {
|
|
|
|
callback(error);
|
2013-11-21 00:24:49 +00:00
|
|
|
} else if(parentDirectoryNode.mode !== MODE_DIRECTORY || !parentDirectoryNode.data) {
|
2013-11-12 18:39:11 +00:00
|
|
|
callback(new ENotDirectory('a component of the path prefix is not a directory'));
|
|
|
|
} else {
|
2013-11-22 21:12:31 +00:00
|
|
|
context.get(parentDirectoryNode.data, get_node_from_parent_directory_data);
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-11-12 18:39:11 +00:00
|
|
|
}
|
2013-05-31 01:44:32 +00:00
|
|
|
|
2013-11-12 18:39:11 +00:00
|
|
|
// in: parent directory data
|
2013-11-22 21:12:31 +00:00
|
|
|
// out: searched node
|
|
|
|
function get_node_from_parent_directory_data(error, parentDirectoryData) {
|
2013-11-12 18:39:11 +00:00
|
|
|
if(error) {
|
|
|
|
callback(error);
|
|
|
|
} else {
|
|
|
|
if(!_(parentDirectoryData).has(name)) {
|
|
|
|
callback(new ENoEntry('path does not exist'));
|
2013-05-31 01:44:32 +00:00
|
|
|
} else {
|
2013-11-12 18:39:11 +00:00
|
|
|
var nodeId = parentDirectoryData[name].id;
|
2013-11-22 21:12:31 +00:00
|
|
|
context.get(nodeId, is_symbolic_link);
|
2013-05-31 01:44:32 +00:00
|
|
|
}
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-11-12 18:39:11 +00:00
|
|
|
}
|
2013-03-11 19:14:26 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
function is_symbolic_link(error, node) {
|
|
|
|
if(error) {
|
|
|
|
callback(error);
|
|
|
|
} else {
|
|
|
|
if(node.mode == MODE_SYMBOLIC_LINK) {
|
|
|
|
followedCount++;
|
|
|
|
if(followedCount > SYMLOOP_MAX){
|
|
|
|
callback(new ELoop('too many symbolic links were encountered'));
|
|
|
|
} else {
|
|
|
|
follow_symbolic_link(node.data);
|
|
|
|
}
|
|
|
|
} else {
|
2013-11-27 00:12:25 +00:00
|
|
|
callback(null, node);
|
2013-11-22 21:12:31 +00:00
|
|
|
}
|
|
|
|
}
|
2013-03-11 19:14:26 +00:00
|
|
|
}
|
2013-05-31 07:52:04 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
function follow_symbolic_link(data) {
|
|
|
|
data = normalize(data);
|
|
|
|
parentPath = dirname(data);
|
|
|
|
name = basename(data);
|
|
|
|
if(ROOT_DIRECTORY_NAME == name) {
|
|
|
|
context.get(ROOT_NODE_ID, check_root_directory_node);
|
|
|
|
} else {
|
|
|
|
find_node(context, parentPath, read_parent_directory_data);
|
|
|
|
}
|
|
|
|
}
|
2013-05-31 07:52:04 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
if(ROOT_DIRECTORY_NAME == name) {
|
|
|
|
context.get(ROOT_NODE_ID, check_root_directory_node);
|
|
|
|
} else {
|
|
|
|
find_node(context, parentPath, read_parent_directory_data);
|
|
|
|
}
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-05-31 07:52:04 +00:00
|
|
|
|
2013-07-12 15:11:05 +00:00
|
|
|
/*
|
|
|
|
* make_root_directory
|
|
|
|
*/
|
2013-05-31 07:52:04 +00:00
|
|
|
|
2013-05-31 01:44:32 +00:00
|
|
|
// Note: this should only be invoked when formatting a new file system
|
2013-11-22 21:12:31 +00:00
|
|
|
function make_root_directory(context, callback) {
|
2013-05-31 01:44:32 +00:00
|
|
|
var directoryNode;
|
|
|
|
var directoryData;
|
2013-03-11 19:14:26 +00:00
|
|
|
|
2013-05-31 01:44:32 +00:00
|
|
|
function write_directory_node(error, existingNode) {
|
|
|
|
if(!error && existingNode) {
|
2013-07-12 15:11:05 +00:00
|
|
|
callback(new EExists());
|
2013-05-31 01:44:32 +00:00
|
|
|
} else if(error && !error instanceof ENoEntry) {
|
|
|
|
callback(error);
|
2013-03-11 19:14:26 +00:00
|
|
|
} else {
|
2013-05-31 01:44:32 +00:00
|
|
|
directoryNode = new Node(ROOT_NODE_ID, MODE_DIRECTORY);
|
2013-07-12 15:11:05 +00:00
|
|
|
directoryNode.nlinks += 1;
|
2013-11-22 21:12:31 +00:00
|
|
|
context.put(directoryNode.id, directoryNode, write_directory_data);
|
2013-03-11 19:14:26 +00:00
|
|
|
}
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-03-11 19:14:26 +00:00
|
|
|
|
2013-05-31 01:44:32 +00:00
|
|
|
function write_directory_data(error) {
|
|
|
|
if(error) {
|
|
|
|
callback(error);
|
2013-03-11 19:14:26 +00:00
|
|
|
} else {
|
2013-05-31 01:44:32 +00:00
|
|
|
directoryData = {};
|
2013-11-22 21:12:31 +00:00
|
|
|
context.put(directoryNode.data, directoryData, callback);
|
2013-03-11 19:14:26 +00:00
|
|
|
}
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-03-11 19:14:26 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
find_node(context, ROOT_DIRECTORY_NAME, write_directory_node);
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-07-12 15:11:05 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* make_directory
|
|
|
|
*/
|
2013-03-11 19:14:26 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
function make_directory(context, path, callback) {
|
2013-05-31 01:44:32 +00:00
|
|
|
path = normalize(path);
|
|
|
|
var name = basename(path);
|
|
|
|
var parentPath = dirname(path);
|
|
|
|
|
|
|
|
var directoryNode;
|
|
|
|
var directoryData;
|
|
|
|
var parentDirectoryNode;
|
|
|
|
var parentDirectoryData;
|
|
|
|
|
|
|
|
function check_if_directory_exists(error, result) {
|
|
|
|
if(!error && result) {
|
2013-07-12 15:11:05 +00:00
|
|
|
callback(new EExists());
|
2013-05-31 01:44:32 +00:00
|
|
|
} else if(error && !error instanceof ENoEntry) {
|
|
|
|
callback(error);
|
|
|
|
} else {
|
2013-11-22 21:12:31 +00:00
|
|
|
find_node(context, parentPath, read_parent_directory_data);
|
2013-05-31 01:44:32 +00:00
|
|
|
}
|
|
|
|
}
|
2013-03-11 19:14:26 +00:00
|
|
|
|
2013-05-31 01:44:32 +00:00
|
|
|
function read_parent_directory_data(error, result) {
|
|
|
|
if(error) {
|
|
|
|
callback(error);
|
2013-03-11 19:14:26 +00:00
|
|
|
} else {
|
2013-05-31 01:44:32 +00:00
|
|
|
parentDirectoryNode = result;
|
2013-11-22 21:12:31 +00:00
|
|
|
context.get(parentDirectoryNode.data, write_directory_node);
|
2013-03-11 19:14:26 +00:00
|
|
|
}
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-03-11 19:14:26 +00:00
|
|
|
|
2013-05-31 01:44:32 +00:00
|
|
|
function write_directory_node(error, result) {
|
|
|
|
if(error) {
|
|
|
|
callback(error);
|
2013-03-11 19:14:26 +00:00
|
|
|
} else {
|
2013-05-31 01:44:32 +00:00
|
|
|
parentDirectoryData = result;
|
|
|
|
directoryNode = new Node(undefined, MODE_DIRECTORY);
|
2013-07-12 15:11:05 +00:00
|
|
|
directoryNode.nlinks += 1;
|
2013-11-22 21:12:31 +00:00
|
|
|
context.put(directoryNode.id, directoryNode, write_directory_data);
|
2013-03-11 19:14:26 +00:00
|
|
|
}
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-03-11 19:14:26 +00:00
|
|
|
|
2013-05-31 01:44:32 +00:00
|
|
|
function write_directory_data(error) {
|
|
|
|
if(error) {
|
|
|
|
callback(error);
|
2013-03-11 19:14:26 +00:00
|
|
|
} else {
|
2013-05-31 01:44:32 +00:00
|
|
|
directoryData = {};
|
2013-11-22 21:12:31 +00:00
|
|
|
context.put(directoryNode.data, directoryData, update_parent_directory_data);
|
2013-03-11 19:14:26 +00:00
|
|
|
}
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-03-11 19:14:26 +00:00
|
|
|
|
2013-05-31 01:44:32 +00:00
|
|
|
function update_parent_directory_data(error) {
|
|
|
|
if(error) {
|
|
|
|
callback(error);
|
|
|
|
} else {
|
|
|
|
parentDirectoryData[name] = new DirectoryEntry(directoryNode.id, MODE_DIRECTORY);
|
2013-11-22 21:12:31 +00:00
|
|
|
context.put(parentDirectoryNode.data, parentDirectoryData, callback);
|
2013-03-11 19:14:26 +00:00
|
|
|
}
|
|
|
|
}
|
2013-05-31 01:44:32 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
find_node(context, path, check_if_directory_exists);
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-03-11 19:14:26 +00:00
|
|
|
|
2013-07-12 15:11:05 +00:00
|
|
|
/*
|
|
|
|
* remove_directory
|
|
|
|
*/
|
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
function remove_directory(context, path, callback) {
|
2013-05-31 01:44:32 +00:00
|
|
|
path = normalize(path);
|
|
|
|
var name = basename(path);
|
|
|
|
var parentPath = dirname(path);
|
|
|
|
|
|
|
|
var directoryNode;
|
|
|
|
var directoryData;
|
|
|
|
var parentDirectoryNode;
|
|
|
|
var parentDirectoryData;
|
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
function read_parent_directory_data(error, result) {
|
2013-05-31 01:44:32 +00:00
|
|
|
if(error) {
|
|
|
|
callback(error);
|
|
|
|
} else {
|
2013-11-22 21:12:31 +00:00
|
|
|
parentDirectoryNode = result;
|
|
|
|
context.get(parentDirectoryNode.data, check_if_node_exists);
|
2013-05-31 01:44:32 +00:00
|
|
|
}
|
2013-03-11 19:14:26 +00:00
|
|
|
}
|
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
function check_if_node_exists(error, result) {
|
2013-05-31 01:44:32 +00:00
|
|
|
if(error) {
|
|
|
|
callback(error);
|
2013-11-22 21:12:31 +00:00
|
|
|
} else if(ROOT_DIRECTORY_NAME == name) {
|
|
|
|
callback(new EBusy());
|
|
|
|
} else if(!_(result).has(name)) {
|
|
|
|
callback(new ENoEntry());
|
2013-03-11 19:14:26 +00:00
|
|
|
} else {
|
2013-11-22 21:12:31 +00:00
|
|
|
parentDirectoryData = result;
|
|
|
|
directoryNode = parentDirectoryData[name].id;
|
|
|
|
context.get(directoryNode, check_if_node_is_directory);
|
2013-03-11 19:14:26 +00:00
|
|
|
}
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-03-11 19:14:26 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
function check_if_node_is_directory(error, result) {
|
2013-05-31 01:44:32 +00:00
|
|
|
if(error) {
|
|
|
|
callback(error);
|
2013-11-22 21:12:31 +00:00
|
|
|
} else if(result.mode != MODE_DIRECTORY) {
|
|
|
|
callback(new ENotDirectory());
|
2013-05-31 01:44:32 +00:00
|
|
|
} else {
|
2013-11-22 21:12:31 +00:00
|
|
|
directoryNode = result;
|
|
|
|
context.get(directoryNode.data, check_if_directory_is_empty);
|
2013-05-31 01:44:32 +00:00
|
|
|
}
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-03-11 19:14:26 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
function check_if_directory_is_empty(error, result) {
|
2013-05-31 01:44:32 +00:00
|
|
|
if(error) {
|
|
|
|
callback(error);
|
|
|
|
} else {
|
2013-11-22 21:12:31 +00:00
|
|
|
directoryData = result;
|
|
|
|
if(_(directoryData).size() > 0) {
|
|
|
|
callback(new ENotEmpty());
|
|
|
|
} else {
|
|
|
|
remove_directory_entry_from_parent_directory_node();
|
|
|
|
}
|
2013-05-31 01:44:32 +00:00
|
|
|
}
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-03-11 19:14:26 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
function remove_directory_entry_from_parent_directory_node() {
|
|
|
|
delete parentDirectoryData[name];
|
|
|
|
context.put(parentDirectoryNode.data, parentDirectoryData, remove_directory_node);
|
|
|
|
}
|
|
|
|
|
2013-05-31 01:44:32 +00:00
|
|
|
function remove_directory_node(error) {
|
|
|
|
if(error) {
|
|
|
|
callback(error);
|
|
|
|
} else {
|
2013-11-22 21:12:31 +00:00
|
|
|
context.delete(directoryNode.id, remove_directory_data);
|
2013-03-11 19:14:26 +00:00
|
|
|
}
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-05-31 01:44:32 +00:00
|
|
|
|
|
|
|
function remove_directory_data(error) {
|
|
|
|
if(error) {
|
|
|
|
callback(error);
|
|
|
|
} else {
|
2013-11-22 21:12:31 +00:00
|
|
|
context.delete(directoryNode.data, callback);
|
2013-05-31 01:44:32 +00:00
|
|
|
}
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-05-31 01:44:32 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
find_node(context, parentPath, read_parent_directory_data);
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-03-11 19:14:26 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
function open_file(context, path, flags, callback) {
|
2013-07-12 15:11:05 +00:00
|
|
|
path = normalize(path);
|
|
|
|
var name = basename(path);
|
|
|
|
var parentPath = dirname(path);
|
|
|
|
|
|
|
|
var directoryNode;
|
|
|
|
var directoryData;
|
|
|
|
var directoryEntry;
|
|
|
|
var fileNode;
|
|
|
|
var fileData;
|
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
var followedCount = 0;
|
|
|
|
|
2013-07-22 17:08:35 +00:00
|
|
|
if(ROOT_DIRECTORY_NAME == name) {
|
|
|
|
if(_(flags).contains(O_WRITE)) {
|
2013-11-08 20:33:44 +00:00
|
|
|
callback(new EIsDirectory('the named file is a directory and O_WRITE is set'));
|
2013-07-22 17:08:35 +00:00
|
|
|
} else {
|
2013-11-22 21:12:31 +00:00
|
|
|
find_node(context, path, set_file_node);
|
2013-07-22 17:08:35 +00:00
|
|
|
}
|
|
|
|
} else {
|
2013-11-22 21:12:31 +00:00
|
|
|
find_node(context, parentPath, read_directory_data);
|
2013-07-22 17:08:35 +00:00
|
|
|
}
|
2013-07-12 15:11:05 +00:00
|
|
|
|
|
|
|
function read_directory_data(error, result) {
|
|
|
|
if(error) {
|
|
|
|
callback(error);
|
|
|
|
} else {
|
|
|
|
directoryNode = result;
|
2013-11-22 21:12:31 +00:00
|
|
|
context.get(directoryNode.data, check_if_file_exists);
|
2013-07-12 15:11:05 +00:00
|
|
|
}
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-07-12 15:11:05 +00:00
|
|
|
|
|
|
|
function check_if_file_exists(error, result) {
|
|
|
|
if(error) {
|
|
|
|
callback(error);
|
|
|
|
} else {
|
|
|
|
directoryData = result;
|
|
|
|
if(_(directoryData).has(name)) {
|
|
|
|
if(_(flags).contains(O_EXCLUSIVE)) {
|
2013-11-08 20:33:44 +00:00
|
|
|
callback(new ENoEntry('O_CREATE and O_EXCLUSIVE are set, and the named file exists'));
|
2013-07-12 15:11:05 +00:00
|
|
|
} else {
|
|
|
|
directoryEntry = directoryData[name];
|
2013-07-19 15:22:23 +00:00
|
|
|
if(directoryEntry.type == MODE_DIRECTORY && _(flags).contains(O_WRITE)) {
|
2013-11-08 20:33:44 +00:00
|
|
|
callback(new EIsDirectory('the named file is a directory and O_WRITE is set'));
|
2013-07-12 15:11:05 +00:00
|
|
|
} else {
|
2013-11-22 21:12:31 +00:00
|
|
|
context.get(directoryEntry.id, check_if_symbolic_link);
|
2013-07-12 15:11:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(!_(flags).contains(O_CREATE)) {
|
|
|
|
callback(new ENoEntry('O_CREATE is not set and the named file does not exist'));
|
|
|
|
} else {
|
|
|
|
write_file_node();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-07-12 15:11:05 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
function check_if_symbolic_link(error, result) {
|
|
|
|
if(error) {
|
|
|
|
callback(error);
|
|
|
|
} else {
|
|
|
|
var node = result;
|
|
|
|
if(node.mode == MODE_SYMBOLIC_LINK) {
|
|
|
|
followedCount++;
|
|
|
|
if(followedCount > SYMLOOP_MAX){
|
|
|
|
callback(new ELoop('too many symbolic links were encountered'));
|
|
|
|
} else {
|
|
|
|
follow_symbolic_link(node.data);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
set_file_node(undefined, node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function follow_symbolic_link(data) {
|
|
|
|
data = normalize(data);
|
|
|
|
parentPath = dirname(data);
|
|
|
|
name = basename(data);
|
|
|
|
if(ROOT_DIRECTORY_NAME == name) {
|
|
|
|
if(_(flags).contains(O_WRITE)) {
|
|
|
|
callback(new EIsDirectory('the named file is a directory and O_WRITE is set'));
|
|
|
|
} else {
|
|
|
|
find_node(context, path, set_file_node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
find_node(context, parentPath, read_directory_data);
|
|
|
|
}
|
|
|
|
|
2013-07-12 15:11:05 +00:00
|
|
|
function set_file_node(error, result) {
|
|
|
|
if(error) {
|
|
|
|
callback(error);
|
|
|
|
} else {
|
|
|
|
fileNode = result;
|
2013-11-27 00:12:25 +00:00
|
|
|
callback(null, fileNode);
|
2013-07-12 15:11:05 +00:00
|
|
|
}
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-07-12 15:11:05 +00:00
|
|
|
|
|
|
|
function write_file_node() {
|
|
|
|
fileNode = new Node(undefined, MODE_FILE);
|
|
|
|
fileNode.nlinks += 1;
|
2013-11-22 21:12:31 +00:00
|
|
|
context.put(fileNode.id, fileNode, write_file_data);
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-07-12 15:11:05 +00:00
|
|
|
|
|
|
|
function write_file_data(error) {
|
|
|
|
if(error) {
|
|
|
|
callback(error);
|
|
|
|
} else {
|
2013-07-22 17:08:35 +00:00
|
|
|
fileData = new Uint8Array(0);
|
2013-11-22 21:12:31 +00:00
|
|
|
context.put(fileNode.data, fileData, update_directory_data);
|
2013-07-12 15:11:05 +00:00
|
|
|
}
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-07-12 15:11:05 +00:00
|
|
|
|
|
|
|
function update_directory_data(error) {
|
|
|
|
if(error) {
|
|
|
|
callback(error);
|
|
|
|
} else {
|
|
|
|
directoryData[name] = new DirectoryEntry(fileNode.id, MODE_FILE);
|
2013-11-22 21:12:31 +00:00
|
|
|
context.put(directoryNode.data, directoryData, handle_update_result);
|
2013-07-12 15:11:05 +00:00
|
|
|
}
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-07-12 15:11:05 +00:00
|
|
|
|
|
|
|
function handle_update_result(error) {
|
|
|
|
if(error) {
|
|
|
|
callback(error);
|
|
|
|
} else {
|
2013-11-27 00:12:25 +00:00
|
|
|
callback(null, fileNode);
|
2013-07-12 15:11:05 +00:00
|
|
|
}
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
|
|
|
}
|
2013-03-11 19:14:26 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
function write_data(context, ofd, buffer, offset, length, position, callback) {
|
2013-07-12 15:11:05 +00:00
|
|
|
var fileNode;
|
|
|
|
var fileData;
|
2013-03-11 19:14:26 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
function return_nbytes(error) {
|
|
|
|
if(error) {
|
|
|
|
callback(error);
|
|
|
|
} else {
|
2013-11-27 00:12:25 +00:00
|
|
|
callback(null, length);
|
2013-11-22 21:12:31 +00:00
|
|
|
}
|
|
|
|
}
|
2013-03-11 19:14:26 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
function update_file_node(error) {
|
2013-07-12 15:11:05 +00:00
|
|
|
if(error) {
|
|
|
|
callback(error);
|
|
|
|
} else {
|
2013-11-22 21:12:31 +00:00
|
|
|
context.put(fileNode.id, fileNode, return_nbytes);
|
2013-07-12 15:11:05 +00:00
|
|
|
}
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-05-31 01:44:32 +00:00
|
|
|
|
2013-07-12 15:11:05 +00:00
|
|
|
function update_file_data(error, result) {
|
|
|
|
if(error) {
|
|
|
|
callback(error);
|
|
|
|
} else {
|
|
|
|
fileData = result;
|
2013-07-29 18:55:25 +00:00
|
|
|
var _position = (!(undefined === position || null === position)) ? position : ofd.position;
|
2013-07-12 15:11:05 +00:00
|
|
|
var newSize = Math.max(fileData.length, _position + length);
|
|
|
|
var newData = new Uint8Array(newSize);
|
|
|
|
if(fileData) {
|
|
|
|
newData.set(fileData);
|
|
|
|
}
|
|
|
|
newData.set(buffer, _position);
|
|
|
|
if(undefined === position) {
|
|
|
|
ofd.position += length;
|
|
|
|
}
|
2013-05-31 01:44:32 +00:00
|
|
|
|
2013-07-12 15:11:05 +00:00
|
|
|
fileNode.size = newSize;
|
|
|
|
fileNode.mtime = Date.now();
|
|
|
|
fileNode.version += 1;
|
2013-05-31 01:44:32 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
context.put(fileNode.data, newData, update_file_node);
|
2013-07-12 15:11:05 +00:00
|
|
|
}
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-05-31 01:44:32 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
function read_file_data(error, result) {
|
2013-07-12 15:11:05 +00:00
|
|
|
if(error) {
|
|
|
|
callback(error);
|
|
|
|
} else {
|
2013-11-22 21:12:31 +00:00
|
|
|
fileNode = result;
|
|
|
|
context.get(fileNode.data, update_file_data);
|
2013-07-12 15:11:05 +00:00
|
|
|
}
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-07-12 15:11:05 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
context.get(ofd.id, read_file_data);
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-07-12 15:11:05 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
function read_data(context, ofd, buffer, offset, length, position, callback) {
|
2013-07-12 15:11:05 +00:00
|
|
|
var fileNode;
|
|
|
|
var fileData;
|
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
function handle_file_data(error, result) {
|
2013-07-12 15:11:05 +00:00
|
|
|
if(error) {
|
|
|
|
callback(error);
|
|
|
|
} else {
|
|
|
|
fileData = result;
|
2013-07-29 18:55:25 +00:00
|
|
|
var _position = (!(undefined === position || null === position)) ? position : ofd.position;
|
2013-07-12 15:11:05 +00:00
|
|
|
length = (_position + length > buffer.length) ? length - _position : length;
|
|
|
|
var dataView = fileData.subarray(_position, _position + length);
|
2013-07-22 17:08:35 +00:00
|
|
|
buffer.set(dataView, offset);
|
2013-07-12 15:11:05 +00:00
|
|
|
if(undefined === position) {
|
|
|
|
ofd.position += length;
|
|
|
|
}
|
2013-11-27 00:12:25 +00:00
|
|
|
callback(null, length);
|
2013-07-12 15:11:05 +00:00
|
|
|
}
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-11-22 21:12:31 +00:00
|
|
|
|
|
|
|
function read_file_data(error, result) {
|
|
|
|
if(error) {
|
|
|
|
callback(error);
|
|
|
|
} else {
|
|
|
|
fileNode = result;
|
|
|
|
context.get(fileNode.data, handle_file_data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
context.get(ofd.id, read_file_data);
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-07-12 15:11:05 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
function stat_file(context, path, callback) {
|
2013-07-12 15:11:05 +00:00
|
|
|
path = normalize(path);
|
|
|
|
var name = basename(path);
|
2013-05-31 01:44:32 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
function check_file(error, result) {
|
|
|
|
if(error) {
|
|
|
|
callback(error);
|
|
|
|
} else {
|
2013-11-27 00:12:25 +00:00
|
|
|
callback(null, result);
|
2013-11-22 21:12:31 +00:00
|
|
|
}
|
|
|
|
}
|
2013-05-31 01:44:32 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
find_node(context, path, check_file);
|
|
|
|
}
|
|
|
|
|
|
|
|
function fstat_file(context, ofd, callback) {
|
2013-07-12 15:11:05 +00:00
|
|
|
function check_file(error, result) {
|
|
|
|
if(error) {
|
|
|
|
callback(error);
|
|
|
|
} else {
|
2013-11-27 00:12:25 +00:00
|
|
|
callback(null, result);
|
2013-07-12 15:11:05 +00:00
|
|
|
}
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-11-22 21:12:31 +00:00
|
|
|
|
|
|
|
context.get(ofd.id, check_file);
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-05-31 07:52:04 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
function lstat_file(context, path, callback) {
|
|
|
|
path = normalize(path);
|
|
|
|
var name = basename(path);
|
|
|
|
var parentPath = dirname(path);
|
|
|
|
|
|
|
|
var directoryNode;
|
|
|
|
var directoryData;
|
|
|
|
|
|
|
|
if(ROOT_DIRECTORY_NAME == name) {
|
|
|
|
context.get(ROOT_NODE_ID, check_file);
|
|
|
|
} else {
|
|
|
|
find_node(context, parentPath, read_directory_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
function read_directory_data(error, result) {
|
|
|
|
if(error) {
|
|
|
|
callback(error);
|
|
|
|
} else {
|
|
|
|
directoryNode = result;
|
|
|
|
context.get(directoryNode.data, check_if_file_exists);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function check_if_file_exists(error, result) {
|
|
|
|
if(error) {
|
|
|
|
callback(error);
|
|
|
|
} else {
|
|
|
|
directoryData = result;
|
|
|
|
if(!_(directoryData).has(name)) {
|
|
|
|
callback(new ENoEntry('a component of the path does not name an existing file'));
|
|
|
|
} else {
|
|
|
|
context.get(directoryData[name].id, check_file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-29 18:55:25 +00:00
|
|
|
|
|
|
|
function check_file(error, result) {
|
|
|
|
if(error) {
|
|
|
|
callback(error);
|
|
|
|
} else {
|
2013-11-27 00:12:25 +00:00
|
|
|
callback(null, result);
|
2013-07-29 18:55:25 +00:00
|
|
|
}
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
|
|
|
}
|
2013-07-29 18:55:25 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
function link_node(context, oldpath, newpath, callback) {
|
2013-07-26 02:49:53 +00:00
|
|
|
oldpath = normalize(oldpath);
|
|
|
|
var oldname = basename(oldpath);
|
|
|
|
var oldParentPath = dirname(oldpath);
|
|
|
|
|
|
|
|
newpath = normalize(newpath);
|
|
|
|
var newname = basename(newpath);
|
|
|
|
var newParentPath = dirname(newpath);
|
|
|
|
|
|
|
|
var oldDirectoryNode;
|
|
|
|
var oldDirectoryData;
|
|
|
|
var newDirectoryNode;
|
|
|
|
var newDirectoryData;
|
|
|
|
var fileNode;
|
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
function update_file_node(error, result) {
|
|
|
|
if(error) {
|
|
|
|
callback(error);
|
|
|
|
} else {
|
|
|
|
fileNode = result;
|
|
|
|
fileNode.nlinks += 1;
|
|
|
|
context.put(fileNode.id, fileNode, callback);
|
|
|
|
}
|
|
|
|
}
|
2013-07-26 02:49:53 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
function read_directory_entry(error, result) {
|
2013-07-26 02:49:53 +00:00
|
|
|
if(error) {
|
|
|
|
callback(error);
|
|
|
|
} else {
|
2013-11-22 21:12:31 +00:00
|
|
|
context.get(newDirectoryData[newname].id, update_file_node);
|
2013-07-26 02:49:53 +00:00
|
|
|
}
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-07-26 02:49:53 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
function check_if_new_file_exists(error, result) {
|
2013-07-26 02:49:53 +00:00
|
|
|
if(error) {
|
|
|
|
callback(error);
|
|
|
|
} else {
|
2013-11-22 21:12:31 +00:00
|
|
|
newDirectoryData = result;
|
|
|
|
if(_(newDirectoryData).has(newname)) {
|
|
|
|
callback(new EExists('newpath resolves to an existing file'));
|
2013-07-26 02:49:53 +00:00
|
|
|
} else {
|
2013-11-22 21:12:31 +00:00
|
|
|
newDirectoryData[newname] = oldDirectoryData[oldname];
|
|
|
|
context.put(newDirectoryNode.data, newDirectoryData, read_directory_entry);
|
2013-07-26 02:49:53 +00:00
|
|
|
}
|
|
|
|
}
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-07-26 02:49:53 +00:00
|
|
|
|
|
|
|
function read_new_directory_data(error, result) {
|
|
|
|
if(error) {
|
|
|
|
callback(error);
|
|
|
|
} else {
|
|
|
|
newDirectoryNode = result;
|
2013-11-22 21:12:31 +00:00
|
|
|
context.get(newDirectoryNode.data, check_if_new_file_exists);
|
2013-07-26 02:49:53 +00:00
|
|
|
}
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-07-26 02:49:53 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
function check_if_old_file_exists(error, result) {
|
2013-07-26 02:49:53 +00:00
|
|
|
if(error) {
|
|
|
|
callback(error);
|
|
|
|
} else {
|
2013-11-22 21:12:31 +00:00
|
|
|
oldDirectoryData = result;
|
|
|
|
if(!_(oldDirectoryData).has(oldname)) {
|
|
|
|
callback(new ENoEntry('a component of either path prefix does not exist'));
|
2013-07-26 02:49:53 +00:00
|
|
|
} else {
|
2013-11-22 21:12:31 +00:00
|
|
|
find_node(context, newParentPath, read_new_directory_data);
|
2013-07-26 02:49:53 +00:00
|
|
|
}
|
|
|
|
}
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-07-26 02:49:53 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
function read_old_directory_data(error, result) {
|
2013-07-26 02:49:53 +00:00
|
|
|
if(error) {
|
|
|
|
callback(error);
|
|
|
|
} else {
|
2013-11-22 21:12:31 +00:00
|
|
|
oldDirectoryNode = result;
|
|
|
|
context.get(oldDirectoryNode.data, check_if_old_file_exists);
|
2013-07-26 02:49:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
find_node(context, oldParentPath, read_old_directory_data);
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-07-26 04:15:48 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
function unlink_node(context, path, callback) {
|
2013-07-26 04:15:48 +00:00
|
|
|
path = normalize(path);
|
2013-11-22 21:12:31 +00:00
|
|
|
var name = basename(path);
|
|
|
|
var parentPath = dirname(path);
|
2013-07-26 04:15:48 +00:00
|
|
|
|
|
|
|
var directoryNode;
|
|
|
|
var directoryData;
|
|
|
|
var fileNode;
|
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
function update_directory_data(error) {
|
|
|
|
if(error) {
|
|
|
|
callback(error);
|
|
|
|
} else {
|
|
|
|
delete directoryData[name];
|
|
|
|
context.put(directoryNode.data, directoryData, callback);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function delete_file_data(error) {
|
|
|
|
if(error) {
|
|
|
|
callback(error);
|
|
|
|
} else {
|
|
|
|
context.delete(fileNode.data, update_directory_data);
|
|
|
|
}
|
|
|
|
}
|
2013-07-26 04:15:48 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
function update_file_node(error, result) {
|
2013-07-26 04:15:48 +00:00
|
|
|
if(error) {
|
|
|
|
callback(error);
|
|
|
|
} else {
|
2013-11-22 21:12:31 +00:00
|
|
|
fileNode = result;
|
|
|
|
fileNode.nlinks -= 1;
|
|
|
|
if(fileNode.nlinks < 1) {
|
|
|
|
context.delete(fileNode.id, delete_file_data);
|
|
|
|
} else {
|
|
|
|
context.put(fileNode.id, fileNode, update_directory_data);
|
|
|
|
}
|
2013-07-26 04:15:48 +00:00
|
|
|
}
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-07-26 04:15:48 +00:00
|
|
|
|
|
|
|
function check_if_file_exists(error, result) {
|
2013-07-26 02:49:53 +00:00
|
|
|
if(error) {
|
|
|
|
callback(error);
|
|
|
|
} else {
|
2013-07-26 04:15:48 +00:00
|
|
|
directoryData = result;
|
|
|
|
if(!_(directoryData).has(name)) {
|
|
|
|
callback(new ENoEntry('a component of the path does not name an existing file'));
|
|
|
|
} else {
|
2013-11-22 21:12:31 +00:00
|
|
|
context.get(directoryData[name].id, update_file_node);
|
2013-07-26 04:15:48 +00:00
|
|
|
}
|
2013-07-26 02:49:53 +00:00
|
|
|
}
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-07-26 02:49:53 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
function read_directory_data(error, result) {
|
2013-07-26 02:49:53 +00:00
|
|
|
if(error) {
|
|
|
|
callback(error);
|
|
|
|
} else {
|
2013-11-22 21:12:31 +00:00
|
|
|
directoryNode = result;
|
|
|
|
context.get(directoryNode.data, check_if_file_exists);
|
2013-07-26 04:15:48 +00:00
|
|
|
}
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-07-26 04:15:48 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
find_node(context, parentPath, read_directory_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
function read_directory(context, path, callback) {
|
|
|
|
path = normalize(path);
|
|
|
|
var name = basename(path);
|
|
|
|
|
|
|
|
var directoryNode;
|
|
|
|
var directoryData;
|
|
|
|
|
|
|
|
function handle_directory_data(error, result) {
|
|
|
|
if(error) {
|
|
|
|
callback(error);
|
|
|
|
} else {
|
|
|
|
directoryData = result;
|
|
|
|
var files = Object.keys(directoryData);
|
2013-11-27 00:12:25 +00:00
|
|
|
callback(null, files);
|
2013-11-22 21:12:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function read_directory_data(error, result) {
|
|
|
|
if(error) {
|
|
|
|
callback(error);
|
|
|
|
} else {
|
|
|
|
directoryNode = result;
|
|
|
|
context.get(directoryNode.data, handle_directory_data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
find_node(context, path, read_directory_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
function make_symbolic_link(context, srcpath, dstpath, callback) {
|
|
|
|
dstpath = normalize(dstpath);
|
|
|
|
var name = basename(dstpath);
|
|
|
|
var parentPath = dirname(dstpath);
|
|
|
|
|
|
|
|
var directoryNode;
|
|
|
|
var directoryData;
|
|
|
|
var fileNode;
|
|
|
|
|
|
|
|
if(ROOT_DIRECTORY_NAME == name) {
|
|
|
|
callback(new EExists('the destination path already exists'));
|
|
|
|
} else {
|
|
|
|
find_node(context, parentPath, read_directory_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
function read_directory_data(error, result) {
|
|
|
|
if(error) {
|
|
|
|
callback(error);
|
|
|
|
} else {
|
|
|
|
directoryNode = result;
|
|
|
|
context.get(directoryNode.data, check_if_file_exists);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function check_if_file_exists(error, result) {
|
2013-07-26 04:15:48 +00:00
|
|
|
if(error) {
|
|
|
|
callback(error);
|
|
|
|
} else {
|
2013-11-22 21:12:31 +00:00
|
|
|
directoryData = result;
|
|
|
|
if(_(directoryData).has(name)) {
|
|
|
|
callback(new EExists('the destination path already exists'));
|
|
|
|
} else {
|
|
|
|
write_file_node();
|
|
|
|
}
|
2013-07-26 02:49:53 +00:00
|
|
|
}
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-07-26 04:15:48 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
function write_file_node() {
|
|
|
|
fileNode = new Node(undefined, MODE_SYMBOLIC_LINK);
|
|
|
|
fileNode.nlinks += 1;
|
|
|
|
fileNode.size = srcpath.length;
|
|
|
|
fileNode.data = srcpath;
|
|
|
|
context.put(fileNode.id, fileNode, update_directory_data);
|
|
|
|
}
|
|
|
|
|
2013-07-26 04:15:48 +00:00
|
|
|
function update_directory_data(error) {
|
|
|
|
if(error) {
|
|
|
|
callback(error);
|
|
|
|
} else {
|
2013-11-22 21:12:31 +00:00
|
|
|
directoryData[name] = new DirectoryEntry(fileNode.id, MODE_SYMBOLIC_LINK);
|
|
|
|
context.put(directoryNode.data, directoryData, callback);
|
2013-07-26 04:15:48 +00:00
|
|
|
}
|
|
|
|
}
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-07-26 02:49:53 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
function read_link(context, path, callback) {
|
2013-11-07 20:55:14 +00:00
|
|
|
path = normalize(path);
|
|
|
|
var name = basename(path);
|
2013-11-22 21:12:31 +00:00
|
|
|
var parentPath = dirname(path);
|
2013-11-07 20:55:14 +00:00
|
|
|
|
|
|
|
var directoryNode;
|
|
|
|
var directoryData;
|
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
find_node(context, parentPath, read_directory_data);
|
2013-11-07 20:55:14 +00:00
|
|
|
|
|
|
|
function read_directory_data(error, result) {
|
|
|
|
if(error) {
|
|
|
|
callback(error);
|
|
|
|
} else {
|
|
|
|
directoryNode = result;
|
2013-11-22 21:12:31 +00:00
|
|
|
context.get(directoryNode.data, check_if_file_exists);
|
2013-11-07 20:55:14 +00:00
|
|
|
}
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-11-07 20:55:14 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
function check_if_file_exists(error, result) {
|
2013-11-07 20:55:14 +00:00
|
|
|
if(error) {
|
|
|
|
callback(error);
|
|
|
|
} else {
|
|
|
|
directoryData = result;
|
2013-11-22 21:12:31 +00:00
|
|
|
if(!_(directoryData).has(name)) {
|
|
|
|
callback(new ENoEntry('a component of the path does not name an existing file'));
|
|
|
|
} else {
|
|
|
|
context.get(directoryData[name].id, check_if_symbolic);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function check_if_symbolic(error, result) {
|
|
|
|
if(error) {
|
|
|
|
callback(error);
|
|
|
|
} else {
|
|
|
|
if(result.mode != MODE_SYMBOLIC_LINK) {
|
|
|
|
callback(new EInvalid("path not a symbolic link"));
|
|
|
|
} else {
|
2013-11-27 00:12:25 +00:00
|
|
|
callback(null, result.data);
|
2013-11-22 21:12:31 +00:00
|
|
|
}
|
2013-11-07 20:55:14 +00:00
|
|
|
}
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
|
|
|
}
|
2013-11-07 20:55:14 +00:00
|
|
|
|
2013-11-09 20:12:52 +00:00
|
|
|
function validate_flags(flags) {
|
|
|
|
if(!_(O_FLAGS).has(flags)) {
|
|
|
|
return null;
|
|
|
|
}
|
2013-11-22 21:12:31 +00:00
|
|
|
return O_FLAGS[flags];
|
2013-11-09 20:12:52 +00:00
|
|
|
}
|
|
|
|
|
2013-05-31 01:44:32 +00:00
|
|
|
/*
|
|
|
|
* FileSystem
|
2013-11-22 21:12:31 +00:00
|
|
|
*
|
|
|
|
* A FileSystem takes an `options` object, which can specify a number of,
|
|
|
|
* options. All options are optional, and include:
|
|
|
|
*
|
|
|
|
* name: the name of the file system, defaults to "local"
|
|
|
|
*
|
|
|
|
* flags: one or more flags to use when creating/opening the file system.
|
|
|
|
* For example: "FORMAT" will cause the file system to be formatted.
|
|
|
|
* No explicit flags are set by default.
|
|
|
|
*
|
2013-11-27 00:12:25 +00:00
|
|
|
* provider: an explicit storage provider to use for the file
|
|
|
|
* system's database context provider. A number of context
|
|
|
|
* providers are included (see /src/providers), and users
|
|
|
|
* can write one of their own and pass it in to be used.
|
|
|
|
* By default an IndexedDB provider is used.
|
2013-11-22 21:12:31 +00:00
|
|
|
*
|
2013-11-27 00:12:25 +00:00
|
|
|
* callback: a callback function to be executed when the file system becomes
|
|
|
|
* ready for use. Depending on the context provider used, this might
|
|
|
|
* be right away, or could take some time. The callback should expect
|
|
|
|
* an `error` argument, which will be null if everything worked. Also
|
|
|
|
* users should check the file system's `readyState` and `error`
|
|
|
|
* properties to make sure it is usable.
|
2013-05-31 01:44:32 +00:00
|
|
|
*/
|
2013-11-27 00:12:25 +00:00
|
|
|
function FileSystem(options, callback) {
|
2013-11-22 21:12:31 +00:00
|
|
|
options = options || {};
|
2013-11-27 00:12:25 +00:00
|
|
|
callback = callback || nop;
|
2013-03-11 19:14:26 +00:00
|
|
|
|
2013-11-27 00:12:25 +00:00
|
|
|
var name = options.name || FILE_SYSTEM_NAME;
|
2013-11-22 21:12:31 +00:00
|
|
|
var flags = options.flags;
|
2013-11-27 00:12:25 +00:00
|
|
|
var provider = options.provider || new providers.Default(name);
|
2013-11-22 21:12:31 +00:00
|
|
|
var forceFormatting = _(flags).contains(FS_FORMAT);
|
2013-11-22 01:44:57 +00:00
|
|
|
|
2013-11-27 00:12:25 +00:00
|
|
|
var fs = this;
|
2013-11-22 21:12:31 +00:00
|
|
|
fs.readyState = FS_PENDING;
|
|
|
|
fs.name = name;
|
|
|
|
fs.error = null;
|
|
|
|
|
2013-11-27 00:12:25 +00:00
|
|
|
// Safely expose the list of open files and file
|
|
|
|
// descriptor management functions
|
|
|
|
var openFiles = {};
|
|
|
|
var nextDescriptor = 1;
|
|
|
|
Object.defineProperty(this, "openFiles", {
|
|
|
|
get: function() { return openFiles; }
|
|
|
|
});
|
|
|
|
this.allocDescriptor = function(openFileDescription) {
|
|
|
|
var fd = nextDescriptor ++;
|
|
|
|
openFiles[fd] = openFileDescription;
|
|
|
|
return fd;
|
|
|
|
};
|
|
|
|
this.releaseDescriptor = function(fd) {
|
|
|
|
delete openFiles[fd];
|
|
|
|
};
|
|
|
|
|
|
|
|
// Safely expose the operation queue
|
|
|
|
var queue = [];
|
|
|
|
this.queueOrRun = function(operation) {
|
|
|
|
var error;
|
|
|
|
|
|
|
|
if(FS_READY == fs.readyState) {
|
|
|
|
operation.call(fs);
|
|
|
|
} else if(FS_ERROR == fs.readyState) {
|
|
|
|
error = new EFileSystemError('unknown error');
|
|
|
|
} else {
|
|
|
|
queue.push(operation);
|
|
|
|
}
|
|
|
|
|
|
|
|
return error;
|
|
|
|
};
|
|
|
|
function runQueued() {
|
|
|
|
queue.forEach(function(operation) {
|
|
|
|
operation.call(this);
|
|
|
|
}.bind(fs));
|
|
|
|
queue = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open file system storage provider
|
2013-11-22 21:12:31 +00:00
|
|
|
provider.open(function(err, needsFormatting) {
|
|
|
|
function complete(error) {
|
|
|
|
fs.provider = provider;
|
|
|
|
if(error) {
|
|
|
|
fs.readyState = FS_ERROR;
|
|
|
|
} else {
|
|
|
|
fs.readyState = FS_READY;
|
2013-11-27 00:12:25 +00:00
|
|
|
runQueued();
|
2013-11-22 21:12:31 +00:00
|
|
|
}
|
2013-11-27 00:12:25 +00:00
|
|
|
callback(error);
|
2013-11-22 21:12:31 +00:00
|
|
|
}
|
2013-03-11 19:14:26 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
if(err) {
|
|
|
|
return complete(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we don't need or want formatting, we're done
|
|
|
|
if(!(forceFormatting || needsFormatting)) {
|
2013-11-27 00:12:25 +00:00
|
|
|
return complete(null);
|
2013-11-22 21:12:31 +00:00
|
|
|
}
|
|
|
|
// otherwise format the fs first
|
|
|
|
var context = provider.getReadWriteContext();
|
|
|
|
context.clear(function(err) {
|
|
|
|
if(err) {
|
2013-11-27 00:12:25 +00:00
|
|
|
complete(err);
|
|
|
|
return;
|
2013-11-22 21:12:31 +00:00
|
|
|
}
|
|
|
|
make_root_directory(context, complete);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-11-27 00:12:25 +00:00
|
|
|
// Expose storage providers on FileSystem constructor
|
|
|
|
FileSystem.providers = providers;
|
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
function _open(fs, context, path, flags, callback) {
|
2013-11-21 00:24:49 +00:00
|
|
|
function check_result(error, fileNode) {
|
|
|
|
if(error) {
|
2013-11-21 21:15:30 +00:00
|
|
|
callback(error);
|
2013-11-21 00:24:49 +00:00
|
|
|
} else {
|
|
|
|
var position;
|
|
|
|
if(_(flags).contains(O_APPEND)) {
|
|
|
|
position = fileNode.size;
|
|
|
|
} else {
|
|
|
|
position = 0;
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-11-22 21:12:31 +00:00
|
|
|
var openFileDescription = new OpenFileDescription(fileNode.id, flags, position);
|
2013-11-27 00:12:25 +00:00
|
|
|
var fd = fs.allocDescriptor(openFileDescription);
|
|
|
|
callback(null, fd);
|
2013-11-21 00:24:49 +00:00
|
|
|
}
|
|
|
|
}
|
2013-07-19 15:22:23 +00:00
|
|
|
|
2013-11-21 00:24:49 +00:00
|
|
|
flags = validate_flags(flags);
|
|
|
|
if(!flags) {
|
2013-11-21 21:15:30 +00:00
|
|
|
callback(new EInvalid('flags is not valid'));
|
2013-11-21 00:24:49 +00:00
|
|
|
}
|
2013-07-12 15:11:05 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
open_file(context, path, flags, check_result);
|
|
|
|
}
|
|
|
|
|
|
|
|
function _close(fs, fd, callback) {
|
2013-11-27 00:12:25 +00:00
|
|
|
if(!_(fs.openFiles).has(fd)) {
|
2013-11-21 21:15:30 +00:00
|
|
|
callback(new EBadFileDescriptor('invalid file descriptor'));
|
2013-07-12 15:11:05 +00:00
|
|
|
} else {
|
2013-11-27 00:12:25 +00:00
|
|
|
fs.releaseDescriptor(fd);
|
|
|
|
callback(null);
|
2013-07-12 15:11:05 +00:00
|
|
|
}
|
2013-11-22 21:12:31 +00:00
|
|
|
}
|
2013-07-12 15:11:05 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
function _mkdir(context, path, callback) {
|
2013-11-21 00:24:49 +00:00
|
|
|
function check_result(error) {
|
|
|
|
if(error) {
|
2013-11-21 21:15:30 +00:00
|
|
|
callback(error);
|
2013-11-21 00:24:49 +00:00
|
|
|
} else {
|
2013-11-27 00:12:25 +00:00
|
|
|
callback(null);
|
2013-11-21 00:24:49 +00:00
|
|
|
}
|
|
|
|
}
|
2013-03-11 19:14:26 +00:00
|
|
|
|
2013-11-21 00:24:49 +00:00
|
|
|
make_directory(context, path, check_result);
|
2013-11-22 21:12:31 +00:00
|
|
|
}
|
2013-05-31 01:44:32 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
function _rmdir(context, path, callback) {
|
2013-11-21 00:24:49 +00:00
|
|
|
function check_result(error) {
|
|
|
|
if(error) {
|
2013-11-21 21:15:30 +00:00
|
|
|
callback(error);
|
2013-11-21 00:24:49 +00:00
|
|
|
} else {
|
2013-11-27 00:12:25 +00:00
|
|
|
callback(null);
|
2013-11-21 00:24:49 +00:00
|
|
|
}
|
|
|
|
}
|
2013-05-31 01:44:32 +00:00
|
|
|
|
2013-11-21 00:24:49 +00:00
|
|
|
remove_directory(context, path, check_result);
|
2013-11-22 21:12:31 +00:00
|
|
|
}
|
2013-07-12 15:11:05 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
function _stat(context, name, path, callback) {
|
2013-11-21 00:24:49 +00:00
|
|
|
function check_result(error, result) {
|
|
|
|
if(error) {
|
2013-11-21 21:15:30 +00:00
|
|
|
callback(error);
|
2013-11-21 00:24:49 +00:00
|
|
|
} else {
|
2013-11-22 21:12:31 +00:00
|
|
|
var stats = new Stats(result, name);
|
2013-11-27 00:12:25 +00:00
|
|
|
callback(null, stats);
|
2013-11-21 00:24:49 +00:00
|
|
|
}
|
|
|
|
}
|
2013-07-12 15:11:05 +00:00
|
|
|
|
2013-11-21 00:24:49 +00:00
|
|
|
stat_file(context, path, check_result);
|
2013-11-22 21:12:31 +00:00
|
|
|
}
|
2013-07-29 18:55:25 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
function _fstat(fs, context, fd, callback) {
|
2013-11-21 00:24:49 +00:00
|
|
|
function check_result(error, result) {
|
|
|
|
if(error) {
|
2013-11-21 21:15:30 +00:00
|
|
|
callback(error);
|
2013-11-21 00:24:49 +00:00
|
|
|
} else {
|
2013-11-22 21:12:31 +00:00
|
|
|
var stats = new Stats(result, fs.name);
|
2013-11-27 00:12:25 +00:00
|
|
|
callback(null, stats);
|
2013-11-21 00:24:49 +00:00
|
|
|
}
|
|
|
|
}
|
2013-07-29 18:55:25 +00:00
|
|
|
|
2013-11-27 00:12:25 +00:00
|
|
|
var ofd = fs.openFiles[fd];
|
2013-07-29 18:55:25 +00:00
|
|
|
|
2013-11-21 00:24:49 +00:00
|
|
|
if(!ofd) {
|
2013-11-21 21:15:30 +00:00
|
|
|
callback(new EBadFileDescriptor('invalid file descriptor'));
|
2013-11-21 00:24:49 +00:00
|
|
|
} else {
|
|
|
|
fstat_file(context, ofd, check_result);
|
|
|
|
}
|
2013-11-22 21:12:31 +00:00
|
|
|
}
|
2013-07-12 15:11:05 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
function _link(context, oldpath, newpath, callback) {
|
2013-11-21 00:24:49 +00:00
|
|
|
function check_result(error) {
|
|
|
|
if(error) {
|
2013-11-21 21:15:30 +00:00
|
|
|
callback(error);
|
2013-11-21 00:24:49 +00:00
|
|
|
} else {
|
2013-11-27 00:12:25 +00:00
|
|
|
callback(null);
|
2013-11-21 00:24:49 +00:00
|
|
|
}
|
|
|
|
}
|
2013-07-23 03:34:38 +00:00
|
|
|
|
2013-11-21 00:24:49 +00:00
|
|
|
link_node(context, oldpath, newpath, check_result);
|
2013-11-22 21:12:31 +00:00
|
|
|
}
|
2013-07-12 15:11:05 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
function _unlink(context, path, callback) {
|
2013-11-21 00:24:49 +00:00
|
|
|
function check_result(error) {
|
|
|
|
if(error) {
|
2013-11-21 21:15:30 +00:00
|
|
|
callback(error);
|
2013-11-21 00:24:49 +00:00
|
|
|
} else {
|
2013-11-27 00:12:25 +00:00
|
|
|
callback(null);
|
2013-11-21 00:24:49 +00:00
|
|
|
}
|
|
|
|
}
|
2013-07-26 02:49:53 +00:00
|
|
|
|
2013-11-21 00:24:49 +00:00
|
|
|
unlink_node(context, path, check_result);
|
2013-11-22 21:12:31 +00:00
|
|
|
}
|
2013-07-12 15:11:05 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
function _read(fs, context, fd, buffer, offset, length, position, callback) {
|
2013-11-21 00:24:49 +00:00
|
|
|
offset = (undefined === offset) ? 0 : offset;
|
|
|
|
length = (undefined === length) ? buffer.length - offset : length;
|
2013-05-31 01:44:32 +00:00
|
|
|
|
2013-11-21 00:24:49 +00:00
|
|
|
function check_result(error, nbytes) {
|
|
|
|
if(error) {
|
2013-11-21 21:15:30 +00:00
|
|
|
callback(error);
|
2013-11-21 00:24:49 +00:00
|
|
|
} else {
|
2013-11-27 00:12:25 +00:00
|
|
|
callback(null, nbytes);
|
2013-11-21 00:24:49 +00:00
|
|
|
}
|
|
|
|
}
|
2013-07-12 15:11:05 +00:00
|
|
|
|
2013-11-27 00:12:25 +00:00
|
|
|
var ofd = fs.openFiles[fd];
|
2013-07-12 15:11:05 +00:00
|
|
|
|
2013-11-21 00:24:49 +00:00
|
|
|
if(!ofd) {
|
2013-11-21 21:15:30 +00:00
|
|
|
callback(new EBadFileDescriptor('invalid file descriptor'));
|
2013-11-21 00:24:49 +00:00
|
|
|
} else if(!_(ofd.flags).contains(O_READ)) {
|
2013-11-21 21:15:30 +00:00
|
|
|
callback(new EBadFileDescriptor('descriptor does not permit reading'));
|
2013-11-21 00:24:49 +00:00
|
|
|
} else {
|
|
|
|
read_data(context, ofd, buffer, offset, length, position, check_result);
|
|
|
|
}
|
2013-11-22 21:12:31 +00:00
|
|
|
}
|
2013-11-08 20:33:44 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
function _readFile(fs, context, path, options, callback) {
|
2013-11-21 00:24:49 +00:00
|
|
|
if(!options) {
|
|
|
|
options = { encoding: null, flag: 'r' };
|
|
|
|
} else if(typeof options === "function") {
|
|
|
|
callback = options;
|
|
|
|
options = { encoding: null, flag: 'r' };
|
|
|
|
} else if(typeof options === "string") {
|
|
|
|
options = { encoding: options, flag: 'r' };
|
|
|
|
}
|
2013-11-08 20:33:44 +00:00
|
|
|
|
2013-11-21 00:24:49 +00:00
|
|
|
var flags = validate_flags(options.flag || 'r');
|
|
|
|
if(!flags) {
|
2013-11-21 21:15:30 +00:00
|
|
|
callback(new EInvalid('flags is not valid'));
|
2013-11-21 00:24:49 +00:00
|
|
|
}
|
2013-11-08 20:33:44 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
open_file(context, path, flags, function(err, fileNode) {
|
2013-11-21 00:24:49 +00:00
|
|
|
if(err) {
|
2013-11-21 21:15:30 +00:00
|
|
|
return callback(err);
|
2013-11-21 00:24:49 +00:00
|
|
|
}
|
|
|
|
var ofd = new OpenFileDescription(fileNode.id, flags, 0);
|
2013-11-27 00:12:25 +00:00
|
|
|
var fd = fs.allocDescriptor(ofd);
|
2013-11-21 00:24:49 +00:00
|
|
|
|
|
|
|
fstat_file(context, ofd, function(err2, fstatResult) {
|
|
|
|
if(err2) {
|
2013-11-21 21:15:30 +00:00
|
|
|
return callback(err2);
|
2013-11-21 00:24:49 +00:00
|
|
|
}
|
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
var stats = new Stats(fstatResult, fs.name);
|
2013-11-21 00:24:49 +00:00
|
|
|
var size = stats.size;
|
|
|
|
var buffer = new Uint8Array(size);
|
|
|
|
|
|
|
|
read_data(context, ofd, buffer, 0, size, 0, function(err3, nbytes) {
|
|
|
|
if(err3) {
|
2013-11-21 21:15:30 +00:00
|
|
|
return callback(err3);
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-11-27 00:12:25 +00:00
|
|
|
fs.releaseDescriptor(fd);
|
2013-11-08 20:33:44 +00:00
|
|
|
|
2013-11-21 00:24:49 +00:00
|
|
|
var data;
|
|
|
|
if(options.encoding === 'utf8') {
|
|
|
|
data = new TextDecoder('utf-8').decode(buffer);
|
|
|
|
} else {
|
|
|
|
data = buffer;
|
|
|
|
}
|
2013-11-27 00:12:25 +00:00
|
|
|
callback(null, data);
|
2013-11-21 00:24:49 +00:00
|
|
|
});
|
|
|
|
});
|
2013-11-08 20:33:44 +00:00
|
|
|
|
2013-11-21 00:24:49 +00:00
|
|
|
});
|
2013-11-22 21:12:31 +00:00
|
|
|
}
|
2013-11-08 20:33:44 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
function _write(fs, context, fd, buffer, offset, length, position, callback) {
|
2013-11-21 00:24:49 +00:00
|
|
|
offset = (undefined === offset) ? 0 : offset;
|
|
|
|
length = (undefined === length) ? buffer.length - offset : length;
|
|
|
|
|
|
|
|
function check_result(error, nbytes) {
|
|
|
|
if(error) {
|
2013-11-21 21:15:30 +00:00
|
|
|
callback(error);
|
2013-11-21 00:24:49 +00:00
|
|
|
} else {
|
2013-11-27 00:12:25 +00:00
|
|
|
callback(null, nbytes);
|
2013-11-21 00:24:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-27 00:12:25 +00:00
|
|
|
var ofd = fs.openFiles[fd];
|
2013-11-21 00:24:49 +00:00
|
|
|
|
|
|
|
if(!ofd) {
|
2013-11-21 21:15:30 +00:00
|
|
|
callback(new EBadFileDescriptor('invalid file descriptor'));
|
2013-11-21 00:24:49 +00:00
|
|
|
} else if(!_(ofd.flags).contains(O_WRITE)) {
|
2013-11-21 21:15:30 +00:00
|
|
|
callback(new EBadFileDescriptor('descriptor does not permit writing'));
|
2013-11-21 00:24:49 +00:00
|
|
|
} else if(buffer.length - offset < length) {
|
2013-11-21 21:15:30 +00:00
|
|
|
callback(new EIO('intput buffer is too small'));
|
2013-11-21 00:24:49 +00:00
|
|
|
} else {
|
|
|
|
write_data(context, ofd, buffer, offset, length, position, check_result);
|
|
|
|
}
|
2013-11-22 21:12:31 +00:00
|
|
|
}
|
2013-05-31 01:44:32 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
function _writeFile(fs, context, path, data, options, callback) {
|
2013-11-21 00:24:49 +00:00
|
|
|
if(!options) {
|
|
|
|
options = { encoding: 'utf8', flag: 'w' };
|
|
|
|
} else if(typeof options === "function") {
|
|
|
|
callback = options;
|
|
|
|
options = { encoding: 'utf8', flag: 'w' };
|
|
|
|
} else if(typeof options === "string") {
|
|
|
|
options = { encoding: options, flag: 'w' };
|
|
|
|
}
|
2013-05-31 01:44:32 +00:00
|
|
|
|
2013-11-21 00:24:49 +00:00
|
|
|
var flags = validate_flags(options.flag || 'w');
|
|
|
|
if(!flags) {
|
2013-11-21 21:15:30 +00:00
|
|
|
callback(new EInvalid('flags is not valid'));
|
2013-11-21 00:24:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(typeof data === "string" && options.encoding === 'utf8') {
|
|
|
|
data = new TextEncoder('utf-8').encode(data);
|
|
|
|
}
|
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
open_file(context, path, flags, function(err, fileNode) {
|
2013-11-21 00:24:49 +00:00
|
|
|
if(err) {
|
2013-11-21 21:15:30 +00:00
|
|
|
return callback(err);
|
2013-11-21 00:24:49 +00:00
|
|
|
}
|
|
|
|
var ofd = new OpenFileDescription(fileNode.id, flags, 0);
|
2013-11-27 00:12:25 +00:00
|
|
|
var fd = fs.allocDescriptor(ofd);
|
2013-11-21 00:24:49 +00:00
|
|
|
|
|
|
|
write_data(context, ofd, data, 0, data.length, 0, function(err2, nbytes) {
|
|
|
|
if(err2) {
|
2013-11-21 21:15:30 +00:00
|
|
|
return callback(err2);
|
2013-11-08 20:33:44 +00:00
|
|
|
}
|
2013-11-27 00:12:25 +00:00
|
|
|
fs.releaseDescriptor(fd);
|
|
|
|
callback(null);
|
2013-11-21 00:24:49 +00:00
|
|
|
});
|
|
|
|
});
|
2013-11-22 21:12:31 +00:00
|
|
|
}
|
2013-11-21 00:24:49 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
function _getxattr(path, name, callback) {
|
|
|
|
// TODO
|
|
|
|
}
|
2013-07-12 15:11:05 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
function _setxattr(path, name, value, callback) {
|
|
|
|
// TODO
|
|
|
|
}
|
2013-05-31 01:44:32 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
function _lseek(fs, context, fd, offset, whence, callback) {
|
2013-11-21 00:24:49 +00:00
|
|
|
function check_result(error, offset) {
|
|
|
|
if(error) {
|
2013-11-21 21:15:30 +00:00
|
|
|
callback(error);
|
2013-11-21 00:24:49 +00:00
|
|
|
} else {
|
2013-11-21 21:15:30 +00:00
|
|
|
callback(offset);
|
2013-11-21 00:24:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function update_descriptor_position(error, stats) {
|
|
|
|
if(error) {
|
2013-11-21 21:15:30 +00:00
|
|
|
callback(error);
|
2013-11-21 00:24:49 +00:00
|
|
|
} else {
|
|
|
|
if(stats.size + offset < 0) {
|
2013-11-21 21:15:30 +00:00
|
|
|
callback(new EInvalid('resulting file offset would be negative'));
|
2013-07-22 17:08:35 +00:00
|
|
|
} else {
|
2013-11-21 00:24:49 +00:00
|
|
|
ofd.position = stats.size + offset;
|
2013-11-27 00:12:25 +00:00
|
|
|
callback(null, ofd.position);
|
2013-07-22 17:08:35 +00:00
|
|
|
}
|
2013-11-21 00:24:49 +00:00
|
|
|
}
|
|
|
|
}
|
2013-07-12 15:11:05 +00:00
|
|
|
|
2013-11-27 00:12:25 +00:00
|
|
|
var ofd = fs.openFiles[fd];
|
2013-11-21 00:24:49 +00:00
|
|
|
|
|
|
|
if(!ofd) {
|
2013-11-21 21:15:30 +00:00
|
|
|
callback(new EBadFileDescriptor('invalid file descriptor'));
|
2013-11-21 00:24:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if('SET' === whence) {
|
|
|
|
if(offset < 0) {
|
2013-11-21 21:15:30 +00:00
|
|
|
callback(new EInvalid('resulting file offset would be negative'));
|
2013-11-21 00:24:49 +00:00
|
|
|
} else {
|
|
|
|
ofd.position = offset;
|
2013-11-27 00:12:25 +00:00
|
|
|
callback(null, ofd.position);
|
2013-11-21 00:24:49 +00:00
|
|
|
}
|
|
|
|
} else if('CUR' === whence) {
|
|
|
|
if(ofd.position + offset < 0) {
|
2013-11-21 21:15:30 +00:00
|
|
|
callback(new EInvalid('resulting file offset would be negative'));
|
2013-11-21 00:24:49 +00:00
|
|
|
} else {
|
|
|
|
ofd.position += offset;
|
2013-11-27 00:12:25 +00:00
|
|
|
callback(null, ofd.position);
|
2013-11-21 00:24:49 +00:00
|
|
|
}
|
|
|
|
} else if('END' === whence) {
|
|
|
|
fstat_file(context, ofd, update_descriptor_position);
|
|
|
|
} else {
|
2013-11-21 21:15:30 +00:00
|
|
|
callback(new EInvalid('whence argument is not a proper value'));
|
2013-11-21 00:24:49 +00:00
|
|
|
}
|
2013-11-22 21:12:31 +00:00
|
|
|
}
|
2013-11-08 20:33:44 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
function _readdir(context, path, callback) {
|
2013-11-21 00:24:49 +00:00
|
|
|
function check_result(error, files) {
|
|
|
|
if(error) {
|
2013-11-21 21:15:30 +00:00
|
|
|
callback(error);
|
2013-11-21 00:24:49 +00:00
|
|
|
} else {
|
2013-11-27 00:12:25 +00:00
|
|
|
callback(null, files);
|
2013-11-21 00:24:49 +00:00
|
|
|
}
|
|
|
|
}
|
2013-11-08 20:33:44 +00:00
|
|
|
|
2013-11-21 00:24:49 +00:00
|
|
|
read_directory(context, path, check_result);
|
2013-11-22 21:12:31 +00:00
|
|
|
}
|
2013-11-08 20:33:44 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
function _utimes(path, atime, mtime, callback) {
|
|
|
|
// TODO
|
|
|
|
}
|
2013-11-09 19:51:21 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
function _rename(context, oldpath, newpath, callback) {
|
|
|
|
function check_result(error) {
|
|
|
|
if(error) {
|
|
|
|
callback(error);
|
|
|
|
} else {
|
2013-11-27 00:12:25 +00:00
|
|
|
callback(null);
|
2013-11-22 21:12:31 +00:00
|
|
|
}
|
|
|
|
}
|
2013-11-08 20:33:44 +00:00
|
|
|
|
2013-11-21 00:24:49 +00:00
|
|
|
function unlink_old_node(error) {
|
|
|
|
if(error) {
|
2013-11-21 21:15:30 +00:00
|
|
|
callback(error);
|
2013-11-21 00:24:49 +00:00
|
|
|
} else {
|
|
|
|
unlink_node(context, oldpath, check_result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
link_node(context, oldpath, newpath, unlink_old_node);
|
|
|
|
}
|
|
|
|
|
|
|
|
function _symlink(context, srcpath, dstpath, callback) {
|
2013-11-21 00:24:49 +00:00
|
|
|
function check_result(error) {
|
|
|
|
if(error) {
|
2013-11-21 21:15:30 +00:00
|
|
|
callback(error);
|
2013-11-21 00:24:49 +00:00
|
|
|
} else {
|
2013-11-27 00:12:25 +00:00
|
|
|
callback(null);
|
2013-11-21 00:24:49 +00:00
|
|
|
}
|
|
|
|
}
|
2013-07-29 18:55:25 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
make_symbolic_link(context, srcpath, dstpath, check_result);
|
2013-11-21 00:24:49 +00:00
|
|
|
}
|
2013-11-22 21:12:31 +00:00
|
|
|
|
|
|
|
function _readlink(context, path, callback) {
|
|
|
|
function check_result(error, result) {
|
|
|
|
if(error) {
|
2013-11-21 00:24:49 +00:00
|
|
|
callback(error);
|
2013-11-22 21:12:31 +00:00
|
|
|
} else {
|
2013-11-27 00:12:25 +00:00
|
|
|
callback(null, result);
|
2013-11-22 21:12:31 +00:00
|
|
|
}
|
2013-11-21 00:24:49 +00:00
|
|
|
}
|
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
read_link(context, path, check_result);
|
|
|
|
}
|
2013-07-29 18:55:25 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
function _realpath(fd, length, callback) {
|
|
|
|
// TODO
|
|
|
|
}
|
2013-11-12 18:39:11 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
function _lstat(fs, context, path, callback) {
|
|
|
|
function check_result(error, result) {
|
|
|
|
if(error) {
|
|
|
|
callback(error);
|
|
|
|
} else {
|
|
|
|
var stats = new Stats(result, fs.name);
|
2013-11-27 00:12:25 +00:00
|
|
|
callback(null, stats);
|
2013-11-21 00:24:49 +00:00
|
|
|
}
|
2013-11-22 21:12:31 +00:00
|
|
|
}
|
2013-07-23 03:34:38 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
lstat_file(context, path, check_result);
|
|
|
|
}
|
2013-07-29 18:55:25 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
function _truncate(path, length, callback) {
|
|
|
|
// TODO
|
|
|
|
}
|
2013-07-29 19:59:17 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
function _ftruncate(fd, length, callback) {
|
|
|
|
// TODO
|
|
|
|
}
|
2013-07-29 18:55:25 +00:00
|
|
|
|
2013-11-21 00:24:49 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
/**
|
|
|
|
* Public API for FileSystem
|
|
|
|
*/
|
2013-11-21 00:24:49 +00:00
|
|
|
|
2013-11-22 21:12:31 +00:00
|
|
|
FileSystem.prototype.open = function(path, flags, callback) {
|
2013-11-21 00:24:49 +00:00
|
|
|
var fs = this;
|
2013-11-27 00:12:25 +00:00
|
|
|
var error = fs.queueOrRun(
|
2013-11-21 00:24:49 +00:00
|
|
|
function() {
|
2013-11-22 21:12:31 +00:00
|
|
|
var context = fs.provider.getReadWriteContext();
|
|
|
|
_open(fs, context, path, flags, callback);
|
2013-07-29 18:55:25 +00:00
|
|
|
}
|
|
|
|
);
|
2013-11-22 01:44:57 +00:00
|
|
|
if(error) callback(error);
|
2013-07-23 03:34:38 +00:00
|
|
|
};
|
2013-11-22 21:12:31 +00:00
|
|
|
FileSystem.prototype.close = function(fd, callback) {
|
|
|
|
_close(this, fd, callback);
|
2013-11-21 00:24:49 +00:00
|
|
|
};
|
2013-11-22 21:12:31 +00:00
|
|
|
FileSystem.prototype.mkdir = function(path, callback) {
|
2013-11-21 00:24:49 +00:00
|
|
|
var fs = this;
|
2013-11-27 00:12:25 +00:00
|
|
|
var error = fs.queueOrRun(
|
2013-11-07 20:55:14 +00:00
|
|
|
function() {
|
2013-11-22 21:12:31 +00:00
|
|
|
var context = fs.provider.getReadWriteContext();
|
|
|
|
_mkdir(context, path, callback);
|
2013-11-07 20:55:14 +00:00
|
|
|
}
|
|
|
|
);
|
2013-11-22 01:44:57 +00:00
|
|
|
if(error) callback(error);
|
2013-07-29 21:58:55 +00:00
|
|
|
};
|
2013-11-22 21:12:31 +00:00
|
|
|
FileSystem.prototype.rmdir = function(path, callback) {
|
2013-11-21 00:24:49 +00:00
|
|
|
var fs = this;
|
2013-11-27 00:12:25 +00:00
|
|
|
var error = fs.queueOrRun(
|
2013-11-21 00:24:49 +00:00
|
|
|
function() {
|
2013-11-22 21:12:31 +00:00
|
|
|
var context = fs.provider.getReadWriteContext();
|
|
|
|
_rmdir(context, path, callback);
|
2013-11-21 00:24:49 +00:00
|
|
|
}
|
|
|
|
);
|
2013-11-22 01:44:57 +00:00
|
|
|
if(error) callback(error);
|
2013-07-29 18:55:25 +00:00
|
|
|
};
|
2013-11-22 21:12:31 +00:00
|
|
|
FileSystem.prototype.stat = function(path, callback) {
|
2013-11-21 00:24:49 +00:00
|
|
|
var fs = this;
|
2013-11-27 00:12:25 +00:00
|
|
|
var error = fs.queueOrRun(
|
2013-11-12 18:39:11 +00:00
|
|
|
function() {
|
2013-11-22 21:12:31 +00:00
|
|
|
var context = fs.provider.getReadWriteContext();
|
|
|
|
_stat(context, fs.name, path, callback);
|
2013-11-12 18:39:11 +00:00
|
|
|
}
|
|
|
|
);
|
2013-11-22 01:44:57 +00:00
|
|
|
if(error) callback(error);
|
2013-07-29 18:55:25 +00:00
|
|
|
};
|
2013-11-22 21:12:31 +00:00
|
|
|
FileSystem.prototype.fstat = function(fd, callback) {
|
2013-11-21 00:24:49 +00:00
|
|
|
var fs = this;
|
2013-11-27 00:12:25 +00:00
|
|
|
var error = fs.queueOrRun(
|
2013-11-21 00:24:49 +00:00
|
|
|
function() {
|
2013-11-22 21:12:31 +00:00
|
|
|
var context = fs.provider.getReadWriteContext();
|
|
|
|
_fstat(fs, context, fd, callback);
|
2013-11-21 00:24:49 +00:00
|
|
|
}
|
|
|
|
);
|
2013-11-22 01:44:57 +00:00
|
|
|
if(error) callback(error);
|
2013-07-29 18:55:25 +00:00
|
|
|
};
|
2013-11-22 21:12:31 +00:00
|
|
|
FileSystem.prototype.link = function(oldpath, newpath, callback) {
|
2013-11-21 00:24:49 +00:00
|
|
|
var fs = this;
|
2013-11-27 00:12:25 +00:00
|
|
|
var error = fs.queueOrRun(
|
2013-11-21 00:24:49 +00:00
|
|
|
function() {
|
2013-11-22 21:12:31 +00:00
|
|
|
var context = fs.provider.getReadWriteContext();
|
|
|
|
_link(context, oldpath, newpath, callback);
|
2013-11-21 00:24:49 +00:00
|
|
|
}
|
|
|
|
);
|
2013-11-22 01:44:57 +00:00
|
|
|
if(error) callback(error);
|
2013-05-31 01:44:32 +00:00
|
|
|
};
|
2013-11-22 21:12:31 +00:00
|
|
|
FileSystem.prototype.unlink = function(path, callback) {
|
2013-11-21 00:24:49 +00:00
|
|
|
var fs = this;
|
2013-11-27 00:12:25 +00:00
|
|
|
var error = fs.queueOrRun(
|
2013-11-21 00:24:49 +00:00
|
|
|
function() {
|
2013-11-22 21:12:31 +00:00
|
|
|
var context = fs.provider.getReadWriteContext();
|
|
|
|
_unlink(context, path, callback);
|
2013-11-21 00:24:49 +00:00
|
|
|
}
|
|
|
|
);
|
2013-11-22 01:44:57 +00:00
|
|
|
if(error) callback(error);
|
2013-11-21 00:24:49 +00:00
|
|
|
};
|
2013-11-22 21:12:31 +00:00
|
|
|
FileSystem.prototype.read = function(fd, buffer, offset, length, position, callback) {
|
2013-11-21 00:24:49 +00:00
|
|
|
var fs = this;
|
2013-11-27 00:12:25 +00:00
|
|
|
var error = fs.queueOrRun(
|
2013-11-21 00:24:49 +00:00
|
|
|
function() {
|
2013-11-22 21:12:31 +00:00
|
|
|
var context = fs.provider.getReadWriteContext();
|
|
|
|
_read(fs, context, fd, buffer, offset, length, position, callback);
|
2013-11-21 00:24:49 +00:00
|
|
|
}
|
|
|
|
);
|
2013-11-22 01:44:57 +00:00
|
|
|
if(error) callback(error);
|
2013-11-21 00:24:49 +00:00
|
|
|
};
|
2013-11-22 21:12:31 +00:00
|
|
|
FileSystem.prototype.readFile = function(path, options, callback) {
|
2013-11-21 00:24:49 +00:00
|
|
|
var fs = this;
|
2013-11-27 00:12:25 +00:00
|
|
|
var error = fs.queueOrRun(
|
2013-11-21 00:24:49 +00:00
|
|
|
function() {
|
2013-11-22 21:12:31 +00:00
|
|
|
var context = fs.provider.getReadWriteContext();
|
|
|
|
_readFile(fs, context, path, options, callback);
|
2013-11-21 00:24:49 +00:00
|
|
|
}
|
|
|
|
);
|
2013-11-22 01:44:57 +00:00
|
|
|
if(error) callback(error);
|
2013-11-21 00:24:49 +00:00
|
|
|
};
|
2013-11-22 21:12:31 +00:00
|
|
|
FileSystem.prototype.write = function(fd, buffer, offset, length, position, callback) {
|
2013-11-21 00:24:49 +00:00
|
|
|
var fs = this;
|
2013-11-27 00:12:25 +00:00
|
|
|
var error = fs.queueOrRun(
|
2013-11-21 00:24:49 +00:00
|
|
|
function() {
|
2013-11-22 21:12:31 +00:00
|
|
|
var context = fs.provider.getReadWriteContext();
|
|
|
|
_write(fs, context, fd, buffer, offset, length, position, callback);
|
2013-11-21 00:24:49 +00:00
|
|
|
}
|
|
|
|
);
|
2013-11-22 21:12:31 +00:00
|
|
|
|
2013-11-22 01:44:57 +00:00
|
|
|
if(error) callback(error);
|
2013-11-21 00:24:49 +00:00
|
|
|
};
|
2013-11-22 21:12:31 +00:00
|
|
|
FileSystem.prototype.writeFile = function(path, data, options, callback) {
|
2013-11-21 00:24:49 +00:00
|
|
|
var fs = this;
|
2013-11-27 00:12:25 +00:00
|
|
|
var error = fs.queueOrRun(
|
2013-11-21 00:24:49 +00:00
|
|
|
function() {
|
2013-11-22 21:12:31 +00:00
|
|
|
var context = fs.provider.getReadWriteContext();
|
|
|
|
_writeFile(fs, context, path, data, options, callback);
|
2013-11-21 00:24:49 +00:00
|
|
|
}
|
|
|
|
);
|
2013-11-22 01:44:57 +00:00
|
|
|
if(error) callback(error);
|
2013-11-21 00:24:49 +00:00
|
|
|
};
|
2013-11-22 21:12:31 +00:00
|
|
|
FileSystem.prototype.lseek = function(fd, offset, whence, callback) {
|
2013-11-21 00:24:49 +00:00
|
|
|
var fs = this;
|
2013-11-27 00:12:25 +00:00
|
|
|
var error = fs.queueOrRun(
|
2013-11-21 00:24:49 +00:00
|
|
|
function() {
|
2013-11-22 21:12:31 +00:00
|
|
|
var context = fs.provider.getReadWriteContext();
|
|
|
|
_lseek(fs, context, fd, offset, whence, callback);
|
2013-11-21 00:24:49 +00:00
|
|
|
}
|
|
|
|
);
|
2013-11-22 01:44:57 +00:00
|
|
|
if(error) callback(error);
|
2013-11-21 00:24:49 +00:00
|
|
|
};
|
2013-11-22 21:12:31 +00:00
|
|
|
FileSystem.prototype.readdir = function(path, callback) {
|
2013-11-21 00:24:49 +00:00
|
|
|
var fs = this;
|
2013-11-27 00:12:25 +00:00
|
|
|
var error = fs.queueOrRun(
|
2013-11-21 00:24:49 +00:00
|
|
|
function() {
|
2013-11-22 21:12:31 +00:00
|
|
|
var context = fs.provider.getReadWriteContext();
|
|
|
|
_readdir(context, path, callback);
|
2013-11-21 00:24:49 +00:00
|
|
|
}
|
|
|
|
);
|
2013-11-22 01:44:57 +00:00
|
|
|
if(error) callback(error);
|
2013-11-21 00:24:49 +00:00
|
|
|
};
|
2013-11-22 21:12:31 +00:00
|
|
|
FileSystem.prototype.rename = function(oldpath, newpath, callback) {
|
2013-11-21 00:24:49 +00:00
|
|
|
var fs = this;
|
2013-11-27 00:12:25 +00:00
|
|
|
var error = fs.queueOrRun(
|
2013-11-21 00:24:49 +00:00
|
|
|
function() {
|
2013-11-22 21:12:31 +00:00
|
|
|
var context = fs.provider.getReadWriteContext();
|
|
|
|
_rename(context, oldpath, newpath, callback);
|
2013-11-21 00:24:49 +00:00
|
|
|
}
|
|
|
|
);
|
2013-11-22 01:44:57 +00:00
|
|
|
if(error) callback(error);
|
2013-07-29 21:58:55 +00:00
|
|
|
};
|
2013-11-22 21:12:31 +00:00
|
|
|
FileSystem.prototype.readlink = function(path, callback) {
|
|
|
|
var fs = this;
|
2013-11-27 00:12:25 +00:00
|
|
|
var error = fs.queueOrRun(
|
2013-11-22 21:12:31 +00:00
|
|
|
function() {
|
|
|
|
var context = fs.provider.getReadWriteContext();
|
|
|
|
_readlink(context, path, callback);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
if(error) callback(error);
|
2013-07-29 21:58:55 +00:00
|
|
|
};
|
2013-11-22 21:12:31 +00:00
|
|
|
FileSystem.prototype.symlink = function(srcpath, dstpath, callback) {
|
|
|
|
var fs = this;
|
2013-11-27 00:12:25 +00:00
|
|
|
var error = fs.queueOrRun(
|
2013-11-22 21:12:31 +00:00
|
|
|
function() {
|
|
|
|
var context = fs.provider.getReadWriteContext();
|
|
|
|
_symlink(context, srcpath, dstpath, callback);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
if(error) callback(error);
|
2013-07-29 21:58:55 +00:00
|
|
|
};
|
2013-11-22 21:12:31 +00:00
|
|
|
FileSystem.prototype.lstat = function(path, callback) {
|
|
|
|
var fs = this;
|
2013-11-27 00:12:25 +00:00
|
|
|
var error = fs.queueOrRun(
|
2013-11-22 21:12:31 +00:00
|
|
|
function() {
|
|
|
|
var context = fs.provider.getReadWriteContext();
|
|
|
|
_lstat(fs, context, path, callback);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
if(error) callback(error);
|
2013-07-29 21:58:55 +00:00
|
|
|
};
|
|
|
|
|
2013-05-31 01:44:32 +00:00
|
|
|
return {
|
2013-11-27 00:12:25 +00:00
|
|
|
FileSystem: FileSystem
|
2013-05-31 01:44:32 +00:00
|
|
|
};
|
2013-03-11 19:14:26 +00:00
|
|
|
|
|
|
|
});
|
2013-11-22 21:12:31 +00:00
|
|
|
|
2013-11-07 21:29:23 +00:00
|
|
|
var IDBFS = require( "src/fs" );
|
2013-03-11 19:14:26 +00:00
|
|
|
|
|
|
|
return IDBFS;
|
|
|
|
|
|
|
|
}));
|
|
|
|
|