Merge pull request #2055 from olamothe/master

Add _.partition, _.property, _.constant, _.now,
This commit is contained in:
Masahiro Wakame
2014-04-21 13:53:09 +09:00
2 changed files with 58 additions and 1 deletions
+18
View File
@@ -67,6 +67,17 @@ _.shuffle([1, 2, 3, 4, 5, 6]);
_.size({ one: 1, two: 2, three: 3 });
_.partition<number>([0, 1, 2, 3, 4, 5], (num)=>{return num % 2 ==0});
interface Family {
name: string;
relation : string;
}
var isUncleMoe = _.matches<Family, boolean>({name : 'moe', relation : 'uncle'});
_.filter([{name: 'larry', relation : 'father'}, {name : 'moe', relation : 'uncle'}], isUncleMoe);
///////////////////////////////////////////////////////////////////////////////////////
_.first([5, 4, 3, 2, 1]);
@@ -204,6 +215,8 @@ _.isArray([1, 2, 3]);
_.isObject({});
_.isObject(1);
_.property('name')(moe);
// (() => { return _.isArguments(arguments); })(1, 2, 3);
_.isArguments([1, 2, 3]);
@@ -235,6 +248,11 @@ _.isUndefined((<any>window).missingVariable);
///////////////////////////////////////////////////////////////////////////////////////
var UncleMoe = {name: 'moe'};
_.constant(UncleMoe)();
typeof _.now() === "number";
var underscore = _.noConflict();
var moe2 = { name: 'moe' };
+40 -1
View File
@@ -1,4 +1,4 @@
// Type definitions for Underscore 1.5.2
// Type definitions for Underscore 1.6.0
// Project: http://underscorejs.org/
// Definitions by: Boris Yankov <https://github.com/borisyankov/>
// Definitions by: Josh Baldwin <https://github.com/jbaldwin/>
@@ -543,6 +543,19 @@ interface UnderscoreStatic {
* @return Number of values in `list`.
**/
size<T>(list: _.Collection<T>): number;
/**
* Split array into two arrays:
* one whose elements all satisfy predicate and one whose elements all do not satisfy predicate.
* @param array Array to split in two
* @param iterator Filter iterator function for each element in `array`.
* @param context `this` object in `iterator`, optional.
* @return Array where Array[0] are the elements in `array` that satisfies the predicate, and Array[1] the elements that did not.
**/
partition<T>(
array: Array<T>,
iterator: _.ListIterator<T, boolean>,
context?: any): T[][];
/*********
* Arrays *
@@ -1134,6 +1147,20 @@ interface UnderscoreStatic {
**/
has(object: any, key: string): boolean;
/**
* Returns a function that will itself return the key property of any passed-in object
* @param key Property of the object
* @return Function which accept an object an returns the value of key in that object
**/
property(key: string): (object: Object)=> any;
/**
* Returns a predicate function that will tell you if a passed in object contains all of the key/value properties present in attrs.
* @param attrs Object with key values pair
* @return Predicate function
**/
matches<T, TResult>(attrs: T): _.ListIterator<T, TResult>;
/**
* Performs an optimized deep comparison between the two objects,
* to determine if they should be considered equal.
@@ -1270,6 +1297,13 @@ interface UnderscoreStatic {
**/
identity<T>(value: T): T;
/**
* Creates a function that returns the same value that is used as the argument of _.constant
* @param value Identity of this object.
* @return Function that return value.
**/
constant<T>(value: T): () => T;
/**
* Invokes the given iterator function n times.
* Each invocation of iterator is called with an index argument
@@ -1353,6 +1387,11 @@ interface UnderscoreStatic {
**/
templateSettings: _.TemplateSettings;
/**
* Returns an integer timestamp for the current time, using the fastest method available in the runtime. Useful for implementing timing/animation functions.
**/
now(): number;
/* **********
* Chaining *
*********** */