mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-06-02 06:29:40 +08:00
Renamed sugar-tests, added underscore-typed-tests
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
/// <reference path="../sugar.d.ts" />
|
||||
/// <reference path="sugar.d.ts" />
|
||||
|
||||
'schfifty'.add(' five'); // - > schfifty five
|
||||
'dopamine'.insert('e', 3); // - > dopeamine
|
||||
@@ -45,7 +45,7 @@
|
||||
'jumpy'.each(); // - > ['j', 'u', 'm', 'p', 'y']
|
||||
'jumpy'.each(/[r-z]/); // - > ['u', 'y']
|
||||
'jumpy'.each(/[r-z]/, function (m) {
|
||||
// Called twice: "u", "y"
|
||||
// Called twice: "u", "y"
|
||||
});
|
||||
|
||||
'gonna get encoded!'.encodeBase64(); // - > 'Z29ubmEgZ2V0IGVuY29kZWQh'
|
||||
@@ -178,4 +178,170 @@
|
||||
'hello'.startsWith('HELL'); // - > false
|
||||
'hello'.startsWith('HELL', false); // - > true
|
||||
|
||||
'<p>just <b>some</b> text</p>'.stripTags(); // - > 'just some text'
|
||||
'<p>just <b>some</b> text</p>'.stripTags('p'); // - > 'just <b>some</b> text'
|
||||
|
||||
'man from the boondocks'.titleize(); // - > 'Man from the Boondocks'
|
||||
'x-men: the last stand'.titleize(); // - > 'X Men: The Last Stand'
|
||||
'TheManWithoutAPast'.titleize(); // - > 'The Man Without a Past'
|
||||
'raiders_of_the_lost_ark'.titleize(); // - > 'Raiders of the Lost Ark'
|
||||
|
||||
'lucky charms'.to(); // - > 'lucky charms'
|
||||
'lucky charms'.to(7); // - > 'lucky ch'
|
||||
|
||||
'153'.toNumber(); // - > 153
|
||||
'12,000'.toNumber(); // - > 12000
|
||||
'10px'.toNumber(); // - > 10
|
||||
'ff'.toNumber(16); // - > 255
|
||||
|
||||
' wasabi '.trim(); // - > 'wasabi'
|
||||
' wasabi '.trimLeft(); // - > 'wasabi '
|
||||
' wasabi '.trimRight(); // - > ' wasabi'
|
||||
|
||||
'just sittin on the dock of the bay'.truncate(20); // - > 'just sittin on the do...'
|
||||
'just sittin on the dock of the bay'.truncate(20, false); // - > 'just sittin on the...'
|
||||
'just sittin on the dock of the bay'.truncate(20, true, 'middle'); // - > 'just sitt...of the bay'
|
||||
'just sittin on the dock of the bay'.truncate(20, true, 'left'); // - > '...the dock of the bay'
|
||||
|
||||
'a-farewell-to-arms'.underscore(); // - > 'a_farewell_to_arms'
|
||||
'capsLock'.underscore(); // - > 'caps_lock'
|
||||
|
||||
'<p>some text</p>'.unescapeHTML(); // - > '<p>some text</p>'
|
||||
'one & two'.unescapeHTML(); // - > 'one & two'
|
||||
|
||||
'http%3A%2F%2Ffoo.com%2Fthe%20bar'.unescapeURL(); // - > 'http://foo.com/the bar'
|
||||
'http%3A%2F%2Ffoo.com%2Fthe%20bar'.unescapeURL(true); // - > 'http%3A%2F%2Ffoo.com%2Fthe bar'
|
||||
|
||||
'broken wear'.words(); // - > ['broken', 'wear']
|
||||
'broken wear'.words(function (w) {
|
||||
// Called twice: "broken", "wear"
|
||||
});
|
||||
|
||||
'??? YAMADA??!'.zenkaku(); // - > '??? YAMADA??!'
|
||||
'??? YAMADA??!'.zenkaku('a'); // - > '??? YAMADA??!'
|
||||
'??? YAMADA??!'.zenkaku('alphabet'); // - > '??? YAMADA??!'
|
||||
'?????! 25???!'.zenkaku('katakana', 'numbers'); // - > '?????! 25???!'
|
||||
'?????! 25???!'.zenkaku('k', 'n'); // - > '?????! 25???!'
|
||||
'?????! 25???!'.zenkaku('kn'); // - > '?????! 25???!'
|
||||
'?????! 25???!'.zenkaku('sp'); // - > '?????! 25???!'
|
||||
|
||||
// static
|
||||
//Number.random(50, 100); // - > ex.85
|
||||
//Number.random(50); // - > ex.27
|
||||
//Number.random(); // - > ex.0
|
||||
|
||||
(1000).abbr(); // - > "1k"
|
||||
(1000000).abbr(); // - > "1m"
|
||||
(1280).abbr(1); // - > "1.3k"
|
||||
|
||||
(1000).bytes(); // - > "1kB"
|
||||
(1000).bytes(2); // - > "0.98kB"
|
||||
((10).pow(20)).bytes(); // - > "90,949,470TB"
|
||||
((10).pow(20)).bytes(0, false); // - > "87EB"
|
||||
|
||||
(1000).bytes(); // - > "1kB"
|
||||
(1000).bytes(2); // - > "0.98kB"
|
||||
((10).pow(20)).bytes(); // - > "90,949,470TB"
|
||||
((10).pow(20)).bytes(0, false); // - > "87EB"
|
||||
|
||||
(3.241).ceil(); // - > 4
|
||||
(-3.241).ceil(); // - > -3
|
||||
(3.241).ceil(2); // - > 3.25
|
||||
(3748).ceil(-2); // - > 3800
|
||||
|
||||
(65).chr(); // - > "A"
|
||||
(75).chr(); // - > "K"
|
||||
|
||||
(8).downto(3); // - > [8, 7, 6, 5, 4, 3]
|
||||
(8).downto(3, function (n) {
|
||||
// This function is called 6 times receiving n as the value.
|
||||
});
|
||||
(8).downto(2, null, 2); // - > [8, 6, 4, 2]
|
||||
|
||||
(500).duration(); // - > '500 milliseconds'
|
||||
(1200).duration(); // - > '1 second'
|
||||
(75).minutes().duration(); // - > '1 hour'
|
||||
(75).minutes().duration('es'); // - > '1 hora'
|
||||
|
||||
(3.241).floor(); // - > 3
|
||||
(-3.841).floor(); // - > -4
|
||||
(3.241).floor(2); // - > 3.24
|
||||
(3748).floor(-2); // - > 3700
|
||||
|
||||
(56782).format(); //- > '56,782'
|
||||
(56782).format(2); // - > '56,782.00'
|
||||
(4388.43).format(2, ' '); // - > '4 388.43'
|
||||
(4388.43).format(2, '.', ','); // - > '4.388,43'
|
||||
|
||||
(255).hex(); // - > 'ff';
|
||||
(255).hex(4); // - > '00ff';
|
||||
(23654).hex(); // - > '5c66';
|
||||
|
||||
(6).isEven(); // - > true
|
||||
(17).isEven(); // - > false
|
||||
|
||||
(420).isInteger(); // - > true
|
||||
(4.5).isInteger(); // - > false
|
||||
|
||||
(6).isMultipleOf(2); // - > true
|
||||
(17).isMultipleOf(2); // - > false
|
||||
(32).isMultipleOf(4); // - > true
|
||||
(34).isMultipleOf(4); // - > false
|
||||
|
||||
(3).isOdd(); // - > true
|
||||
(18).isOdd(); // - > false
|
||||
|
||||
(64).log(2); // - > 6
|
||||
(9).log(3); // - > 2
|
||||
(5).log(); // - > 1.6094379124341003
|
||||
|
||||
(3).pow(3); // - > 27
|
||||
(-3).abs(); // - > 3
|
||||
(1024).sqrt(); // - > 32
|
||||
|
||||
(1000).metric(); // - > "1k"
|
||||
(1000000).metric(); // - > "1,000k"
|
||||
(1000000).metric(0, false); // - > "1M"
|
||||
(1249).metric(2) + 'g'; // - > "1.25kg"
|
||||
(0.025).metric() + 'm'; // - > "25mm"
|
||||
|
||||
(1).ordinalize(); // - > '1st'
|
||||
(2).ordinalize(); // - > '2nd'
|
||||
(8).ordinalize(); // - > '8th'
|
||||
|
||||
(5).pad(2); // - > '05'
|
||||
(-5).pad(4); // - > '-0005'
|
||||
(82).pad(3, true); // - > '+082'
|
||||
|
||||
(3.241).round(); // - > 3
|
||||
(-3.841).round(); // - > -4
|
||||
(3.241).round(2); // - > 3.24
|
||||
(3748).round(-2); // - > 3800
|
||||
|
||||
(8).times(function (i) {
|
||||
// This function is called 8 times.
|
||||
});
|
||||
|
||||
(420).toNumber(); // - > 420
|
||||
|
||||
(5).milliseconds(); // - > 5
|
||||
(10).hours(); // - > 36000000
|
||||
(1).day(); // - > 86400000
|
||||
|
||||
(5).daysAfter('tuesday'); // - > 5 days after tuesday of this week
|
||||
(1).yearAfter('January 23, 1997'); // - > January 23, 1998
|
||||
|
||||
(5).weeksAgo(); // - > 5 weeks ago
|
||||
(1).yearAgo(); // - > January 23, 1996
|
||||
|
||||
(5).daysBefore('tuesday'); // - > 5 days before tuesday of this week
|
||||
(1).yearBefore('January 23, 1997'); // - > January 23, 1996
|
||||
|
||||
(5).weeksFromNow(); // - > 5 weeks ago
|
||||
(1).yearFromNow(); // - > January 23, 1998
|
||||
|
||||
(2).upto(6); // - > [2, 3, 4, 5, 6]
|
||||
(2).upto(6, function (n) {
|
||||
// This function is called 5 times receiving n as the value.
|
||||
});
|
||||
(2).upto(8, null, 2); // - > [2, 4, 6, 8]
|
||||
944
sugar/sugar.d.ts
vendored
944
sugar/sugar.d.ts
vendored
File diff suppressed because it is too large
Load Diff
251
underscore/underscore-typed-tests.ts
Normal file
251
underscore/underscore-typed-tests.ts
Normal file
@@ -0,0 +1,251 @@
|
||||
/// <reference path="underscore-typed.d.ts" />
|
||||
|
||||
declare var $;
|
||||
|
||||
_.each([1, 2, 3], (num) => alert(num.toString()));
|
||||
_.each({ one: 1, two: 2, three: 3 }, (value: number, key?: string) => alert(value.toString()));
|
||||
|
||||
_.map([1, 2, 3], (num) => num * 3);
|
||||
_.map({ one: 1, two: 2, three: 3 }, (value: number, key?: string) => value * 3);
|
||||
|
||||
var sum = _.reduce([1, 2, 3], (memo, num) => memo + num, 0);
|
||||
|
||||
var list = [[0, 1], [2, 3], [4, 5]];
|
||||
var flat = _.reduceRight(list, (a, b) => a.concat(b), []);
|
||||
|
||||
var even = _.find([1, 2, 3, 4, 5, 6], (num) => num % 2 == 0);
|
||||
|
||||
var evens = _.filter([1, 2, 3, 4, 5, 6], (num) => num % 2 == 0);
|
||||
|
||||
var listOfPlays = [{ title: "Cymbeline", author: "Shakespeare", year: 1611 }, { title: "The Tempest", author: "Shakespeare", year: 1611 }, { title: "Other", author: "Not Shakespeare", year: 2012 }];
|
||||
_.where(listOfPlays, { author: "Shakespeare", year: 1611 });
|
||||
|
||||
var odds = _.reject([1, 2, 3, 4, 5, 6], (num) => num % 2 == 0);
|
||||
|
||||
_.all([true, 1, null, 'yes'], _.identity);
|
||||
|
||||
_.any([null, 0, 'yes', false]);
|
||||
|
||||
_.contains([1, 2, 3], 3);
|
||||
|
||||
_.invoke([[5, 1, 7], [3, 2, 1]], 'sort');
|
||||
|
||||
var stooges = [{ name: 'moe', age: 40 }, { name: 'larry', age: 50 }, { name: 'curly', age: 60 }];
|
||||
_.pluck(stooges, 'name');
|
||||
|
||||
_.max(stooges, (stooge) => stooge.age);
|
||||
|
||||
var numbers = [10, 5, 100, 2, 1000];
|
||||
_.min(numbers);
|
||||
|
||||
_.sortBy([1, 2, 3, 4, 5, 6], (num) => Math.sin(num));
|
||||
|
||||
|
||||
// not sure how this is typechecking at all.. Math.floor(e) is number not string..?
|
||||
_([1.3, 2.1, 2.4]).groupBy((e: number, i?: number, list?: number[]) => Math.floor(e));
|
||||
_.groupBy([1.3, 2.1, 2.4], (num: number) => Math.floor(num));
|
||||
_.groupBy(['one', 'two', 'three'], 'length');
|
||||
|
||||
_.countBy([1, 2, 3, 4, 5], (num) => num % 2 == 0 ? 'even' : 'odd');
|
||||
|
||||
_.shuffle([1, 2, 3, 4, 5, 6]);
|
||||
|
||||
// (function(){ return _.toArray(arguments).slice(1); })(1, 2, 3, 4);
|
||||
|
||||
_.size({ one: 1, two: 2, three: 3 });
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
_.first([5, 4, 3, 2, 1]);
|
||||
_.initial([5, 4, 3, 2, 1]);
|
||||
_.last([5, 4, 3, 2, 1]);
|
||||
_.rest([5, 4, 3, 2, 1]);
|
||||
_.compact([0, 1, false, 2, '', 3]);
|
||||
|
||||
_.flatten([1, 2, 3, 4]);
|
||||
_.flatten([1, <any>[2]]);
|
||||
|
||||
// typescript doesn't like the elements being different
|
||||
_.flatten([1, [2], <any>[3, <any>[[4]]]]);
|
||||
_.flatten([1, [2], <any>[3, <any>[[4]]]], true);
|
||||
_.without([1, 2, 1, 0, 3, 1, 4], 0, 1);
|
||||
_.union([1, 2, 3], [101, 2, 1, 10], [2, 1]);
|
||||
_.intersection([1, 2, 3], [101, 2, 1, 10], [2, 1]);
|
||||
_.difference([1, 2, 3, 4, 5], [5, 2, 10]);
|
||||
_.uniq([1, 2, 1, 3, 1, 4]);
|
||||
_.zip(['moe', 'larry', 'curly'], [30, 40, 50], [true, false, false]);
|
||||
_.object(['moe', 'larry', 'curly'], [30, 40, 50]);
|
||||
_.object([[<any>'moe', 30], [<any>'larry', 40], [<any>'curly', 50]]);
|
||||
_.indexOf([1, 2, 3], 2);
|
||||
_.lastIndexOf([1, 2, 3, 1, 2, 3], 2);
|
||||
_.sortedIndex([10, 20, 30, 40, 50], 35);
|
||||
_.range(10);
|
||||
_.range(1, 11);
|
||||
_.range(0, 30, 5);
|
||||
_.range(0, 30, 5);
|
||||
_.range(0);
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
var func = function (greeting) { return greeting + ': ' + this.name };
|
||||
// need a second var otherwise typescript thinks func signature is the above func type,
|
||||
// instead of the newly returned _bind => func type.
|
||||
var func2 = _.bind(func, { name: 'moe' }, 'hi');
|
||||
func2();
|
||||
|
||||
var buttonView = {
|
||||
label: 'underscore',
|
||||
onClick: function () { alert('clicked: ' + this.label); },
|
||||
onHover: function () { console.log('hovering: ' + this.label); }
|
||||
};
|
||||
_.bindAll(buttonView);
|
||||
$('#underscore_button').bind('click', buttonView.onClick);
|
||||
|
||||
var fibonacci = _.memoize(function (n) {
|
||||
return n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2);
|
||||
});
|
||||
|
||||
var log = _.bind(console.log, console);
|
||||
_.delay(log, 1000, 'logged later');
|
||||
|
||||
_.defer(function () { alert('deferred'); });
|
||||
|
||||
var updatePosition = () => alert('updating position...');
|
||||
var throttled = _.throttle(updatePosition, 100);
|
||||
$(window).scroll(throttled);
|
||||
|
||||
var calculateLayout = () => alert('calculating layout...');
|
||||
var lazyLayout = _.debounce(calculateLayout, 300);
|
||||
$(window).resize(lazyLayout);
|
||||
|
||||
var createApplication = () => alert('creating application...');
|
||||
var initialize = _.once(createApplication);
|
||||
initialize();
|
||||
initialize();
|
||||
|
||||
var notes: any[];
|
||||
var render = () => alert("rendering...");
|
||||
var renderNotes = _.after(notes.length, render);
|
||||
_.each(notes, (note) => note.asyncSave({ success: renderNotes }));
|
||||
|
||||
var hello = function (name) { return "hello: " + name; };
|
||||
// can't use the same "hello" var otherwise typescript fails
|
||||
var hello2 = _.wrap(hello, (func) => { return "before, " + func("moe") + ", after"; });
|
||||
hello2();
|
||||
|
||||
var greet = function (name) { return "hi: " + name; };
|
||||
var exclaim = function (statement) { return statement + "!"; };
|
||||
var welcome = _.compose(exclaim, greet);
|
||||
welcome('moe');
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
_.keys({ one: 1, two: 2, three: 3 });
|
||||
_.values({ one: 1, two: 2, three: 3 });
|
||||
_.pairs({ one: 1, two: 2, three: 3 });
|
||||
_.invert({ Moe: "Moses", Larry: "Louis", Curly: "Jerome" });
|
||||
_.functions(_);
|
||||
_.extend({ name: 'moe' }, { age: 50 });
|
||||
_.pick({ name: 'moe', age: 50, userid: 'moe1' }, 'name', 'age');
|
||||
_.omit({ name: 'moe', age: 50, userid: 'moe1' }, 'userid');
|
||||
|
||||
var iceCream = { flavor: "chocolate" };
|
||||
_.defaults(iceCream, { flavor: "vanilla", sprinkles: "lots" });
|
||||
|
||||
_.clone({ name: 'moe' });
|
||||
|
||||
_.chain([1, 2, 3, 200])
|
||||
.filter(function (num) { return num % 2 == 0; })
|
||||
.tap(alert)
|
||||
.map(function (num) { return num * num })
|
||||
.value();
|
||||
|
||||
_.has({ a: 1, b: 2, c: 3 }, "b");
|
||||
|
||||
var moe = { name: 'moe', luckyNumbers: [13, 27, 34] };
|
||||
var clone = { name: 'moe', luckyNumbers: [13, 27, 34] };
|
||||
moe == clone;
|
||||
_.isEqual(moe, clone);
|
||||
|
||||
_.isEmpty([1, 2, 3]);
|
||||
_.isEmpty({});
|
||||
|
||||
_.isElement($('body')[0]);
|
||||
|
||||
(function () { return _.isArray(arguments); })();
|
||||
_.isArray([1, 2, 3]);
|
||||
|
||||
_.isObject({});
|
||||
_.isObject(1);
|
||||
|
||||
|
||||
// (() => { return _.isArguments(arguments); })(1, 2, 3);
|
||||
_.isArguments([1, 2, 3]);
|
||||
|
||||
_.isFunction(alert);
|
||||
|
||||
_.isString("moe");
|
||||
|
||||
_.isNumber(8.4 * 5);
|
||||
|
||||
_.isFinite(-101);
|
||||
|
||||
_.isFinite(-Infinity);
|
||||
|
||||
_.isBoolean(null);
|
||||
|
||||
_.isDate(new Date());
|
||||
|
||||
_.isRegExp(/moe/);
|
||||
|
||||
_.isNaN(NaN);
|
||||
isNaN(undefined);
|
||||
_.isNaN(undefined);
|
||||
|
||||
_.isNull(null);
|
||||
_.isNull(undefined);
|
||||
|
||||
_.isUndefined((<any>window).missingVariable);
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
var underscore = _.noConflict();
|
||||
|
||||
var moe2 = { name: 'moe' };
|
||||
moe2 === _.identity(moe);
|
||||
|
||||
var genie;
|
||||
_(3).times(function (n) { genie.grantWishNumber(n); });
|
||||
|
||||
_.random(0, 100);
|
||||
|
||||
_.mixin({
|
||||
capitalize: function (string) {
|
||||
return string.charAt(0).toUpperCase() + string.substring(1).toLowerCase();
|
||||
}
|
||||
});
|
||||
(<any>_("fabio")).capitalize();
|
||||
|
||||
_.uniqueId('contact_');
|
||||
|
||||
_.escape('Curly, Larry & Moe');
|
||||
|
||||
var object = { cheese: 'crumpets', stuff: function () { return 'nonsense'; } };
|
||||
_.result(object, 'cheese');
|
||||
|
||||
_.result(object, 'stuff');
|
||||
|
||||
var compiled = _.template("hello: <%= name %>");
|
||||
compiled({ name: 'moe' });
|
||||
var list2 = "<% _.each(people, function(name) { %> <li><%= name %></li> <% }); %>";
|
||||
_.template(list2, { people: ['moe', 'curly', 'larry'] });
|
||||
var template = _.template("<b><%- value %></b>");
|
||||
template({ value: '<script>' });
|
||||
var compiled2 = _.template("<% print('Hello ' + epithet); %>");
|
||||
compiled2({ epithet: "stooge" });
|
||||
_.templateSettings = {
|
||||
interpolate: /\{\{(.+?)\}\}/g
|
||||
};
|
||||
var template2 = _.template("Hello {{ name }}!");
|
||||
template2({ name: "Mustache" });
|
||||
_.template("Using 'with': <%= data.answer %>", { answer: 'no' }, { variable: 'data' });
|
||||
Reference in New Issue
Block a user