Guest User

Untitled

a guest
Oct 18th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. ```javascript
  2. export let pipe = (data, filters) => {
  3. (typeof filters === 'function' ? [filters] : filters).map(
  4. filter =>
  5. data = typeof filter === 'string' ? pipe[filter](data) : filter(data)
  6. );
  7. return data;
  8. };
  9.  
  10. // 设置第一个默认管道
  11. pipe.numeric = data => {
  12. if (typeof data === 'string') {
  13. data = data.replace(/[^\d]/g, '');
  14. return Number.parseInt(data);
  15. } else return data;
  16. };
  17.  
  18. // 设置第二个默认管道
  19. pipe.toArray = data => {
  20. data = `${data}`;
  21. const arr = [];
  22. for (let i = 0; i < data.length; i++) {
  23. arr.push(data.at(i));
  24. }
  25. return arr;
  26. };
  27.  
  28. // DEMO
  29. const a = pipe('happy 2016', [
  30.  'numeric', // 添加第一个管道处理
  31.  'toArray', // 添加第二个管道处理
  32.  // 添加临时定义管道处理函数
  33.   d => d.toString().replace(/,/g, '-')
  34. ]);
  35. console.log(a); // 2-0-1-6
  36. ```
Add Comment
Please, Sign In to add comment