Giter Site home page Giter Site logo

asd-xiv / m Goto Github PK

View Code? Open in Web Editor NEW
4.0 4.0 1.0 7.38 MB

Point free style, functional library for Javascript with focus on object arrays.

Home Page: https://asd-xiv.github.io/m/

License: MIT License

JavaScript 99.60% Shell 0.40%
point-free functional javascript library

m's Introduction

CircleCI npm version Coverage Status

m

Point free style, functional library for Javascript with focus on object arrays.


Install

npm install @asd14/m

Use

import { pipe, trim, split, dropLast, push, join } from "@asd14/m"

const removeTrailingSlash = source =>
  source[source.length - 1] === sep ? source.slice(0, -1) : source

const renameFile = newName =>
  pipe(
    removeTrailingSlash,
    split(sep),
    dropLast,
    push(trim(sep, newName)),
    join(sep)
  )

"With" pattern

Some functions have a *With variant. find has findWith, filter has filterWith etc. They allow for less boilerplate and more intuitive way of handling object arrays.

import { find, findWith, filterWith, not, is } from "@asd14/m"

const todos = [
  { id: 1, name: "lorem", tagId: 2 },
  { id: 2, name: "ipsum", tagId: null },
  { id: 3, name: "dolor", tagId: null },
]
/* Predicate fn */
find(item => item.id === 1, todos)
// => {id: 1, name: "lorem", tagId: 2}

/* Matching object */
findWith(
  {
    id: 1,
  },
  todos
)
// => {id: 1, name: "lorem", tagId: 2}

/* Matching object & predicate fn */
filterWith(
  {
    tagId: is, // same as `tagId: source => is(source)`
  },
  todos
)
// => [{id: 1, name: "lorem", tagId: 2}]

/* Syntactic sugar */
filterWith(
  {
    "!tagId": is, // same as `tagId: not(is)`
  },
  todos
)
// => [
//  {id: 2, name: "ipsum", tagId: null},
//  {id: 3, name: "dolor", tagId: null}
// ]

|> pipe

There is no structure difference between pipe and compose, both will use the same building blocks to get from A to B.

A series of transformations over an initial input can be written as x -> f -> g -> result, piping, or as result = g(f(x)), composing. The difference is only syntactic. Input is the same, transformations and order of application are the same, the result will be the same.

Given that:

it makes sense to choose the syntax more aligned with our intuition and context. The transformations are applied in a certain order with time as a medium - input -> t0 -> t1 -> tn -> output.

const { sep } = require("path")
const { pipe, compose, join, push, dropLast, split } = require("@asd14/m")

// Compose: g(f(x))
const renameFile = newName => filePath =>
  compose(join(sep), push(newName), dropLast, split(sep))(filePath)

// Pipe: x -> f -> g
const renameFile = newName => filePath =>
  pipe(split(sep), dropLast, push(newName), join(sep))(filePath)

// More expressive with pipeline operator
const renameFile = newName => filePath =>
  filePath |> split(sep) |> dropLast |> push(newName) |> join(sep)

Develop

git clone [email protected]:asd14-xyz/m.git && \
  cd m && \
  npm run setup

# run tests (any `*.test.js`) once
npm test

# watch `src` folder for changes and run test automatically
npm run tdd

Contributors

Thank you for contributing your time and knowledge:

Changelog

See the releases section for details.

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.