Skip to content

Commit

Permalink
Merge pull request #12 from HuehueJS/array
Browse files Browse the repository at this point in the history
Array
  • Loading branch information
rafaelim authored Nov 21, 2018
2 parents 4ee76a2 + b046d0a commit 30dde78
Show file tree
Hide file tree
Showing 15 changed files with 251 additions and 75 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@
"coverage": "nyc report --reporter=text-lcov | codeclimate-test-reporter "
},
"devDependencies": {
"@types/chai": "^4.1.4",
"@types/chai": "^4.1.7",
"@types/mocha": "^5.2.5",
"chai": "^4.1.2",
"chai-string": "^1.4.0",
"chai": "^4.2.0",
"chai-string": "^1.5.0",
"codeclimate-test-reporter": "^0.5.0",
"mocha": "^5.2.0",
"nyc": "^12.0.2",
"ts-node": "^7.0.0",
"ts-node": "^7.0.1",
"typescript": "^3.1.3"
},
"directories": {
Expand Down
29 changes: 29 additions & 0 deletions src/array/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

const reduceToObject = (acc, [k, v]) => Object.assign(acc, { [k as string]: v });

const transformValue = it => ([k, v]) => ([k, it(v)]);

const setAtObject = obj => ([k, v]) => obj[k] = v;

const combine = ([k, v]) => v.map(it => [k, it]);

const swap = ([k, v]) => [v, k];

function flat<E>(acc: Array<E>, array: Array<E>): Array<E> {
return acc.concat(array)
}

export const Mappers = {
Value: transformValue,
Combine: combine,
Swap: swap
}

export const Reducers = {
Object: reduceToObject,
Flat: flat
}

export const Iter = {
SetAt: setAtObject
}
38 changes: 38 additions & 0 deletions src/generator/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

class Generator implements Iterable<number>{

constructor(
private startAt: number,
private endAt: number,
private step: number
) {

}

[Symbol.iterator](): Iterator<number> {
const [startAt, endAt, step] = [
this.startAt,
this.endAt,
this.step
];
let current = startAt;
return {
next(): IteratorResult<number> {
while (current != endAt) {
const value = current;
current += step;
return { done: false, value }
}
return { done: true, value: null }
}
}
}

}

export function range(arg0, arg1 = null, arg2 = null): Iterable<number> {
const startAt = arg1 === null ? 0 : arg0
const endAt = arg1 === null ? arg0 : arg1;
const step = arg2 !== null ? arg2 : (startAt <= endAt ? 1 : -1)
return new Generator(startAt, endAt, step)
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export const isNullOrUndefined = (obj: any) => [null, undefined].indexOf(obj) !== -1;

export const requireNotNull = function (obj: any, message: string) {
export const requireNotNull = (obj: any, message: string) => {
if (isNullOrUndefined(obj)) {
throw Error(message);
}
Expand Down
72 changes: 72 additions & 0 deletions test/array.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { expect } from 'chai';
import { Mappers, Reducers, Iter } from "../src/array"

describe("Mappers", () => {
describe("Value", () => {
it("map only the value", () => {
const origin = { a: 1, b: 2 };
const expected = [['a', 2], ['b', 4]];
const result =
Object.entries(origin)
.map(Mappers.Value(it => it * 2))
expect(result).to.deep.equal(expected);
})
})
describe("Swap", () => {
it("swap values", () => {
const origin = [[1, 'a'], [2, 'b']];
const expected = [['a', 1], ['b', 2]];
const result = origin.map(Mappers.Swap);
expect(result).to.deep.equal(expected);
})
})
describe("Compine", () => {
it('Cartesian product', () => {
const origin = [['a', [1, 2, 3]], ['b', [3, 2, 1]]];
const expected = [
[
['a', 1],
['a', 2],
['a', 3],
], [
['b', 3],
['b', 2],
['b', 1],
]
]
const result = origin.map(Mappers.Combine);
expect(result).to.deep.equal(expected);
})
})
})
describe("Reducers", () => {
describe("Object", () => {
it("", () => {
const origin = { a: 1, b: 2 };
const result =
Object.entries(origin)
.reduce(Reducers.Object, {})
expect(result).to.deep.equal(origin)
})
})
describe("Flat", () => {
it("", () => {
const origin = [[1, 2], [3, 4], [5, 6]];
const expected = [1, 2, 3, 4, 5, 6];
const result = origin.reduce(Reducers.Flat);
expect(result).to.deep.equal(expected);
})
})
})
describe("Iter", () => {
describe("SetAt", () => {
it("", () => {
const origin = { a: 1, b: 2 };
const result = {}
Object.entries(origin)
.forEach(Iter.SetAt(result))
expect(result).to.deep.equal(origin);
})
})
})

8 changes: 4 additions & 4 deletions test/clone.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { expect } from 'chai';
import { clone } from '../src/index';

describe("#clone", function () {
it('should have all keys when clone an obj', function () {
describe("#clone", () => {
it('should have all keys when clone an obj', () => {
const obj = {
first_name: 'Bruce',
last_name: 'Banner'
};
expect(clone(obj)).to.have.all.keys('first_name', 'last_name');
})
it('should not modify original object', function () {
it('should not modify original object', () => {
const obj = {
first_name: 'Bruce',
last_name: 'Banner'
Expand All @@ -18,7 +18,7 @@ describe("#clone", function () {
cloned_obj.first_name = 'Clint';
expect(cloned_obj.first_name).to.not.equal(obj.first_name);
})
it('should not modify nested original object', function () {
it('should not modify nested original object', () => {
const obj = {
person: {
first_name: 'Bruce',
Expand Down
22 changes: 11 additions & 11 deletions test/fomart.spec.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { expect } from 'chai';
import { format } from '../src/string/index';

describe("#formatstr", function() {
it("return Hello with first name", function() {
expect(format("Hello {firstName}",{firstName: 'Clint'})).to.equal("Hello Clint");
describe("#formatstr", () => {
it("return Hello with first name", () => {
expect(format("Hello {firstName}", { firstName: 'Clint' })).to.equal("Hello Clint");
})
it("return Hello with first and last name ", function() {
expect(format("Hello {firstName} {lastName}",{firstName: 'Clint', lastName: 'Barton'})).to.equal("Hello Clint Barton");
it("return Hello with first and last name ", () => {
expect(format("Hello {firstName} {lastName}", { firstName: 'Clint', lastName: 'Barton' })).to.equal("Hello Clint Barton");
})
it("return Hello with name of a person ", function() {
expect(format("Hello {person.name}",{person: {name: 'Clint Barton'}})).to.equal("Hello Clint Barton");
it("return Hello with name of a person ", () => {
expect(format("Hello {person.name}", { person: { name: 'Clint Barton' } })).to.equal("Hello Clint Barton");
})
it("when data is empty return message", function() {
expect(format("Hello {firstName}",{})).to.equal("Hello {firstName}");
it("when data is empty return message", () => {
expect(format("Hello {firstName}", {})).to.equal("Hello {firstName}");
})
it("when key not in data is return itself", function() {
expect(format("Hello {firstName}, you\'ve {age} years old",{firstName: 'Clint'})).to.equal("Hello Clint, you've {age} years old");
it("when key not in data is return itself", () => {
expect(format("Hello {firstName}, you\'ve {age} years old", { firstName: 'Clint' })).to.equal("Hello Clint, you've {age} years old");
})
})
24 changes: 12 additions & 12 deletions test/getattr.spec.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,48 @@
import { getattr } from '../src/index';
import { expect } from 'chai';

describe('#getattr', function () {
it('when key exists should return the value', function () {
describe('#getattr', () => {
it('when key exists should return the value', () => {
let obj = { 'key': 1 }
expect(getattr(obj, 'key')).to.equal(1)
})
it('when key exists should return the value', function () {
it('when key exists should return the value', () => {
let obj = [1]
expect(getattr(obj, 0)).to.equal(1)
})
it('when key is an array with a valid key should return the value', function () {
it('when key is an array with a valid key should return the value', () => {
let obj = { 'key': 1 }
expect(getattr(obj, ['key'])).to.equal(1)
})
it('when key is an array with a valid key should return the value', function () {
it('when key is an array with a valid key should return the value', () => {
let obj = [1]
expect(getattr(obj, [0])).to.equal(1)
})
it('when key is an array with recursive valid keys should return the value', function () {
it('when key is an array with recursive valid keys should return the value', () => {
let obj = { 'key_1': { 'key_2': 1 } }
expect(getattr(obj, ['key_1', 'key_2'])).to.equal(1)
})
it('when key is an array with recursive valid keys should return the value', function () {
it('when key is an array with recursive valid keys should return the value', () => {
let obj = [[0, 0], [1, 0]]
expect(getattr(obj, [1, 0])).to.equal(1)
})
it('when key is an array with recursive valid keys should return the value', function () {
it('when key is an array with recursive valid keys should return the value', () => {
let obj = { 'key_1': [1] }
expect(getattr(obj, ['key_1', 0])).to.equal(1)
})
it('when key doesn\'t exists should return the default value', function () {
it('when key doesn\'t exists should return the default value', () => {
let obj = { 'key': 1 }
expect(getattr(obj, 'wrong_key', 'default')).to.equal('default')
})
it('when obj is null should return the default value', function () {
it('when obj is null should return the default value', () => {
let obj = null;
expect(getattr(obj, 'key', 'default')).to.equal('default')
})
it('when obj is undefined should return the default value', function () {
it('when obj is undefined should return the default value', () => {
let obj = undefined;
expect(getattr(obj, 'key', 'default')).to.equal('default')
})
it('when obj isn\'t a object or a array should throw an error', function () {
it('when obj isn\'t a object or a array should throw an error', () => {
let obj = 1;
expect(() => getattr(obj, 'key')).to.throw()
})
Expand Down
6 changes: 3 additions & 3 deletions test/is-callable.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { expect } from 'chai';
import { isCallable } from '../src/index';

describe("#isCallable", function() {
it('when string return false', function(){
describe("#isCallable", () => {
it('when string return false', () => {
expect(isCallable('Banner')).to.equal(false);
})
it('when function return true', function(){
it('when function return true', () => {
const test = () => console.log('Banner');
expect(isCallable(test)).to.equal(true);
})
Expand Down
20 changes: 10 additions & 10 deletions test/is-empty.spec.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
import { expect } from 'chai';
import { isEmpty } from '../src/index';

describe("#isEmpty", function() {
it('when array is empty return true', function(){
describe("#isEmpty", () => {
it('when array is empty return true', () => {
expect(isEmpty([])).to.equal(true);
})
it('when array isn\'t empty return false', function(){
it('when array isn\'t empty return false', () => {
expect(isEmpty(['Bruce'])).to.equal(false);
})
it('when object is empty return true', function(){
it('when object is empty return true', () => {
expect(isEmpty({})).to.equal(true);
})
it('when array isn\'t empty return false', function(){
expect(isEmpty({name:'Bruce Banner'})).to.equal(false);
it('when array isn\'t empty return false', () => {
expect(isEmpty({ name: 'Bruce Banner' })).to.equal(false);
})
it('when null return true', function(){
it('when null return true', () => {
expect(isEmpty(null)).to.equal(true);
})
it('when null return true', function(){
it('when null return true', () => {
expect(isEmpty(undefined)).to.equal(true);
})
it('when 0 return true', function(){
it('when 0 return true', () => {
expect(isEmpty(0)).to.equal(true);
})
it('when 1 return false', function(){
it('when 1 return false', () => {
expect(isEmpty(1)).to.equal(false);
})
})
6 changes: 3 additions & 3 deletions test/is-string.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { expect } from 'chai';
import { isString } from '../src/string/index';

describe("#isString", function() {
it('when number return false', function(){
describe("#isString", () => {
it('when number return false', () => {
expect(isString(0)).to.equal(false);
})
it('when string return true', function(){
it('when string return true', () => {
expect(isString('Bruce')).to.equal(true);
})
})
Loading

0 comments on commit 30dde78

Please sign in to comment.