Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ```javascript
- export let pipe = (data, filters) => {
- (typeof filters === 'function' ? [filters] : filters).map(
- filter =>
- data = typeof filter === 'string' ? pipe[filter](data) : filter(data)
- );
- return data;
- };
- // 设置第一个默认管道
- pipe.numeric = data => {
- if (typeof data === 'string') {
- data = data.replace(/[^\d]/g, '');
- return Number.parseInt(data);
- } else return data;
- };
- // 设置第二个默认管道
- pipe.toArray = data => {
- data = `${data}`;
- const arr = [];
- for (let i = 0; i < data.length; i++) {
- arr.push(data.at(i));
- }
- return arr;
- };
- // DEMO
- const a = pipe('happy 2016', [
- 'numeric', // 添加第一个管道处理
- 'toArray', // 添加第二个管道处理
- // 添加临时定义管道处理函数
- d => d.toString().replace(/,/g, '-')
- ]);
- console.log(a); // 2-0-1-6
- ```
Add Comment
Please, Sign In to add comment