Skip to main content

A collection of built-in object method and property as currying function

test GitHub release deno land nest badge deno doc deno version node support version

GitHub (Pre-)Release Date dependencies Status codecov Codacy Badge npm type definitions Commitizen friendly Gitmoji semantic-release License: MIT FOSSA Status


The methods and properties of the build-in object are made independent as functions. All functions are currying and have no side effects such as exceptions.

✨ Features

  • ⚑ Multi runtime support (Deno, Node.js and Browsers)
  • πŸ“š Pure TypeScript and provides type definition
  • :earth_americas: Universal module, providing ES modules and Commonjs
  • :package: Optimized, super slim size
  • πŸ“„ TSDoc-style comments

Package name

core-fn (deno.land, nest.land, npm)

:question: Why

This project provides all build-in object methods and properties as no side-effect currying functions.

The differences with core-js-pure are as follows:

  • The goal of this project is not to become polifill. It is to provide small basic functions for functional programming.

  • All functions are currying.

  • All functions have no side effects, do not raise exceptions, and return values.

This allows you to use the basic functions for functional programming without having to define them.

For example, if you are going to use String#replace to convert a string, you can often make it a function to increase independence and testability.

const fallback = (val: string) => val.replace("https", "http");

A more generalized version of this function would look like this:

const replace = (from: string, to: string) =>
  (val: string) => val.replace(from, to);
const fallback = replace("https", "http");

This makes a generic replace function that can be used for other strings.

const shorten = replace("typescript", "ts");

However, the replace function is not easy to use. The replace function always requires two arguments.

If you want to convert the string typescript to javascript and ts, you will have to make a duplicate declaration.

const shorten = replace(''typescript'', 'ts'')
const toJavaScript = replace('typescript', 'javascript')

This can be solved by currying.

// currying replace function
const fromTypescript = curryingReplace("typescript");
const shorten = fromTypescript("ts");
const toJavaScript = fromTypescript("javascript");

shorten("typescript is great"); // 'ts is great'
toJavaScript("typescript"); // 'javascript'

It’s hard to make currying and the Type system coexist, but this project solves that problem. All currently supported functions are here.

Also, all pure functions can be synthesized to create a concise pipeline.

The following example composes the String object methods of trim, replace and toUpperCase to create a new function.

const title = pipe(trim, replace("typescript", "ts"), toUpperCase);

title(" typescript is great  "); // 'TS IS GREAT'

Since this project targets all build-in objects, it is recommended to use pipe for composite functions.

πŸ’« Usage

core-fn provides multi platform modules.

πŸ¦• Deno

deno.land

import { replace } from "https://deno.land/x/core-fn/mod.ts";

replace("hello")("hi", "hello Tom"); // 'hi Tom'

nest.land

import { replace } from "https://x.nest.land/core-fn/mod.ts";

replace("hello")("hi", "hello Tom"); // 'hi Tom'

:package: Node.js

Install

npm i core-fn
or
yarn add core-fn

ES modules

import { replace } from "core-fn";

replace("hello")("hi", "hello Tom"); // 'hi Tom'

Commonjs

const { replace } = require("core-fn");

replace("hello")("hi", "hello Tom"); // 'hi Tom'

:globe_with_meridians: Browser

The module that bundles the dependencies is obtained from skypack.

<script type="module">
  import { replace } from "https://cdn.skypack.dev/core-fn";
  console.log(replace('hello')('hi', 'hello Tom'); // 'hi Tom'
</script>

πŸ“ API

String

charAt, endsWith, match, repeat, replace, startsWith, toLowerCase, toUpperCase, trimEnd, trimLeft, trimRight, trimStart, trim

Symbol

description

RegExp

dotAll, exec, flags, global, ignoreCase, lastIndex, multiline, source, sticky, test, unicode

Common(Methods or properties of multiple types)

includes, length, map, slice

πŸ’š Supports

ie is no longer supported to reduce bundle size.

The TypeScript version must be 4.1.0 or higher.

This project provides ES modules and Commonjs.

If you have an opinion about what to support, you can open an issue to discuss it.

The browserslist has the following settings.

defaults
last 8 version
not IE <= 11
not ie_mob <= 11
node 6
Deno
Deno
Node.js
Node.js
IE / Edge
Edge
Firefox
Firefox
Chrome
Chrome
Safari
Safari
iOS Safari
iOS Safari
Samsung
Samsung
Opera
Opera
^1.6.0 ^6.17.0 ^83 ^78 ^83 ^11 ^12.0 ^7.2 ^68

:handshake: Contributing

Contributions, issues and feature requests are welcome!
Feel free to check issues.

Contributing guide

🌱 Show your support

Give a ⭐️ if this project helped you!

πŸ’‘ License

Copyright Β© 2021-present TomokiMiyauci.

Released under the MIT license

FOSSA Status