Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
987 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,5 @@ | |
node_modules | ||
dist | ||
build | ||
lib | ||
coverage | ||
**/temp/** |
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 |
---|---|---|
|
@@ -23,3 +23,5 @@ rollup.* | |
node_modules | ||
tsconfig.json | ||
rollup.config.js | ||
.travis.yml | ||
coverage |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,283 @@ | ||
/* eslint-disable no-undef */ | ||
const { createPipeCore } = require('../lib/cjs'); | ||
|
||
const _value = { | ||
name: 'pipe-core', | ||
age: 1, | ||
location: () => 'Asia', | ||
nick: { | ||
pipe: 1, | ||
core: 2 | ||
}, | ||
city: [1, 2, 3] | ||
}; | ||
|
||
const customStartFunction = { | ||
getName (value) { | ||
return value.name; | ||
}, | ||
getDoubleAge (value) { | ||
return value.age * 2; | ||
}, | ||
getLocation (value) { | ||
return value.location(); | ||
}, | ||
getNick (value) { | ||
return value.nick; | ||
}, | ||
getCity (value) { | ||
return value.city; | ||
}, | ||
getValue (value) { | ||
return value; | ||
} | ||
}; | ||
|
||
const sleep = async (time = 3000) => new Promise(resolve => { | ||
setTimeout(() => resolve(), time); | ||
}); | ||
|
||
test('test async process', async () => { | ||
const valueCore = createPipeCore(_value, customStartFunction); | ||
let a = 0; | ||
await valueCore | ||
.getName(async name => { | ||
a++; | ||
await sleep(); | ||
return `changed ${name}`; | ||
}) | ||
.pipe(name => { | ||
expect(a).toBe(1); | ||
a++; | ||
expect(name).toBe('changed pipe-core'); | ||
}) | ||
.getValue(value => { | ||
expect(a).toBe(2); | ||
const { location, ...val } = value; | ||
expect(val).toEqual({ | ||
name: 'pipe-core', | ||
age: 1, | ||
nick: { | ||
pipe: 1, | ||
core: 2 | ||
}, | ||
city: [1, 2, 3] | ||
}); | ||
}) | ||
.pipeEnd(); | ||
}); | ||
|
||
test('test setValue process', async () => { | ||
const valueCore = createPipeCore(_value, customStartFunction); | ||
let a = 0; | ||
await valueCore | ||
.getName(async name => { | ||
a++; | ||
await sleep(); | ||
return `changed ${name}`; | ||
}) | ||
.pipe(name => { | ||
expect(a).toBe(1); | ||
a++; | ||
expect(name).toBe('changed pipe-core'); | ||
}) | ||
.getValue((value, _, set) => { | ||
expect(a).toBe(2); | ||
const { location, ...val } = value; | ||
expect(val).toEqual({ | ||
name: 'pipe-core', | ||
age: 1, | ||
nick: { | ||
pipe: 1, | ||
core: 2 | ||
}, | ||
city: [1, 2, 3] | ||
}); | ||
set({ age: 100, name: 'set age' }); | ||
}) | ||
.pipeEnd(); | ||
|
||
await valueCore | ||
.getName(name => { | ||
expect(name).toBe('set age'); | ||
}) | ||
.getDoubleAge(age => { | ||
expect(age).toBe(200); | ||
}) | ||
.pipe((_, __, set) => { | ||
set({ age: 1 }); | ||
}) | ||
.pipeEnd() | ||
.then(value => { | ||
const { location, ...val } = value; | ||
expect(val).toEqual({ | ||
name: 'set age', | ||
age: 1, | ||
nick: { | ||
pipe: 1, | ||
core: 2 | ||
}, | ||
city: [1, 2, 3] | ||
}); | ||
}); | ||
}); | ||
|
||
test('test empty value core', async () => { | ||
const valueCore = createPipeCore(_value); | ||
|
||
await valueCore | ||
.pipeEnd() | ||
.then(value => { | ||
const { location, ...val } = value; | ||
expect(val).toEqual({ | ||
name: 'pipe-core', | ||
age: 1, | ||
nick: { | ||
pipe: 1, | ||
core: 2 | ||
}, | ||
city: [1, 2, 3] | ||
}); | ||
}); | ||
}); | ||
|
||
test('test use piece pipe core process', async () => { | ||
const valueCore = createPipeCore(_value, customStartFunction); | ||
// 计数器 | ||
let count = 0; | ||
await valueCore | ||
.getName(name => { | ||
count++; | ||
expect(name).toBe('pipe-core'); | ||
return name; | ||
}) | ||
.pipe((name, piecePipe, set) => { | ||
expect(count).toBe(1); | ||
count++; | ||
set({ name: 'new pipe-core' }); | ||
piecePipe | ||
.getName(() => { | ||
expect(count).toBe(2); | ||
count++; | ||
}); | ||
}) | ||
.getValue((_, piecePipe) => { | ||
expect(count).toBe(3); | ||
count++; | ||
piecePipe | ||
.getName(name => { | ||
expect(count).toBe(4); | ||
count++; | ||
expect(name).toBe('new pipe-core'); | ||
return name; | ||
}) | ||
.pipe((name, piecePipe) => { | ||
expect(count).toBe(5); | ||
count++; | ||
piecePipe | ||
.getName(_name => { | ||
expect(count).toBe(6); | ||
count++; | ||
return _name; | ||
}) | ||
.pipe((_name, _, set) => { | ||
expect(count).toBe(7); | ||
count++; | ||
set({ name: 'pipe-core' }); | ||
}); | ||
}); | ||
}) | ||
.getName(name => { | ||
expect(count).toBe(8); | ||
count++; | ||
expect(name).toBe('pipe-core'); | ||
}) | ||
.pipeEnd() | ||
.then(value => { | ||
expect(count).toBe(9); | ||
const { location, ...rest } = value; | ||
expect(rest).toEqual({ | ||
name: 'pipe-core', | ||
age: 1, | ||
nick: { | ||
pipe: 1, | ||
core: 2 | ||
}, | ||
city: [1, 2, 3] | ||
}); | ||
}); | ||
}); | ||
|
||
test('test createPipeCore case', async () => { | ||
const valueCore = createPipeCore(_value, customStartFunction); | ||
|
||
await valueCore | ||
.getDoubleAge(doubleAge => { | ||
expect(doubleAge).toBe(2); | ||
return doubleAge / 2; | ||
}) | ||
.pipe(divideAge => { | ||
expect(divideAge).toBe(1); | ||
}) | ||
.getDoubleAge(doubleAge => { | ||
expect(doubleAge).toBe(2); | ||
}) | ||
.getName(name => { | ||
expect(name).toBe('pipe-core'); | ||
}) | ||
.getName(name => { | ||
expect(name).toBe('pipe-core'); | ||
return 'changed-pipe-core'; | ||
}) | ||
.pipe(changedName => { | ||
expect(changedName).toEqual('changed-pipe-core'); | ||
}) | ||
.getCity(city => { | ||
expect(city).toEqual([1, 2, 3]); | ||
return city.reverse(); | ||
}) | ||
.pipe((changedCity, _, update) => { | ||
expect(changedCity).toEqual([3, 2, 1]); | ||
update({ city: [3, 2, 1] }); | ||
}) | ||
.getCity(city => { | ||
return city; | ||
}) | ||
.pipe(city => { | ||
expect(city).toEqual([3, 2, 1]); | ||
}) | ||
.getLocation(location => { | ||
expect(location).toBe('Asia'); | ||
}) | ||
.pipe((val, _, update) => { | ||
expect(val).toBeUndefined(); | ||
update({ location: () => 'Europe' }); | ||
}) | ||
.getNick(nick => { | ||
return { | ||
pipe: nick.core, | ||
core: nick.pipe | ||
}; | ||
}) | ||
.pipe((nick, _, update) => { | ||
expect(nick).toEqual({ | ||
pipe: 2, | ||
core: 1 | ||
}); | ||
update({ nick: { pipe: 2, core: 1 } }); | ||
}) | ||
.pipeEnd() | ||
.then(val => { | ||
const { location, ...otherVal } = val; | ||
expect(location()).toBe('Europe'); | ||
expect(otherVal).toEqual({ | ||
name: 'pipe-core', | ||
age: 1, | ||
nick: { | ||
pipe: 2, | ||
core: 1 | ||
}, | ||
city: [3, 2, 1] | ||
}); | ||
}); | ||
}); |