Skip to content

Commit 3cf473b

Browse files
Adds collectionsjs type.
1 parent 3411697 commit 3cf473b

File tree

4 files changed

+126
-0
lines changed

4 files changed

+126
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import Collection from 'collectionsjs';
2+
3+
const collectable = [
4+
{ name: 'Arya Stark', age: 9 },
5+
{ name: 'Bran Stark', age: 7 },
6+
{ name: 'Jon Snow', age: 14 }
7+
];
8+
9+
const collection = new Collection(collectable);
10+
11+
const item = { 6: 7 };
12+
13+
collection.add(item);
14+
collection.all();
15+
collection.average('age');
16+
collection.chunk(2).all();
17+
collection.collect(collectable);
18+
19+
const characters = [
20+
{ name: 'Ned Stark', age: 40},
21+
{ name: 'Catelyn Stark', age: 35}
22+
];
23+
24+
const array = ['a', 'b', 'c'];
25+
26+
collection.concat(characters);
27+
collection.contains(stark => stark.name === 'John Snow');
28+
collection.count();
29+
collection.each(t => t = 3);
30+
collection.filter(stark => stark.age === 14);
31+
collection.find('bran');
32+
collection.first(item => item.age > 7);
33+
collection.flatten(true);
34+
collection.get(2);
35+
collection.has({ name: 'Bran Stark', age: 7 });
36+
collection.join();
37+
collection.keys();
38+
collection.last();
39+
collection.map(stark => stark.name);
40+
collection.pluck('name');
41+
collection.push({name: 'Robb Stark', age: 17});
42+
collection.reduce((previous, current) => previous + current, 0);
43+
collection.reject(stark => stark.age < 14);
44+
collection.remove({name: 'Robb Stark', age: 17});
45+
collection.skip(2);
46+
collection.slice(1, 3);
47+
collection.sort();
48+
collection.sortBy('name');
49+
collection.stringify();
50+
collection.sum('age');
51+
collection.take(2);
52+
collection.macro('addToMembers', (collection, n) => collection.map((collectionItem: any) => collectionItem + n));
53+
collection.unique(s => s.grade);
54+
collection.values();
55+
collection.where('age', 14);
56+
collection.where(stark => stark.age === 14);
57+
collection.zip(array);

types/collectionsjs/index.d.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Type definitions for collectionsjs 0.3
2+
// Project: https://github.com/logaretm/collectionsjs#readme
3+
// Definitions by: Jaymeh <https://github.com/jaymeh>
4+
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
5+
// TypeScript Version: 3.1
6+
7+
export default class Collection {
8+
constructor(items?: any);
9+
add(item: any): Collection;
10+
all(): Collection;
11+
average(property?: string | ((property?: number) => number)): number;
12+
chunk(size: number): Collection;
13+
collect(collectable: any[]|string): Collection;
14+
concat(collection: any[]|Collection): Collection;
15+
contains(closure: ((item: any) => boolean)): boolean;
16+
count(): number;
17+
each(callback: (item: any) => void): Collection;
18+
filter(callback: (item: any) => any): Collection;
19+
find(item: any): number;
20+
first(callback?: ((item: any) => any)|null): any;
21+
flatten(deep?: boolean): Collection;
22+
get(index: number): any;
23+
has(item: any): boolean;
24+
join(separator?: string): string;
25+
keys(): Collection;
26+
last(callback?: ((item: any) => any)|null): any;
27+
map(callback: (item: any) => any): Collection;
28+
pluck(property: string): Collection;
29+
push(item: any): Collection;
30+
reduce(callback: (previous: any, current: any) => any, initial: any): any;
31+
reject(callback: (item: any) => any): Collection;
32+
remove(item: any): boolean;
33+
skip(count: number): Collection;
34+
slice(start: number, end?: number): Collection;
35+
sort(compare?: () => any): Collection;
36+
sortBy(property: string, order?: string): Collection;
37+
stringify(): string;
38+
sum(property?: string|null): any;
39+
take(count: number): Collection;
40+
macro(name: string, callback: (...args: any) => any): any;
41+
unique(callback?: string|null|((item: any) => any)): Collection;
42+
values(): Collection;
43+
where(callback: ((item: any) => any)|string, value?: any): Collection;
44+
zip(array: any[]|Collection): Collection;
45+
}

types/collectionsjs/tsconfig.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"compilerOptions": {
3+
"module": "commonjs",
4+
"lib": [
5+
"es6"
6+
],
7+
"noImplicitAny": true,
8+
"noImplicitThis": true,
9+
"strictNullChecks": true,
10+
"baseUrl": "../",
11+
"typeRoots": [
12+
"../"
13+
],
14+
"types": [],
15+
"noEmit": true,
16+
"forceConsistentCasingInFileNames": true,
17+
"strictFunctionTypes": true
18+
},
19+
"files": [
20+
"index.d.ts",
21+
"collectionsjs-tests.ts"
22+
]
23+
}

types/collectionsjs/tslint.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "extends": "dtslint/dt.json" }

0 commit comments

Comments
 (0)