Skip to content

Commit 30dde78

Browse files
authored
Merge pull request #12 from HuehueJS/array
Array
2 parents 4ee76a2 + b046d0a commit 30dde78

15 files changed

+251
-75
lines changed

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@
2727
"coverage": "nyc report --reporter=text-lcov | codeclimate-test-reporter "
2828
},
2929
"devDependencies": {
30-
"@types/chai": "^4.1.4",
30+
"@types/chai": "^4.1.7",
3131
"@types/mocha": "^5.2.5",
32-
"chai": "^4.1.2",
33-
"chai-string": "^1.4.0",
32+
"chai": "^4.2.0",
33+
"chai-string": "^1.5.0",
3434
"codeclimate-test-reporter": "^0.5.0",
3535
"mocha": "^5.2.0",
3636
"nyc": "^12.0.2",
37-
"ts-node": "^7.0.0",
37+
"ts-node": "^7.0.1",
3838
"typescript": "^3.1.3"
3939
},
4040
"directories": {

src/array/index.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
const reduceToObject = (acc, [k, v]) => Object.assign(acc, { [k as string]: v });
3+
4+
const transformValue = it => ([k, v]) => ([k, it(v)]);
5+
6+
const setAtObject = obj => ([k, v]) => obj[k] = v;
7+
8+
const combine = ([k, v]) => v.map(it => [k, it]);
9+
10+
const swap = ([k, v]) => [v, k];
11+
12+
function flat<E>(acc: Array<E>, array: Array<E>): Array<E> {
13+
return acc.concat(array)
14+
}
15+
16+
export const Mappers = {
17+
Value: transformValue,
18+
Combine: combine,
19+
Swap: swap
20+
}
21+
22+
export const Reducers = {
23+
Object: reduceToObject,
24+
Flat: flat
25+
}
26+
27+
export const Iter = {
28+
SetAt: setAtObject
29+
}

src/generator/index.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
class Generator implements Iterable<number>{
3+
4+
constructor(
5+
private startAt: number,
6+
private endAt: number,
7+
private step: number
8+
) {
9+
10+
}
11+
12+
[Symbol.iterator](): Iterator<number> {
13+
const [startAt, endAt, step] = [
14+
this.startAt,
15+
this.endAt,
16+
this.step
17+
];
18+
let current = startAt;
19+
return {
20+
next(): IteratorResult<number> {
21+
while (current != endAt) {
22+
const value = current;
23+
current += step;
24+
return { done: false, value }
25+
}
26+
return { done: true, value: null }
27+
}
28+
}
29+
}
30+
31+
}
32+
33+
export function range(arg0, arg1 = null, arg2 = null): Iterable<number> {
34+
const startAt = arg1 === null ? 0 : arg0
35+
const endAt = arg1 === null ? arg0 : arg1;
36+
const step = arg2 !== null ? arg2 : (startAt <= endAt ? 1 : -1)
37+
return new Generator(startAt, endAt, step)
38+
}

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export const isNullOrUndefined = (obj: any) => [null, undefined].indexOf(obj) !== -1;
22

3-
export const requireNotNull = function (obj: any, message: string) {
3+
export const requireNotNull = (obj: any, message: string) => {
44
if (isNullOrUndefined(obj)) {
55
throw Error(message);
66
}

test/array.spec.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { expect } from 'chai';
2+
import { Mappers, Reducers, Iter } from "../src/array"
3+
4+
describe("Mappers", () => {
5+
describe("Value", () => {
6+
it("map only the value", () => {
7+
const origin = { a: 1, b: 2 };
8+
const expected = [['a', 2], ['b', 4]];
9+
const result =
10+
Object.entries(origin)
11+
.map(Mappers.Value(it => it * 2))
12+
expect(result).to.deep.equal(expected);
13+
})
14+
})
15+
describe("Swap", () => {
16+
it("swap values", () => {
17+
const origin = [[1, 'a'], [2, 'b']];
18+
const expected = [['a', 1], ['b', 2]];
19+
const result = origin.map(Mappers.Swap);
20+
expect(result).to.deep.equal(expected);
21+
})
22+
})
23+
describe("Compine", () => {
24+
it('Cartesian product', () => {
25+
const origin = [['a', [1, 2, 3]], ['b', [3, 2, 1]]];
26+
const expected = [
27+
[
28+
['a', 1],
29+
['a', 2],
30+
['a', 3],
31+
], [
32+
['b', 3],
33+
['b', 2],
34+
['b', 1],
35+
]
36+
]
37+
const result = origin.map(Mappers.Combine);
38+
expect(result).to.deep.equal(expected);
39+
})
40+
})
41+
})
42+
describe("Reducers", () => {
43+
describe("Object", () => {
44+
it("", () => {
45+
const origin = { a: 1, b: 2 };
46+
const result =
47+
Object.entries(origin)
48+
.reduce(Reducers.Object, {})
49+
expect(result).to.deep.equal(origin)
50+
})
51+
})
52+
describe("Flat", () => {
53+
it("", () => {
54+
const origin = [[1, 2], [3, 4], [5, 6]];
55+
const expected = [1, 2, 3, 4, 5, 6];
56+
const result = origin.reduce(Reducers.Flat);
57+
expect(result).to.deep.equal(expected);
58+
})
59+
})
60+
})
61+
describe("Iter", () => {
62+
describe("SetAt", () => {
63+
it("", () => {
64+
const origin = { a: 1, b: 2 };
65+
const result = {}
66+
Object.entries(origin)
67+
.forEach(Iter.SetAt(result))
68+
expect(result).to.deep.equal(origin);
69+
})
70+
})
71+
})
72+

test/clone.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { expect } from 'chai';
22
import { clone } from '../src/index';
33

4-
describe("#clone", function () {
5-
it('should have all keys when clone an obj', function () {
4+
describe("#clone", () => {
5+
it('should have all keys when clone an obj', () => {
66
const obj = {
77
first_name: 'Bruce',
88
last_name: 'Banner'
99
};
1010
expect(clone(obj)).to.have.all.keys('first_name', 'last_name');
1111
})
12-
it('should not modify original object', function () {
12+
it('should not modify original object', () => {
1313
const obj = {
1414
first_name: 'Bruce',
1515
last_name: 'Banner'
@@ -18,7 +18,7 @@ describe("#clone", function () {
1818
cloned_obj.first_name = 'Clint';
1919
expect(cloned_obj.first_name).to.not.equal(obj.first_name);
2020
})
21-
it('should not modify nested original object', function () {
21+
it('should not modify nested original object', () => {
2222
const obj = {
2323
person: {
2424
first_name: 'Bruce',

test/fomart.spec.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
import { expect } from 'chai';
22
import { format } from '../src/string/index';
33

4-
describe("#formatstr", function() {
5-
it("return Hello with first name", function() {
6-
expect(format("Hello {firstName}",{firstName: 'Clint'})).to.equal("Hello Clint");
4+
describe("#formatstr", () => {
5+
it("return Hello with first name", () => {
6+
expect(format("Hello {firstName}", { firstName: 'Clint' })).to.equal("Hello Clint");
77
})
8-
it("return Hello with first and last name ", function() {
9-
expect(format("Hello {firstName} {lastName}",{firstName: 'Clint', lastName: 'Barton'})).to.equal("Hello Clint Barton");
8+
it("return Hello with first and last name ", () => {
9+
expect(format("Hello {firstName} {lastName}", { firstName: 'Clint', lastName: 'Barton' })).to.equal("Hello Clint Barton");
1010
})
11-
it("return Hello with name of a person ", function() {
12-
expect(format("Hello {person.name}",{person: {name: 'Clint Barton'}})).to.equal("Hello Clint Barton");
11+
it("return Hello with name of a person ", () => {
12+
expect(format("Hello {person.name}", { person: { name: 'Clint Barton' } })).to.equal("Hello Clint Barton");
1313
})
14-
it("when data is empty return message", function() {
15-
expect(format("Hello {firstName}",{})).to.equal("Hello {firstName}");
14+
it("when data is empty return message", () => {
15+
expect(format("Hello {firstName}", {})).to.equal("Hello {firstName}");
1616
})
17-
it("when key not in data is return itself", function() {
18-
expect(format("Hello {firstName}, you\'ve {age} years old",{firstName: 'Clint'})).to.equal("Hello Clint, you've {age} years old");
17+
it("when key not in data is return itself", () => {
18+
expect(format("Hello {firstName}, you\'ve {age} years old", { firstName: 'Clint' })).to.equal("Hello Clint, you've {age} years old");
1919
})
2020
})

test/getattr.spec.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,48 @@
11
import { getattr } from '../src/index';
22
import { expect } from 'chai';
33

4-
describe('#getattr', function () {
5-
it('when key exists should return the value', function () {
4+
describe('#getattr', () => {
5+
it('when key exists should return the value', () => {
66
let obj = { 'key': 1 }
77
expect(getattr(obj, 'key')).to.equal(1)
88
})
9-
it('when key exists should return the value', function () {
9+
it('when key exists should return the value', () => {
1010
let obj = [1]
1111
expect(getattr(obj, 0)).to.equal(1)
1212
})
13-
it('when key is an array with a valid key should return the value', function () {
13+
it('when key is an array with a valid key should return the value', () => {
1414
let obj = { 'key': 1 }
1515
expect(getattr(obj, ['key'])).to.equal(1)
1616
})
17-
it('when key is an array with a valid key should return the value', function () {
17+
it('when key is an array with a valid key should return the value', () => {
1818
let obj = [1]
1919
expect(getattr(obj, [0])).to.equal(1)
2020
})
21-
it('when key is an array with recursive valid keys should return the value', function () {
21+
it('when key is an array with recursive valid keys should return the value', () => {
2222
let obj = { 'key_1': { 'key_2': 1 } }
2323
expect(getattr(obj, ['key_1', 'key_2'])).to.equal(1)
2424
})
25-
it('when key is an array with recursive valid keys should return the value', function () {
25+
it('when key is an array with recursive valid keys should return the value', () => {
2626
let obj = [[0, 0], [1, 0]]
2727
expect(getattr(obj, [1, 0])).to.equal(1)
2828
})
29-
it('when key is an array with recursive valid keys should return the value', function () {
29+
it('when key is an array with recursive valid keys should return the value', () => {
3030
let obj = { 'key_1': [1] }
3131
expect(getattr(obj, ['key_1', 0])).to.equal(1)
3232
})
33-
it('when key doesn\'t exists should return the default value', function () {
33+
it('when key doesn\'t exists should return the default value', () => {
3434
let obj = { 'key': 1 }
3535
expect(getattr(obj, 'wrong_key', 'default')).to.equal('default')
3636
})
37-
it('when obj is null should return the default value', function () {
37+
it('when obj is null should return the default value', () => {
3838
let obj = null;
3939
expect(getattr(obj, 'key', 'default')).to.equal('default')
4040
})
41-
it('when obj is undefined should return the default value', function () {
41+
it('when obj is undefined should return the default value', () => {
4242
let obj = undefined;
4343
expect(getattr(obj, 'key', 'default')).to.equal('default')
4444
})
45-
it('when obj isn\'t a object or a array should throw an error', function () {
45+
it('when obj isn\'t a object or a array should throw an error', () => {
4646
let obj = 1;
4747
expect(() => getattr(obj, 'key')).to.throw()
4848
})

test/is-callable.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { expect } from 'chai';
22
import { isCallable } from '../src/index';
33

4-
describe("#isCallable", function() {
5-
it('when string return false', function(){
4+
describe("#isCallable", () => {
5+
it('when string return false', () => {
66
expect(isCallable('Banner')).to.equal(false);
77
})
8-
it('when function return true', function(){
8+
it('when function return true', () => {
99
const test = () => console.log('Banner');
1010
expect(isCallable(test)).to.equal(true);
1111
})

test/is-empty.spec.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
import { expect } from 'chai';
22
import { isEmpty } from '../src/index';
33

4-
describe("#isEmpty", function() {
5-
it('when array is empty return true', function(){
4+
describe("#isEmpty", () => {
5+
it('when array is empty return true', () => {
66
expect(isEmpty([])).to.equal(true);
77
})
8-
it('when array isn\'t empty return false', function(){
8+
it('when array isn\'t empty return false', () => {
99
expect(isEmpty(['Bruce'])).to.equal(false);
1010
})
11-
it('when object is empty return true', function(){
11+
it('when object is empty return true', () => {
1212
expect(isEmpty({})).to.equal(true);
1313
})
14-
it('when array isn\'t empty return false', function(){
15-
expect(isEmpty({name:'Bruce Banner'})).to.equal(false);
14+
it('when array isn\'t empty return false', () => {
15+
expect(isEmpty({ name: 'Bruce Banner' })).to.equal(false);
1616
})
17-
it('when null return true', function(){
17+
it('when null return true', () => {
1818
expect(isEmpty(null)).to.equal(true);
1919
})
20-
it('when null return true', function(){
20+
it('when null return true', () => {
2121
expect(isEmpty(undefined)).to.equal(true);
2222
})
23-
it('when 0 return true', function(){
23+
it('when 0 return true', () => {
2424
expect(isEmpty(0)).to.equal(true);
2525
})
26-
it('when 1 return false', function(){
26+
it('when 1 return false', () => {
2727
expect(isEmpty(1)).to.equal(false);
2828
})
2929
})

0 commit comments

Comments
 (0)