A collection of built-in object method and property as currying function
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
andCommonjs
- :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
π 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 |
Node.js |
Edge |
Firefox |
Chrome |
Safari |
iOS Safari |
Samsung |
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.
π± Show your support
Give a βοΈ if this project helped you!
π‘ License
Copyright Β© 2021-present TomokiMiyauci.
Released under the MIT license