Uncouple
Uncouple constructors and classes methods into functions.
Installation
This library is published in the NPM registry and can be installed using any compatible package manager.
npm install uncouple --save # For Yarn, use the command below. yarn add uncouple
Installation from CDN
This module has an UMD bundle available through JSDelivr and Unpkg CDNs.
<!-- For UNPKG use the code below. --> <!-- For JSDelivr use the code below. -->
Usage
Module default exports uncouple function.
uncouple
receives a constructor or a class as argument and returns an object with its uncoupled methods.
; const O = ;// => {// hasOwnProperty: ƒ ()// isPrototypeOf: ƒ ()// propertyIsEnumerable: ƒ ()// toLocaleString: ƒ ()// toString: ƒ ()// valueOf: ƒ ()// } const hasFetch = O;// => true
All uncoupled methods receives an instance as first argument followed by method arguments.
const trim substr = ; ;//=> 'Okay' ;//=> 'CDF'
It also works for Function constructors and classes.
{ thisname = name;} Userprototype { console;}; const getName = ; ;//=> 'João' speed = 0; { thisspeed += speed; } const acelerate = ; const uno = ; ;; unospeed;//=> 180
Use cases
You can reuse methods with duck types, like Array.prototype.filter in a NodeList.
const filter = ; const anchors = document;//=> NodeListOf<HTMLAnchorElement> const isLink = /^https?:\/\//; const links = ;//=> Array<HTMLAnchorElement>
Compositions and smart pipelines became pretty and readable with uncoupled methods.
const trim replace normalize toLocaleLowerCase } = ; " Olá, como vai vocÊ?"|> |> |> trim|> |> toLocaleLowerCase//=> 'ola, como vai voce?' const normalize = ; //=> 'meu nome e vitor, meus bons'
With uncouple
you can call Object
methods with Object.create(null)
, which returns an empty object without prototype.
const user = Object; username = '@VitorLuizC'; user;//=> throws TypeError: user.hasOwnProperty is not a function const hasOwnProperty: has = ; ;//=> true
License
Released under MIT License.