From 6928dced9060c8479f6d23646853ee9a1210fac7 Mon Sep 17 00:00:00 2001 From: Brian Zengel Date: Tue, 22 Oct 2013 02:40:50 -0400 Subject: [PATCH] MAJOR!! code refactor to support some awesome chaining logic where only appropriate methods will be available to continue the chain. --- lodash/lodash-tests.ts | 229 +- lodash/lodash.d.ts | 6918 +++++++++++++++++++++------------------- 2 files changed, 3765 insertions(+), 3382 deletions(-) diff --git a/lodash/lodash-tests.ts b/lodash/lodash-tests.ts index e4ac60e3a..2161f4a95 100644 --- a/lodash/lodash-tests.ts +++ b/lodash/lodash-tests.ts @@ -85,43 +85,62 @@ var result; /************* * Chaining * *************/ -result = <_.LoDashWrapper>_('test'); -result = <_.LoDashWrapper>_(['test1', 'test2']); +result = >_('test'); +result = >_(1); +result = >_(true); +result = >_(['test1', 'test2']); +result = >>_({'key1': 'test1', 'key2': 'test2'}); -result = <_.LoDashWrapper>_('test').chain(); -result = <_.LoDashWrapper>_(['test1', 'test2']).chain(); -result = <_.LoDashWrapper>_.chain('test'); -result = <_.LoDashWrapper>_.chain(['test1', 'test2']); +result = >_.chain('test'); +result = >_('test').chain(); +result = >_.chain(1); +result = >_(1).chain(); +result = >_.chain(true); +result = >_(true).chain(); +result = >_.chain(['test1', 'test2']); +result = >_(['test1', 'test2']).chain(); +result = >>_.chain({'key1': 'test1', 'key2': 'test2'}); +result = >>_({'key1': 'test1', 'key2': 'test2'}).chain(); -result = <_.LoDashWrapper>_([1, 2, 3, 4]).concat([5, 6, 7]); -result = <_.LoDashWrapper>_([1, 2, 3, 4]).join(','); -result = <_.LoDashWrapper>_([1, 2, 3, 4]).pop(); +//Wrapped array shortcut methods +result = >_([1, 2, 3, 4]).concat(5, 6); +result = >_([1, 2, 3, 4]).join(','); +result = >_([1, 2, 3, 4]).pop(); _([1, 2, 3, 4]).push(5, 6, 7); -result = <_.LoDashWrapper>_([1, 2, 3, 4]).reverse(); -result = <_.LoDashWrapper>_([1, 2, 3, 4]).shift(); -result = <_.LoDashWrapper>_([1, 2, 3, 4]).slice(1, 2); -result = <_.LoDashWrapper>_([1, 2, 3, 4]).slice(2); -result = <_.LoDashWrapper>_([1, 2, 3, 4]).sort((a, b) => 1); -result = <_.LoDashWrapper>_([1, 2, 3, 4]).splice(1); -result = <_.LoDashWrapper>_([1, 2, 3, 4]).splice(1, 2, 5, 6); -result = <_.LoDashWrapper>_([1, 2, 3, 4]).unshift(5, 6); +result = >_([1, 2, 3, 4]).reverse(); +result = >_([1, 2, 3, 4]).shift(); +result = >_([1, 2, 3, 4]).slice(1, 2); +result = >_([1, 2, 3, 4]).slice(2); +result = >_([1, 2, 3, 4]).sort((a, b) => 1); +result = >_([1, 2, 3, 4]).splice(1); +result = >_([1, 2, 3, 4]).splice(1, 2, 5, 6); +result = >_([1, 2, 3, 4]).unshift(5, 6); result = _.tap([1, 2, 3, 4], function(array) { console.log(array); }); -result = <_.LoDashWrapper>_([1, 2, 3, 4]).tap(function(array) { console.log(array); }); +result = >_('test').tap(function(value) { console.log(value); }); +result = >_([1, 2, 3, 4]).tap(function(array) { console.log(array); }); +result = >>_({'key1': 'test1', 'key2': 'test2'}).tap(function(array) { console.log(array); }); +result = _('test').toString(); result = _([1, 2, 3]).toString(); +result = _({'key1': 'test1', 'key2': 'test2'}).toString(); +result = _('test').valueOf(); result = _([1, 2, 3]).valueOf(); - result = _([1, 2, 3]).value(); +result = >_({'key1': 'test1', 'key2': 'test2'}).valueOf(); -/************* - * Arrays * - *************/ +result = _('test').value(); +result = _([1, 2, 3]).value(); +result = >_({'key1': 'test1', 'key2': 'test2'}).value(); + +// /************* +// * Arrays * +// *************/ result = _.compact([0, 1, false, 2, '', 3]); - result = <_.LoDashWrapper>_([0, 1, false, 2, '', 3]).compact(); + result = >_([0, 1, false, 2, '', 3]).compact(); result = _.difference([1, 2, 3, 4, 5], [5, 2, 10]); -result = <_.LoDashWrapper>_([1, 2, 3, 4, 5]).difference([5, 2, 10]); + result = >_([1, 2, 3, 4, 5]).difference([5, 2, 10]); result = _.rest([1, 2, 3]); result = _.rest([1, 2, 3], 2); @@ -131,21 +150,21 @@ result = _.rest([1, 2, 3], function(num) { result = _.rest(foodsOrganic, 'test'); result = _.rest(foodsType, { 'test': 'value' }); - _.drop([1, 2, 3]); - _.drop([1, 2, 3], 2); - _.drop([1, 2, 3], function(num) { + result = _.drop([1, 2, 3]); + result = _.drop([1, 2, 3], 2); + result = _.drop([1, 2, 3], function(num) { return num < 3; }); - _.drop(foodsOrganic, 'test'); - _.drop(foodsType, { 'test': 'value' }); + result = _.drop(foodsOrganic, 'test'); + result = _.drop(foodsType, { 'test': 'value' }); - _.tail([1, 2, 3]); - _.tail([1, 2, 3], 2); - _.tail([1, 2, 3], function(num) { + result = _.tail([1, 2, 3]); + result = _.tail([1, 2, 3], 2); + result = _.tail([1, 2, 3], function(num) { return num < 3; }); - _.tail(foodsOrganic, 'test'); - _.tail(foodsType, { 'test': 'value' }); + result = _.tail(foodsOrganic, 'test'); + result = _.tail(foodsType, { 'test': 'value' }); result = _.findIndex(['apple', 'banana', 'beet'], function(f) { return /^b/.test(f); @@ -159,7 +178,6 @@ result = _.findLastIndex(['apple', 'banana', 'beet'], function(f) { result = _.findLastIndex(['apple', 'banana', 'beet'], 'apple'); result = _.findLastIndex([{ food: 'apple' }, { food: 'banana' }, { food: 'beet' }], { food: 'apple'}); - result = _.first([1, 2, 3]); result = _.first([1, 2, 3], 2); result = _.first([1, 2, 3], function(num) { @@ -168,29 +186,29 @@ result = _.first([1, 2, 3], function(num) { result = _.first(foodsOrganic, 'organic'); result = _.first(foodsType, { 'type': 'fruit' }); -result = _.head([1, 2, 3]); -result = _.head([1, 2, 3], 2); -result = _.head([1, 2, 3], function(num) { - return num < 3; -}); -result = _.head(foodsOrganic, 'organic'); -result = _.head(foodsType, { 'type': 'fruit' }); + result = _.head([1, 2, 3]); + result = _.head([1, 2, 3], 2); + result = _.head([1, 2, 3], function(num) { + return num < 3; + }); + result = _.head(foodsOrganic, 'organic'); + result = _.head(foodsType, { 'type': 'fruit' }); -result = _.take([1, 2, 3]); -result = _.take([1, 2, 3], 2); -result = _.take([1, 2, 3], function(num) { - return num < 3; -}); -result = _.take(foodsOrganic, 'organic'); -result = _.take(foodsType, { 'type': 'fruit' }); + result = _.take([1, 2, 3]); + result = _.take([1, 2, 3], 2); + result = _.take([1, 2, 3], function(num) { + return num < 3; + }); + result = _.take(foodsOrganic, 'organic'); + result = _.take(foodsType, { 'type': 'fruit' }); result = _.flatten([1, [2], [3, [[4]]]]); result = _.flatten([1, [2], [3, [[4]]]], true); result = _.flatten(stoogesQuotes, 'quotes'); - result = _([1, [2], [3, [[4]]]]).flatten(); - result = _([1, [2], [3, [[4]]]]).flatten(true); - result = _(stoogesQuotes).flatten('quotes'); + result = >_([1, [2], [3, [[4]]]]).flatten(); + result = >_([1, [2], [3, [[4]]]]).flatten(true); + result = >_(stoogesQuotes).flatten('quotes'); result = _.indexOf([1, 2, 3, 1, 2, 3], 2); result = _.indexOf([1, 2, 3, 1, 2, 3], 2, 3); @@ -263,14 +281,14 @@ result = <{x: number;}[]>_.uniq([{ 'x': 1 }, { 'x': 2 }, { 'x': 1 }], 'x'); result = _.unique([1, 2.5, 3, 1.5, 2, 3.5], function(num) { return this.floor(num); }, Math); result = <{x: number;}[]>_.unique([{ 'x': 1 }, { 'x': 2 }, { 'x': 1 }], 'x'); -result = _.unzip(['moe', 'larry'], [30, 40], [true, false]); - result = _.zip(['moe', 'larry'], [30, 40], [true, false]); - result = _.without([1, 2, 1, 0, 3, 1, 4], 0, 1); -/* ************* - * Collections * - ************* */ +result = _.zip(['moe', 'larry'], [30, 40], [true, false]); + result = _.unzip(['moe', 'larry'], [30, 40], [true, false]); + +// /* ************* +// * Collections * +// ************* */ result = _.at(['a', 'b', 'c', 'd', 'e'], [0, 2, 4]); result = _.at(['moe', 'larry', 'curly'], 0, 2); @@ -285,13 +303,13 @@ result = _.contains('curly', 'ur'); result = _.include({ 'name': 'moe', 'age': 40 }, 'moe'); result = _.include('curly', 'ur'); -result = <_.Dictionary>_.countBy([4.3, 6.1, 6.4], function(num) { return Math.floor(num); }); -result = <_.Dictionary>_.countBy([4.3, 6.1, 6.4], function(num) { return this.floor(num); }, Math); -result = <_.Dictionary>_.countBy(['one', 'two', 'three'], 'length'); +result = >_.countBy([4.3, 6.1, 6.4], function(num) { return Math.floor(num); }); +result = >_.countBy([4.3, 6.1, 6.4], function(num) { return this.floor(num); }, Math); +result = >_.countBy(['one', 'two', 'three'], 'length'); - result = <_.LoDashWrapper<_.Dictionary>>_([4.3, 6.1, 6.4]).countBy(function(num) { return Math.floor(num); }); - result = <_.LoDashWrapper<_.Dictionary>>_([4.3, 6.1, 6.4]).countBy(function(num) { return this.floor(num); }, Math); - result = <_.LoDashWrapper<_.Dictionary>>_(['one', 'two', 'three']).countBy('length'); + result = >>_([4.3, 6.1, 6.4]).countBy(function(num) { return Math.floor(num); }); + result = >>_([4.3, 6.1, 6.4]).countBy(function(num) { return this.floor(num); }, Math); + result = >>_(['one', 'two', 'three']).countBy('length'); result = _.every([true, 1, null, 'yes'], Boolean); result = _.every(stoogesAges, 'age'); @@ -313,9 +331,9 @@ result = _.filter(foodsCombined, { 'type': 'fruit' }); result = _.select(foodsCombined, 'organic'); result = _.select(foodsCombined, { 'type': 'fruit' }); - result = _([1, 2, 3, 4, 5, 6]).select(function(num) { return num % 2 == 0; }); - result = _(foodsCombined).select('organic'); - result = _(foodsCombined).select({ 'type': 'fruit' }); + result = _([1, 2, 3, 4, 5, 6]).select(function(num) { return num % 2 == 0; }); + result = _(foodsCombined).select('organic'); + result = _(foodsCombined).select({ 'type': 'fruit' }); result = _.find([1, 2, 3, 4], function(num) { return num % 2 == 0; @@ -342,24 +360,30 @@ result = _.findLast(foodsCombined, { 'type': 'vegetable' }); result = _.findLast(foodsCombined, 'organic'); result = _.forEach([1, 2, 3], function(num) { console.log(num); }); -result = <_.Dictionary>_.forEach({ 'one': 1, 'two': 2, 'three': 3 }, function(num) { console.log(num); }); +result = >_.forEach({ 'one': 1, 'two': 2, 'three': 3 }, function(num) { console.log(num); }); result = _.each([1, 2, 3], function(num) { console.log(num); }); - result = <_.Dictionary>_.each({ 'one': 1, 'two': 2, 'three': 3 }, function(num) { console.log(num); }); + result = >_.each({ 'one': 1, 'two': 2, 'three': 3 }, function(num) { console.log(num); }); + + result = >_([1, 2, 3]).forEach(function(num) { console.log(num); }); + result = >>_({ 'one': 1, 'two': 2, 'three': 3 }).forEach(function(num) { console.log(num); }); + + result = >_([1, 2, 3]).each(function(num) { console.log(num); }); + result = >>_({ 'one': 1, 'two': 2, 'three': 3 }).each(function(num) { console.log(num); }); result = _.forEachRight([1, 2, 3], function(num) { console.log(num); }); -result = <_.Dictionary>_.forEachRight({ 'one': 1, 'two': 2, 'three': 3 }, function(num) { console.log(num); }); +result = >_.forEachRight({ 'one': 1, 'two': 2, 'three': 3 }, function(num) { console.log(num); }); result = _.eachRight([1, 2, 3], function(num) { console.log(num); }); - result = <_.Dictionary>_.eachRight({ 'one': 1, 'two': 2, 'three': 3 }, function(num) { console.log(num); }); + result = >_.eachRight({ 'one': 1, 'two': 2, 'three': 3 }, function(num) { console.log(num); }); -result = <_.Dictionary>_.groupBy([4.2, 6.1, 6.4], function(num) { return Math.floor(num); }); -result = <_.Dictionary>_.groupBy([4.2, 6.1, 6.4], function(num) { return this.floor(num); }, Math); -result = <_.Dictionary>_.groupBy(['one', 'two', 'three'], 'length'); +result = >_.groupBy([4.2, 6.1, 6.4], function(num) { return Math.floor(num); }); +result = >_.groupBy([4.2, 6.1, 6.4], function(num) { return this.floor(num); }, Math); +result = >_.groupBy(['one', 'two', 'three'], 'length'); -result = <_.Dictionary>_.indexBy(keys, 'dir'); -result = <_.Dictionary>_.indexBy(keys, function(key) { return String.fromCharCode(key.code); }); -result = <_.Dictionary>_.indexBy(keys, function(key) { this.fromCharCode(key.code); }, String); +result = >_.indexBy(keys, 'dir'); +result = >_.indexBy(keys, function(key) { return String.fromCharCode(key.code); }); +result = >_.indexBy(keys, function(key) { this.fromCharCode(key.code); }, String); result = _.invoke([[5, 1, 7], [3, 2, 1]], 'sort'); result = _.invoke([123, 456], String.prototype.split, ''); @@ -407,7 +431,6 @@ result = _.reduce({ 'a': 1, 'b': 2, 'c': 3 }, function(result, num, key) { }, {}); result = _.reduceRight([[0, 1], [2, 3], [4, 5]], function(a, b) { return a.concat(b); }, []); - result = _.foldr([[0, 1], [2, 3], [4, 5]], function(a, b) { return a.concat(b); }, []); result = _.reject([1, 2, 3, 4, 5, 6], function(num) { return num % 2 == 0; }); @@ -514,21 +537,20 @@ var greet = function(formatted) { }; result = _.compose(greet, format); -result = <_.LoDashWrapper>_(greet).compose(format); +result = >_(greet).compose(format); var createCallbackObj = { name: 'Joe' }; result = <() => any>_.createCallback('name'); result = <() => boolean>_.createCallback(createCallbackObj); -result = <_.LoDashWrapper<() => any>>_('name').createCallback(); -result = <_.LoDashWrapper<() => boolean>>_(createCallbackObj).createCallback(); - +result = any>>_('name').createCallback(); +result = boolean>>_(createCallbackObj).createCallback(); result = _.curry(function(a, b, c) { - console.log(a + b + c); + console.log(a + b + c); }); -result = <_.LoDashWrapper>_(function(a, b, c) { - console.log(a + b + c); +result = >_(function(a, b, c) { + console.log(a + b + c); }).curry(); declare var source; @@ -543,23 +565,23 @@ source.addEventListener('message', _.debounce(function() {}, 250, { 'maxWait': 1000 }), false); -result = <_.LoDashWrapper>_(function() {}).debounce(150); +result = >_(function() {}).debounce(150); -jQuery('#postbox').on('click', <_.LoDashWrapper>_(function() {}).debounce(300, { +jQuery('#postbox').on('click', >_(function() {}).debounce(300, { 'leading': true, 'trailing': false })); -source.addEventListener('message', <_.LoDashWrapper>_(function() {}).debounce(250, { +source.addEventListener('message', >_(function() {}).debounce(250, { 'maxWait': 1000 }), false); result = _.defer(function() { console.log('deferred'); }); -result = <_.LoDashWrapper>_(function() { console.log('deferred'); }).defer(); +result = >_(function() { console.log('deferred'); }).defer(); var log = _.bind(console.log, console); result = _.delay(log, 1000, 'logged later'); -result = <_.LoDashWrapper>_(log).delay(1000, 'logged later'); +result = >_(log).delay(1000, 'logged later'); var fibonacci = _.memoize(function(n) { return n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2); @@ -614,8 +636,8 @@ result = _.assign({ 'name': 'moe' }, { 'age': 40 }, function(a, b) { return typeof a == 'undefined' ? b : a; }); -result = <_.LoDashWrapper>_({ 'name': 'moe' }).assign({ 'age': 40 }); -result = <_.LoDashWrapper>_({ 'name': 'moe' }).assign({ 'age': 40 }, function(a, b) { +result = >_({ 'name': 'moe' }).assign({ 'age': 40 }); +result = >_({ 'name': 'moe' }).assign({ 'age': 40 }, function(a, b) { return typeof a == 'undefined' ? b : a; }); @@ -624,8 +646,8 @@ result = <_.LoDashWrapper>_({ 'name': 'moe' }).assign({ 'age': 40 }, functi return typeof a == 'undefined' ? b : a; }); - result = <_.LoDashWrapper>_({ 'name': 'moe' }).extend({ 'age': 40 }); - result = <_.LoDashWrapper>_({ 'name': 'moe' }).extend({ 'age': 40 }, function(a, b) { + result = >_({ 'name': 'moe' }).extend({ 'age': 40 }); + result = >_({ 'name': 'moe' }).extend({ 'age': 40 }, function(a, b) { return typeof a == 'undefined' ? b : a; }); @@ -660,7 +682,6 @@ result = _.forInRight(new Dog('Dagny'), function(value, key) { console.log(key); }); - result = _.forOwn({ '0': 'zero', '1': 'one', 'length': 2 }, function(num, key) { console.log(key); }); @@ -670,7 +691,7 @@ result = _.forOwnRight({ '0': 'zero', '1': 'one', 'length': 2 }, function(n }); result = _.functions(_); - result = _.methods(_); + result = _.methods(_); result = _.has({ 'a': 1, 'b': 2, 'c': 3 }, 'b'); @@ -840,7 +861,7 @@ result = _.result(object, 'stuff'); var tempObject = {}; result = _.runInContext(tempObject); -result = <_.TemplateExecutor>_.template('hello <%= name %>'); +result = _.template('hello <%= name %>'); result = _.template('<%- value %>', { 'value': '