-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from HuehueJS/array
Array
- Loading branch information
Showing
15 changed files
with
251 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}) | ||
}) | ||
}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}) | ||
}) |
Oops, something went wrong.