Code Monkey home page Code Monkey logo

iterable-chain's Introduction

iterable-chain

LINQ-style chaining methods for iterables with strict type-checking.

  • Better Types

Methods have various overloads for better type inference.For example, filter has 3 overloads:

interface Chain<T> {
  // normal
  filter(cb: Predicate<T>) : Chain<T>;
  // manual limit result to sub type
  filter<U extends T>(cb: Predicate<T>): Chain<U>;
  // explicit type protection to sub type
  filter<U extends T>(cb: TypeProtection<T, U>): Chain<U>;
}
  • Fluent Interface

Use "chain"s to make your code more elegant.

function uncapitalize<S extends string>(s: S): Uncapitalize<S> {
  // ...
}
const obj = { "gEt": 1, "PoSt": 2, "puT": 3 };

const uncapitalized = Object.fromEntries(
  Object.entries(obj)
    .map(p => [uncapitalize(p[0]), p[1]])
);

const elegant = obj
  .toPairs()
  .map(([key, value]) => [uncapitalize(key), value] as const)
  // key is "gEt" | "poSt" | "puT" now
  .toDictionary(p => p[0], p => p[1]);

Install

npm install @lovekicher/iterable-chain or

yarn add @lovekicher/iterable-chain or

pnpm install @lovekicher/iterable-chain

Quick Start

Add Helper Methods(Optional)

import "@lovekicher/iterable-chain/global";

This will add helper methods(and type definitions) to the prototype or constructor of some primitive types, such as Array.prototype.chain, Array.range, Object.prototype.toPairs, etc.

Do this action only ONCE at the top of your code entry point

Use Chain

  1. Create chain
import { chain } from "@lovekicher/iterable-chain";

const numbers = [1, 2, 3, 4, 5];

// From helper methods
const chain1 = numbers.chain();

// Or directly
const chain2 = chain(numbers);
  1. Add chain operations and collectors
const chain = [
    [2018, 99],
    [2019, 327],
    [2020, 404],
    [2021, 653],
    [2022, 202],
  ]
  .chain()
  .orderBy(x => x[1], true)
  .map(x => x[1])
  .filter(x => x % 2 == 0)
  .map(x => "0x" + x.toString(16));
// All callback functions have not been called

const result = chain.toArray();
// `toArray` is a collect method, now get ["0x194", "0xca"]
const dict = [
    { owner: "foo", count: 3 },
    { owner: "bar", count: 10 },
    { owner: "baz", count: 16 },
  ]
  .chain()
  .toDictionary(x => x.owner, x => x.count);
/* {
  "foo": 3,
  "bar": 10,
  "baz": 16
} */

iterable-chain's People

Contributors

swingcosmic 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.