Remove compiling from _.invert and shimKeys.

Former-commit-id: 9fc384680960cf45f49f16e4cd84e3dca2b4cf19
This commit is contained in:
John-David Dalton
2012-10-14 01:42:44 -07:00
parent 82a6cfc03a
commit d736789bbc
2 changed files with 74 additions and 70 deletions

View File

@@ -94,7 +94,7 @@
'indexOf': ['sortedIndex'], 'indexOf': ['sortedIndex'],
'initial': [], 'initial': [],
'intersection': ['indexOf'], 'intersection': ['indexOf'],
'invert': [], 'invert': ['forOwn'],
'invoke': ['forEach'], 'invoke': ['forEach'],
'isArguments': [], 'isArguments': [],
'isArray': [], 'isArray': [],
@@ -113,7 +113,7 @@
'isRegExp': [], 'isRegExp': [],
'isString': [], 'isString': [],
'isUndefined': [], 'isUndefined': [],
'keys': ['isArguments'], 'keys': ['forOwn', 'isArguments'],
'last': [], 'last': [],
'lastIndexOf': [], 'lastIndexOf': [],
'lateBind': ['isFunction'], 'lateBind': ['isFunction'],

140
lodash.js
View File

@@ -745,24 +745,59 @@
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
/** /**
* Creates an object composed of the inverted keys and values of the given `object`. * 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 * @static
* @memberOf _ * @memberOf _
* @category Objects * @category Objects
* @param {Object} object The object to invert. * @param {Object} object The object to iterate over.
* @returns {Object} Returns the created inverted object. * @param {Function} callback The function called per iteration.
* @param {Mixed} [thisArg] The `this` binding of `callback`.
* @returns {Object} Returns `object`.
* @example * @example
* *
* _.invert({ 'first': 'Moe', 'second': 'Larry', 'third': 'Curly' }); * function Dog(name) {
* // => { 'Moe': 'first', 'Larry': 'second', 'Curly': 'third' } (order is not guaranteed) * 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 invert = createIterator({ var forIn = createIterator(baseIteratorOptions, forEachIteratorOptions, forOwnIteratorOptions, {
'args': 'object', 'useHas': false
'init': '{}',
'inLoop': 'result[value] = index'
}); });
/**
* 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);
/** /**
* Checks if `value` is an `arguments` object. * Checks if `value` is an `arguments` object.
* *
@@ -919,11 +954,13 @@
* @param {Object} object The object to inspect. * @param {Object} object The object to inspect.
* @returns {Array} Returns a new array of property names. * @returns {Array} Returns a new array of property names.
*/ */
var shimKeys = createIterator({ function shimKeys(object) {
'args': 'object', var result = [];
'init': '[]', forOwn(object, function(value, key) {
'inLoop': 'result.push(index)' result.push(key);
}); });
return result;
}
/** /**
* Used to convert characters to HTML entities: * Used to convert characters to HTML entities:
@@ -1087,60 +1124,6 @@
*/ */
var extend = createIterator(extendIteratorOptions); 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, * Creates a sorted array of all enumerable properties, own and inherited,
* of `object` that have function values. * of `object` that have function values.
@@ -1185,6 +1168,27 @@
return object ? hasOwnProperty.call(object, property) : false; return object ? hasOwnProperty.call(object, property) : false;
} }
/**
* 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)
*/
function invert(object) {
var result = {};
forOwn(object, function(value, key) {
result[value] = key;
});
return result;
}
/** /**
* Checks if `value` is a boolean (`true` or `false`) value. * Checks if `value` is a boolean (`true` or `false`) value.
* *