Skip to content

Commit

Permalink
Merge a31a59a into 6a24f4a
Browse files Browse the repository at this point in the history
  • Loading branch information
lengfangbing committed Jul 18, 2022
2 parents 6a24f4a + a31a59a commit 09a88c8
Show file tree
Hide file tree
Showing 9 changed files with 987 additions and 3 deletions.
1 change: 0 additions & 1 deletion .gitignore
Expand Up @@ -3,6 +3,5 @@
node_modules
dist
build
lib
coverage
**/temp/**
2 changes: 2 additions & 0 deletions .npmignore
Expand Up @@ -23,3 +23,5 @@ rollup.*
node_modules
tsconfig.json
rollup.config.js
.travis.yml
coverage
351 changes: 351 additions & 0 deletions lib/cjs/index.js

Large diffs are not rendered by default.

347 changes: 347 additions & 0 deletions lib/esm/index.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions lib/iife/index.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/umd/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "@bell-crow/pipe-core",
"version": "1.4.0",
"version": "1.4.1",
"description": "process data like a pipeline",
"main": "lib/cjs/index.js",
"module": "lib/esm/index.js",
Expand Down
2 changes: 1 addition & 1 deletion src/test/index.test.ts → src/index.test.ts
@@ -1,5 +1,5 @@
/* eslint-disable no-undef */
import { createPipeCore } from '../core';
import { createPipeCore } from './core';

const _value = {
name: 'pipe-core',
Expand Down
283 changes: 283 additions & 0 deletions test/index.test.js
@@ -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]
});
});
});

0 comments on commit 09a88c8

Please sign in to comment.