From c48780f7c8a3917c8dccf94a9641696d7dc843f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ttore=20Leandro=20Tognoli?= Date: Sun, 11 Nov 2018 17:42:59 -0200 Subject: [PATCH 1/4] initial array functions --- src/array/index.ts | 29 +++++++++++++++++++ test/array.spec.ts | 72 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 src/array/index.ts create mode 100644 test/array.spec.ts diff --git a/src/array/index.ts b/src/array/index.ts new file mode 100644 index 0000000..8ffb120 --- /dev/null +++ b/src/array/index.ts @@ -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(acc: Array, array: Array): Array { + 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 +} \ No newline at end of file diff --git a/test/array.spec.ts b/test/array.spec.ts new file mode 100644 index 0000000..785f8d5 --- /dev/null +++ b/test/array.spec.ts @@ -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); + }) + }) +}) + From 3f767580d65f8526977fa17a2634c89b1df91fd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ttore=20Leandro=20Tognoli?= Date: Mon, 12 Nov 2018 22:51:13 -0200 Subject: [PATCH 2/4] generator --- src/generator/index.ts | 38 ++++++++++++++++++++++++++++++++++++++ test/range.spec.ts | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 src/generator/index.ts create mode 100644 test/range.spec.ts diff --git a/src/generator/index.ts b/src/generator/index.ts new file mode 100644 index 0000000..43025b2 --- /dev/null +++ b/src/generator/index.ts @@ -0,0 +1,38 @@ + +class Generator implements Iterable{ + + constructor( + private startAt: number, + private endAt: number, + private step: number + ) { + + } + + [Symbol.iterator](): Iterator { + const [startAt, endAt, step] = [ + this.startAt, + this.endAt, + this.step + ]; + let current = startAt; + return { + next(): IteratorResult { + 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 { + 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) +} \ No newline at end of file diff --git a/test/range.spec.ts b/test/range.spec.ts new file mode 100644 index 0000000..d0168b2 --- /dev/null +++ b/test/range.spec.ts @@ -0,0 +1,37 @@ +import { range } from '../src/generator' +import { expect } from 'chai' + +describe('generator', () => { + describe('range', () => { + it('range(10) should gen [0,1,2...,9]', () => { + const expected = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] + const result = Array.from(range(10)) + expect(result).to.deep.equal(expected) + }) + it('range(0,10) should gen [0,1,2...,9]', () => { + const expected = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] + const result = Array.from(range(0, 10)) + expect(result).to.deep.equal(expected) + }) + it('range(-5,5) should gen [-5,-4,...,4]', () => { + const expected = [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4] + const result = Array.from(range(-5, 5)) + expect(result).to.deep.equal(expected) + }) + it('range(5,-5) should gen [5,4,...,-4]', () => { + const expected = [5, 4, 3, 2, 1, 0, -1, -2, -3, -4] + const result = Array.from(range(5, -5)) + expect(result).to.deep.equal(expected) + }) + it('range(0,10,2) should gen [0,2,4,6,8]', () => { + const expected = [0, 2, 4, 6, 8] + const result = Array.from(range(0, 10, 2)) + expect(result).to.deep.equal(expected) + }) + it('range(10,0,-2) should gen [10,8,6,4,2]', () => { + const expected = [10, 8, 6, 4, 2] + const result = Array.from(range(10, 0, -2)) + expect(result).to.deep.equal(expected) + }) + }) +}) \ No newline at end of file From d90ae6be056bae70b37868847885d8eb5403a320 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ttore=20Leandro=20Tognoli?= Date: Fri, 16 Nov 2018 19:05:13 -0200 Subject: [PATCH 3/4] arrow functions at tests --- package.json | 8 ++++---- test/clone.spec.ts | 8 ++++---- test/fomart.spec.ts | 22 +++++++++++----------- test/getattr.spec.ts | 24 ++++++++++++------------ test/is-callable.spec.ts | 6 +++--- test/is-empty.spec.ts | 20 ++++++++++---------- test/is-string.spec.ts | 6 +++--- test/require.spec.ts | 12 ++++++------ test/setattr.spec.ts | 26 +++++++++++++------------- test/string.spec.ts | 16 ++++++++-------- 10 files changed, 74 insertions(+), 74 deletions(-) diff --git a/package.json b/package.json index 6bba296..4c2909a 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/test/clone.spec.ts b/test/clone.spec.ts index ac7821b..6bc6da4 100644 --- a/test/clone.spec.ts +++ b/test/clone.spec.ts @@ -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' @@ -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', diff --git a/test/fomart.spec.ts b/test/fomart.spec.ts index a2bbcb5..0e24905 100644 --- a/test/fomart.spec.ts +++ b/test/fomart.spec.ts @@ -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"); }) }) \ No newline at end of file diff --git a/test/getattr.spec.ts b/test/getattr.spec.ts index a303c3b..0597ec0 100644 --- a/test/getattr.spec.ts +++ b/test/getattr.spec.ts @@ -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() }) diff --git a/test/is-callable.spec.ts b/test/is-callable.spec.ts index 5c7a6d9..b38d48b 100644 --- a/test/is-callable.spec.ts +++ b/test/is-callable.spec.ts @@ -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); }) diff --git a/test/is-empty.spec.ts b/test/is-empty.spec.ts index 065c2a9..8be5b30 100644 --- a/test/is-empty.spec.ts +++ b/test/is-empty.spec.ts @@ -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); }) }) \ No newline at end of file diff --git a/test/is-string.spec.ts b/test/is-string.spec.ts index 1dd98ce..e0f7da0 100644 --- a/test/is-string.spec.ts +++ b/test/is-string.spec.ts @@ -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); }) }) \ No newline at end of file diff --git a/test/require.spec.ts b/test/require.spec.ts index bfacb0b..a6bb5b0 100644 --- a/test/require.spec.ts +++ b/test/require.spec.ts @@ -1,32 +1,32 @@ import { expect } from 'chai'; import { requireNotNull } from '../src/index'; -describe("#requireNotNull", function () { - it('should throw on undefined', function () { +describe("#requireNotNull", () => { + it('should throw on undefined', () => { let obj = undefined; const test = () => requireNotNull(obj, 'huehue undefined') expect(test).to.throw('huehue undefined') }) - it('should throw on null', function () { + it('should throw on null', () => { let obj = null; const test = () => requireNotNull(obj, 'huehue null') expect(test).to.throw('huehue null') }) - it('should not throw on object', function () { + it('should not throw on object', () => { let obj = {}; const test = () => requireNotNull(obj, 'huehue object') expect(test).to.not.throw('huehue null') }) - it('should not throw on number', function () { + it('should not throw on number', () => { let obj = 0; const test = () => requireNotNull(obj, 'huehue number') expect(test).to.not.throw('huehue number') }) - it('should not throw on object', function () { + it('should not throw on object', () => { let obj = 'huehue'; const test = () => requireNotNull(obj, 'huehue string') expect(test).to.not.throw('huehue string') diff --git a/test/setattr.spec.ts b/test/setattr.spec.ts index 1f6d8a2..65c7def 100644 --- a/test/setattr.spec.ts +++ b/test/setattr.spec.ts @@ -1,54 +1,54 @@ import { setattr } from '../src/index' import { expect } from 'chai' -describe("#setattr", function () { - it('when key exists should set the value', function () { +describe("#setattr", () => { + it('when key exists should set the value', () => { let obj = { 'key': 1 }; setattr(obj, 'key') expect(obj.key).to.equal(null) }) - it('when key exists should set the value', function () { + it('when key exists should set the value', () => { let obj = { 'key': null }; setattr(obj, 'key', 'value') expect(obj.key).to.equal('value') }) - it('when key doesn\'t exists should put the value', function () { + it('when key doesn\'t exists should put the value', () => { let obj = { 'key': null }; setattr(obj, 'key', 'value') expect(obj.key).to.equal('value') }) - it('when key is an array and doesn\'t exists should put the value recursively', function () { - const obj : any = {}; + it('when key is an array and doesn\'t exists should put the value recursively', () => { + const obj: any = {}; setattr(obj, ['key_1', 'key_2'], 'value') expect(obj.key_1.key_2).to.equal('value') }) - it('when key is an array and exists partially should throw an error', function () { + it('when key is an array and exists partially should throw an error', () => { let obj = { 'key_1': null } const test = () => setattr(obj, ['key_1', 'key_2'], 'value') expect(test).to.throw() }) - it('when obj isn\'t an object should throw an error', function () { + it('when obj isn\'t an object should throw an error', () => { let obj = 'not an object'; - const test = () => setattr(obj, ['key_1','key_2'], 'value') + const test = () => setattr(obj, ['key_1', 'key_2'], 'value') expect(test).to.throw() }) - it('when key is an array and exists partially should put the value with force', function () { + it('when key is an array and exists partially should put the value with force', () => { let obj = { 'key_1': null } setattr(obj, ['key_1', 'key_2'], 'value', true) expect(obj.key_1.key_2).to.equal('value') }) - it('force can\'t replace empty object', function () { + it('force can\'t replace empty object', () => { let empty = {} let obj = { 'key_1': empty } setattr(obj, ['key_1', 'key_2'], 'value', true) expect(obj.key_1).to.equal(empty) }) - it('when object is null should throw an error', function () { + it('when object is null should throw an error', () => { let obj = null; const test = () => setattr(obj, 'any_key', 'value') expect(test).to.throw() }) - it('when object is undefined should throw an error', function () { + it('when object is undefined should throw an error', () => { let obj = undefined; const test = () => setattr(obj, 'any_key', 'value') expect(test).to.throw() diff --git a/test/string.spec.ts b/test/string.spec.ts index 7cdd568..23fc331 100644 --- a/test/string.spec.ts +++ b/test/string.spec.ts @@ -1,26 +1,26 @@ import { expect } from 'chai' import { upperCase, lowerCase, upperCaseFirst, lowerCaseFirst } from '../src/string'; -describe('#upperCase',function(){ - it('when a string should return a upper case string',function(){ +describe('#upperCase', () => { + it('when a string should return a upper case string', () => { expect(upperCase('test')).to.equal('TEST') }) }) -describe('#lowerCase',function(){ - it('when a string should return a lower case string',function(){ +describe('#lowerCase', () => { + it('when a string should return a lower case string', () => { expect(lowerCase('TEST')).to.equal('test') }) }) -describe('#lowerCaseFisrt',function(){ - it('when a string should return a equal string with only the first letter lower',function(){ +describe('#lowerCaseFisrt', () => { + it('when a string should return a equal string with only the first letter lower', () => { expect(lowerCaseFirst('TeSt')).to.equal('teSt') }) }) -describe('#upperCaseFirst',function(){ - it('when a string should return a equal string with only the first letter upper',function(){ +describe('#upperCaseFirst', () => { + it('when a string should return a equal string with only the first letter upper', () => { expect(upperCaseFirst('tEsT')).to.equal('TEsT') }) }) \ No newline at end of file From b046d0a4c0894779cd06b51fef814a5e5122adc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ttore=20Leandro=20Tognoli?= Date: Fri, 16 Nov 2018 19:06:20 -0200 Subject: [PATCH 4/4] arrow function --- src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 2f8eb87..759b750 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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); }