GithubHelp home page GithubHelp logo

currino's Introduction

Currino

npm version npm downloads gzip size: JS npm license

函数式风格的 TypeScript 实用工具库

📦 安装

通过 npm:

npm install currino

通过 unpkg CDN:

<script src="https://unpkg.com/[email protected]/dist/currino.iife.js"></script>

🔨 使用

ESM:

import { sum } from 'currino';

Node.JS:

const { sum } = require('currino');

Browser:

Currino.sum(1, 1);

📚 API

列表

forEach

forEach(iteratee, arrayLink);

调用 iteratee 遍历 arrayLink 中的每个元素, iteratee 调用 3 个参数: (value, index, arrayLink)。 如果迭代函数(iteratee)显式的返回 false ,迭代会提前退出。

参数

iteratee (Function): 每次迭代调用的函数。

arrayLink (ArrayLink): 一个用来迭代的列表。

返回

(*): 返回列表 arrayLink

示例

forEach(
  (value) => {
    console.log(value);
  },
  [1, 2]
);
// => 输出 '1' 和 '2'。

forEachRight

forEachRight(iteratee, arrayLink);

这个方法类似 forEach,不同之处在于,forEachRight是从右到左遍历每一个元素。

参数

iteratee (Function): 每次迭代调用的函数。

arrayLink (ArrayLink): 一个用来迭代的列表。

返回

(*): 返回列表 arrayLink

示例

forEachRight(
  (value) => {
    console.log(value);
  },
  [1, 2]
);
// => 输出 '2' 和 '1'。

对象

mapKeys

mapKeys(mapping, object);

这个方法创建一个对象,对象的值与object相同,并且 key 是通过mapping每个自身可枚举属性的字符串产生的。如果mapping是个函数,那么它调用 3 个参数:(value, key, object)。

参数

iteratee (Function): 每次迭代调用的函数。

object (Object): 一个用来迭代的对象。

返回

(*): 返回新对象。

示例

mapKeys(
  (value, key) => {
    return key + value;
  },
  { a: 1, b: 2 }
);
// => { a1: 1, a2: 2 }

eachTree

eachTree(iteratee, childrenKey, tree);

以深度优先的方式,调用 iteratee 遍历 tree(集合) 中的每个节点, iteratee 调用 3 个参数: (node, path, tree)。 如果迭代函数(iteratee)显式的返回 false ,迭代会提前退出。

参数

iteratee (Function): 每次迭代调用的函数。

childrenKey (string): 子节点属性名。

tree (Object): 一个用来迭代的树。

返回

(*): 返回树 tree

示例

const tree = {
  id: 1,
  children: [
    {
      id: 2,
      children: [{ id: 4 }],
    },
    { id: 3 },
  ],
};

eachTree(
  (node) => {
    console.log(node);
  },
  'children',
  tree
);
// => 依次输出 { id: 1 } 、{ id: 2 } 、{ id: 4 } 、{ id: 3 }。

findTree

findTree(predicate, childrenKey, tree);

遍历 tree(树)节点,返回 predicate(断言函数)第一个返回真值的第一个元素。predicate 调用 1 个参数: (node)。

参数

predicate (Function): 每次迭代调用的函数。

childrenKey (string): 子节点属性名。

tree (Object): 一个用来迭代的树。

返回

(*): 返回匹配节点,否则返回undefined

示例

const tree = {
  id: 1,
  children: [{ id: 2 }],
};

findTree((node) => node.id === 2, 'children', tree);
// => 输出 { id: 2 }。

mapTree

mapTree(iteratee, childrenKey, tree);

创建一个树,节点是iteratee遍历tree中的每个节点后返回的结果。iteratee 调用 1 个参数:(node)。

参数

iteratee (Function): 每次迭代调用的函数。

childrenKey (string): 子节点属性名。

tree (Object): 一个用来迭代的树。

返回

(*): 返回新的映射后树。

示例

const tree = {
  id: 1,
  children: [{ id: 2 }],
};

mapTree((node) => ({ key: node.id, children: node.children }), 'children', tree);
// => 输出 { key: 1, children: [{ key: 2 }] }。

searchTree

searchTree(predicate, childrenKey, tree);

遍历 tree(树)节点,返回 predicate(断言函数)所有匹配节点的路径。predicate 调用 1 个参数: (node)。

参数

predicate (Function): 每次迭代调用的函数。

childrenKey (string): 子节点属性名。

tree (Object): 一个用来迭代的树。

返回

(*): 返回匹配节点的路径节点,否则返回空数组。

示例

const tree = {
  id: 1,
  children: [
    { id: 2 },
    {
      id: 3,
      children: [{ id: 4 }],
    },
  ],
};

searchTree((node) => node.id === 4, 'children', tree);
// => 输出 { id: 1, children: [{ id: 3, children: [{ id: 4 }] }] }。

Promise

limit

这个方法可以控制异步函数的并发数,方法将在所有异步结束后,返回异步的结果集。

limit(max, tasks);

参数

max (number):最大并发数。

tasks (Function[]):异步函数集合。

返回

(*):一个 Promise,终值为所有异步函数执行的结果集。

示例

const tasks = [
  () => {
    return new Promise((resolve) => {
      setTimeout(() => resolve(1), 200);
    });
  },
  () => {
    return new Promise((resolve, reject) => {
      setTimeout(() => reject(2), 100);
    });
  },
];

limit(2, tasks); // => 输出 Promise<[{ status: 'fulfilled', value: 1 }, { status: 'rejected', reason: 2 }]>

实用函数

sleep

sleep(1000);

同步阻塞程序,等待一定时间后再执行(也许会有用 😝)。

参数

timeout (number):需要睡眠的毫秒数。

示例

sleep(1000);
console.log('hello currino!'); // => 延迟一秒后输出 hello currino!

函子

Identity

Identity(value);

创建一个基础 Functor。

参数

value (*): 容器内的值。

返回

(*): 返回 Functor

示例

Identity(1)
  .map((x) => x + 1)
  .fold((x) => x);
// => 2

Maybe

这个方法类似 Identity ,不同之处在于,Maybe 可以处理空值。

参数

value (*): 容器内的值。

返回

(*): 返回 Maybe

示例

Maybe(null)
  .map((x) => x + 1)
  .fold((x) => x);
// => null

Either

Either 内部有两个值:左值(Left)和右值(Right)。右值是正常情况下使用的值,左值是右值不存在时使用的默认值。

参数

left (*): 左值。

right (*): 右值。

返回

(*): 返回 Either

示例

Either(1, 2)
  .map((x) => x + 1)
  .fold((x) => x);
// => 3

Either(1, null)
  .map((x) => x + 1)
  .fold((x) => x);
// => 1

currino's People

Contributors

985563349 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.