Comparing version 0.5.0 to 0.6.0
/*! | ||
* uncouple v0.5.0 | ||
* uncouple v0.6.0 | ||
* (c) Vitor Luiz Cavalcanti <vitorluizc@outlook.com> (https://vitorluizc.github.io) | ||
@@ -8,21 +8,129 @@ * Released under the MIT License. | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
/** | ||
* Uncouple methods from constructor (class) into functions. | ||
* Get property keys from object, `T`. | ||
* @param object | ||
*/ | ||
var getKeys = Object.getOwnPropertyNames; | ||
/** | ||
* Curry to check if name is a method name from object, `T`. | ||
* @param object | ||
*/ | ||
var isMethodOf = function (object) { return function (key) { return key !== 'constructor' && typeof object[key] === 'function'; }; }; | ||
/** | ||
* Uncouple methods from function constructor, a class or an object into functions. | ||
* @example ```js | ||
* const { filter } = uncouple(Array); | ||
* const { filter } = uncoupleMethods(Array); | ||
* filter([ 1, 2, 3, 4 ], (value) => value % 2 === 0); | ||
* //=> [ 2, 4 ] | ||
* ``` | ||
* @param constructor - A constructor (class) to be uncoupled into functions. | ||
* @param constructor - A function constructor, a class or an object to be uncoupled into functions. | ||
*/ | ||
var uncouple = function (_a) { | ||
var prototype = _a.prototype; | ||
var names = Object.getOwnPropertyNames(prototype); | ||
return names.reduce(function (methods, name) { | ||
if (typeof prototype[name] === "function" && name !== "constructor") // @ts-ignore | ||
{ methods[name] = Function.call.bind(prototype[name]); } | ||
return methods; | ||
}, Object.create(null)); | ||
var uncoupleMethods = function (constructor) { | ||
var prototype = constructor.prototype || constructor; | ||
var methods = Object.create(null); | ||
getKeys(prototype).filter(isMethodOf(prototype)).forEach(function (name) { | ||
// @ts-ignore | ||
methods[name] = Function.call.bind(prototype[name]); | ||
}); | ||
return methods; | ||
}; | ||
/** | ||
* Uncouple methods from function constructor, a class or an object into functions. | ||
* @example ```js | ||
* const { filter: createFilter } = uncoupleMethodsAsCurries(Array); | ||
* const filter((value) => value % 2 === 0); | ||
* filter([ 1, 2, 3, 4 ]); | ||
* //=> [ 2, 4 ] | ||
* ``` | ||
* @param constructor - A function constructor, a class or an object. | ||
*/ | ||
module.exports = uncouple; | ||
var uncoupleMethodsAsCurries = function (constructor) { | ||
var prototype = constructor.prototype || constructor; | ||
var methods = Object.create(null); | ||
getKeys(prototype).filter(isMethodOf(prototype)).forEach(function (name) { | ||
// @ts-ignore | ||
methods[name] = function () { | ||
var arguments$1 = arguments; | ||
// @ts-ignore | ||
return function (instance) { return prototype[name].apply(instance, arguments$1); }; | ||
}; | ||
}); | ||
return methods; | ||
}; | ||
/** | ||
* Append prefix to a word and capitalize it. | ||
* @param prefix | ||
* @param name | ||
*/ | ||
var prefix = function (prefix, name) { return prefix + (name ? name[0].toUpperCase() + name.substr(1) : ''); }; | ||
/** | ||
* Uncouple getters from function constructor, a class or an object into functions. | ||
* @example ```js | ||
* const { getName } = uncoupleGetters({ | ||
* _name: 'Vitor', | ||
* get name () { | ||
* return this._name; | ||
* } | ||
* }); | ||
* getName({ _name: 'Lucas' }) | ||
* //=> 'Lucas' | ||
* ``` | ||
* @param constructor - A function constructor, a class or an object | ||
*/ | ||
var uncoupleGetters = function (constructor) { | ||
var prototype = constructor.prototype || constructor; | ||
var getters = Object.create(null); | ||
getKeys(prototype).forEach(function (name) { | ||
var descriptor = Object.getOwnPropertyDescriptor(prototype, name) || {}; | ||
if (typeof descriptor.get === 'function') { getters[prefix('get', name)] = Function.call.bind(descriptor.get); } | ||
}); | ||
return getters; | ||
}; | ||
/** | ||
* Uncouple setters from function constructor, a class or an object into functions. | ||
* @example ```js | ||
* const { setName } = uncoupleGetters({ | ||
* _name: 'Vitor', | ||
* set name (name) { | ||
* this._name = name; | ||
* } | ||
* }); | ||
* | ||
* const user = { | ||
* _name: 'Vitor' | ||
* }; | ||
* | ||
* setName(user, 'Lucas'); | ||
* | ||
* user._name; | ||
* //=> 'Lucas' | ||
* ``` | ||
* @param constructor - A function constructor, a class or an object | ||
*/ | ||
var uncoupleSetters = function (constructor) { | ||
var prototype = constructor.prototype || constructor; | ||
var setters = Object.create(null); | ||
getKeys(prototype).forEach(function (name) { | ||
var descriptor = Object.getOwnPropertyDescriptor(prototype, name) || {}; | ||
if (typeof descriptor.set === 'function') // @ts-ignore | ||
{ setters[prefix('set', name)] = Function.call.bind(descriptor.set); } | ||
}); | ||
return setters; | ||
}; | ||
exports.uncoupleGetters = uncoupleGetters; | ||
exports.uncoupleMethods = uncoupleMethods; | ||
exports.uncoupleMethodsAsCurries = uncoupleMethodsAsCurries; | ||
exports.uncoupleSetters = uncoupleSetters; |
/*! | ||
* uncouple v0.5.0 | ||
* uncouple v0.6.0 | ||
* (c) Vitor Luiz Cavalcanti <vitorluizc@outlook.com> (https://vitorluizc.github.io) | ||
@@ -7,28 +7,136 @@ * Released under the MIT License. | ||
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : | ||
typeof define === 'function' && define.amd ? define(factory) : | ||
(global = global || self, global.uncouple = factory()); | ||
}(this, function () { 'use strict'; | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : | ||
typeof define === 'function' && define.amd ? define(['exports'], factory) : | ||
(global = global || self, factory(global.uncouple = {})); | ||
}(this, (function (exports) { 'use strict'; | ||
/** | ||
* Uncouple methods from constructor (class) into functions. | ||
* Get property keys from object, `T`. | ||
* @param object | ||
*/ | ||
var getKeys = Object.getOwnPropertyNames; | ||
/** | ||
* Curry to check if name is a method name from object, `T`. | ||
* @param object | ||
*/ | ||
var isMethodOf = function (object) { return function (key) { return key !== 'constructor' && typeof object[key] === 'function'; }; }; | ||
/** | ||
* Uncouple methods from function constructor, a class or an object into functions. | ||
* @example ```js | ||
* const { filter } = uncouple(Array); | ||
* const { filter } = uncoupleMethods(Array); | ||
* filter([ 1, 2, 3, 4 ], (value) => value % 2 === 0); | ||
* //=> [ 2, 4 ] | ||
* ``` | ||
* @param constructor - A constructor (class) to be uncoupled into functions. | ||
* @param constructor - A function constructor, a class or an object to be uncoupled into functions. | ||
*/ | ||
var uncouple = function (_a) { | ||
var prototype = _a.prototype; | ||
var names = Object.getOwnPropertyNames(prototype); | ||
return names.reduce(function (methods, name) { | ||
if (typeof prototype[name] === "function" && name !== "constructor") // @ts-ignore | ||
{ methods[name] = Function.call.bind(prototype[name]); } | ||
return methods; | ||
}, Object.create(null)); | ||
var uncoupleMethods = function (constructor) { | ||
var prototype = constructor.prototype || constructor; | ||
var methods = Object.create(null); | ||
getKeys(prototype).filter(isMethodOf(prototype)).forEach(function (name) { | ||
// @ts-ignore | ||
methods[name] = Function.call.bind(prototype[name]); | ||
}); | ||
return methods; | ||
}; | ||
/** | ||
* Uncouple methods from function constructor, a class or an object into functions. | ||
* @example ```js | ||
* const { filter: createFilter } = uncoupleMethodsAsCurries(Array); | ||
* const filter((value) => value % 2 === 0); | ||
* filter([ 1, 2, 3, 4 ]); | ||
* //=> [ 2, 4 ] | ||
* ``` | ||
* @param constructor - A function constructor, a class or an object. | ||
*/ | ||
return uncouple; | ||
var uncoupleMethodsAsCurries = function (constructor) { | ||
var prototype = constructor.prototype || constructor; | ||
var methods = Object.create(null); | ||
getKeys(prototype).filter(isMethodOf(prototype)).forEach(function (name) { | ||
// @ts-ignore | ||
methods[name] = function () { | ||
var arguments$1 = arguments; | ||
})); | ||
// @ts-ignore | ||
return function (instance) { return prototype[name].apply(instance, arguments$1); }; | ||
}; | ||
}); | ||
return methods; | ||
}; | ||
/** | ||
* Append prefix to a word and capitalize it. | ||
* @param prefix | ||
* @param name | ||
*/ | ||
var prefix = function (prefix, name) { return prefix + (name ? name[0].toUpperCase() + name.substr(1) : ''); }; | ||
/** | ||
* Uncouple getters from function constructor, a class or an object into functions. | ||
* @example ```js | ||
* const { getName } = uncoupleGetters({ | ||
* _name: 'Vitor', | ||
* get name () { | ||
* return this._name; | ||
* } | ||
* }); | ||
* getName({ _name: 'Lucas' }) | ||
* //=> 'Lucas' | ||
* ``` | ||
* @param constructor - A function constructor, a class or an object | ||
*/ | ||
var uncoupleGetters = function (constructor) { | ||
var prototype = constructor.prototype || constructor; | ||
var getters = Object.create(null); | ||
getKeys(prototype).forEach(function (name) { | ||
var descriptor = Object.getOwnPropertyDescriptor(prototype, name) || {}; | ||
if (typeof descriptor.get === 'function') { getters[prefix('get', name)] = Function.call.bind(descriptor.get); } | ||
}); | ||
return getters; | ||
}; | ||
/** | ||
* Uncouple setters from function constructor, a class or an object into functions. | ||
* @example ```js | ||
* const { setName } = uncoupleGetters({ | ||
* _name: 'Vitor', | ||
* set name (name) { | ||
* this._name = name; | ||
* } | ||
* }); | ||
* | ||
* const user = { | ||
* _name: 'Vitor' | ||
* }; | ||
* | ||
* setName(user, 'Lucas'); | ||
* | ||
* user._name; | ||
* //=> 'Lucas' | ||
* ``` | ||
* @param constructor - A function constructor, a class or an object | ||
*/ | ||
var uncoupleSetters = function (constructor) { | ||
var prototype = constructor.prototype || constructor; | ||
var setters = Object.create(null); | ||
getKeys(prototype).forEach(function (name) { | ||
var descriptor = Object.getOwnPropertyDescriptor(prototype, name) || {}; | ||
if (typeof descriptor.set === 'function') // @ts-ignore | ||
{ setters[prefix('set', name)] = Function.call.bind(descriptor.set); } | ||
}); | ||
return setters; | ||
}; | ||
exports.uncoupleGetters = uncoupleGetters; | ||
exports.uncoupleMethods = uncoupleMethods; | ||
exports.uncoupleMethodsAsCurries = uncoupleMethodsAsCurries; | ||
exports.uncoupleSetters = uncoupleSetters; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
}))); |
/*! | ||
* uncouple v0.5.0 | ||
* uncouple v0.6.0 | ||
* (c) Vitor Luiz Cavalcanti <vitorluizc@outlook.com> (https://vitorluizc.github.io) | ||
* Released under the MIT License. | ||
*/ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e=e||self).uncouple=t()}(this,function(){"use strict";return function(e){var t=e.prototype;return Object.getOwnPropertyNames(t).reduce(function(e,n){return"function"==typeof t[n]&&"constructor"!==n&&(e[n]=Function.call.bind(t[n])),e},Object.create(null))}}); | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t=t||self).uncouple={})}(this,function(t){"use strict";var e=Object.getOwnPropertyNames,n=function(t){return function(e){return"constructor"!==e&&"function"==typeof t[e]}},r=function(t,e){return t+(e?e[0].toUpperCase()+e.substr(1):"")};t.uncoupleGetters=function(t){var n=t.prototype||t,o=Object.create(null);return e(n).forEach(function(t){var e=Object.getOwnPropertyDescriptor(n,t)||{};"function"==typeof e.get&&(o[r("get",t)]=Function.call.bind(e.get))}),o},t.uncoupleMethods=function(t){var r=t.prototype||t,o=Object.create(null);return e(r).filter(n(r)).forEach(function(t){o[t]=Function.call.bind(r[t])}),o},t.uncoupleMethodsAsCurries=function(t){var r=t.prototype||t,o=Object.create(null);return e(r).filter(n(r)).forEach(function(t){o[t]=function(){var e=arguments;return function(n){return r[t].apply(n,e)}}}),o},t.uncoupleSetters=function(t){var n=t.prototype||t,o=Object.create(null);return e(n).forEach(function(t){var e=Object.getOwnPropertyDescriptor(n,t)||{};"function"==typeof e.set&&(o[r("set",t)]=Function.call.bind(e.set))}),o},Object.defineProperty(t,"__esModule",{value:!0})}); | ||
//# sourceMappingURL=index.umd.min.js.map |
{ | ||
"name": "uncouple", | ||
"version": "0.5.0", | ||
"version": "0.6.0", | ||
"description": "Uncouple constructors and classes methods into functions.", | ||
"sideEffects": false, | ||
"cdn": "dist/index.umd.min.js", | ||
@@ -9,3 +10,3 @@ "main": "dist/index.js", | ||
"unpkg": "dist/index.umd.min.js", | ||
"module": "dist/index.mjs", | ||
"module": "dist/index.esm.js", | ||
"jsdelivr": "dist/index.umd.min.js", | ||
@@ -18,4 +19,4 @@ "umd:main": "dist/index.umd.js", | ||
"scripts": { | ||
"pretest": "npm run build", | ||
"test": "ava --no-cache", | ||
"lint": "prettier **/*.{js,json,md,ts} --check", | ||
"build": "bili", | ||
@@ -46,8 +47,10 @@ "prepare": "npm test" | ||
"devDependencies": { | ||
"ava": "^1.3.1", | ||
"bili": "^4.7.1", | ||
"rollup-plugin-typescript2": "^0.20.1", | ||
"ts-node": "^8.0.3", | ||
"typescript": "^3.3.4000" | ||
"@ava/babel": "^1.0.1", | ||
"ava": "^3.5.0", | ||
"bili": "^4.8.1", | ||
"prettier": "^1.19.1", | ||
"rollup-plugin-typescript2": "^0.26.0", | ||
"ts-node": "^8.6.2", | ||
"typescript": "^3.8.3" | ||
} | ||
} |
@@ -5,2 +5,3 @@ # Uncouple | ||
[![License](https://badgen.net/github/license/VitorLuizC/uncouple)](./LICENSE) | ||
[![Install size](https://packagephobia.now.sh/badge?p=uncouple)](https://packagephobia.now.sh/result?p=uncouple) | ||
[![Library minified size](https://badgen.net/bundlephobia/min/uncouple)](https://bundlephobia.com/result?p=uncouple) | ||
@@ -35,6 +36,6 @@ [![Library minified + gzipped size](https://badgen.net/bundlephobia/minzip/uncouple)](https://bundlephobia.com/result?p=uncouple) | ||
// UMD module is exposed through the "uncouple" global function. | ||
console.log(uncouple) | ||
console.log(uncouple); | ||
var O = uncouple(Object) | ||
var isFetchDefined = O.hasOwnProperty(window, 'fetch') | ||
var O = uncouple(Object); | ||
var isFetchDefined = O.hasOwnProperty(window, 'fetch'); | ||
</script> | ||
@@ -85,3 +86,3 @@ ``` | ||
User.prototype.getName = function () { | ||
User.prototype.getName = function() { | ||
console.log(this.name); | ||
@@ -124,3 +125,3 @@ }; | ||
const isLink = (anchor) => /^https?:\/\//.test(anchor.href); | ||
const isLink = anchor => /^https?:\/\//.test(anchor.href); | ||
@@ -127,0 +128,0 @@ const links = filter(anchors, isLink); |
@@ -0,5 +1,6 @@ | ||
import { KeyOf } from './key'; | ||
/** | ||
* Property name from `T`. | ||
* A generic function type. | ||
*/ | ||
declare type PropertyNameOf<T> = Extract<keyof T, string>; | ||
declare type Method = (...args: any[]) => any; | ||
/** | ||
@@ -9,20 +10,88 @@ * Uncoupled methods from `T`. | ||
declare type UncoupledMethodsOf<T> = { | ||
[K in PropertyNameOf<T>]: T[K] extends (...args: any[]) => any ? (instance: T, ...args: Parameters<T[K]>) => ReturnType<T[K]> : never; | ||
[K in KeyOf<T>]: T[K] extends Method ? (instance: T, ...args: Parameters<T[K]>) => ReturnType<T[K]> : never; | ||
}; | ||
/** | ||
* Constructor (class) with generic prototype `T`. | ||
* A function constructor (class) with prototype of `T` or an object of `T`. | ||
*/ | ||
declare type Constructor<T> = (new (...args: any[]) => any) & { | ||
declare type Constructor<T> = { | ||
prototype: T; | ||
}; | ||
} | (T & { | ||
prototype: undefined; | ||
}); | ||
/** | ||
* Uncouple methods from constructor (class) into functions. | ||
* Uncouple methods from function constructor, a class or an object into functions. | ||
* @example ```js | ||
* const { filter } = uncouple(Array); | ||
* const { filter } = uncoupleMethods(Array); | ||
* filter([ 1, 2, 3, 4 ], (value) => value % 2 === 0); | ||
* //=> [ 2, 4 ] | ||
* ``` | ||
* @param constructor - A constructor (class) to be uncoupled into functions. | ||
* @param constructor - A function constructor, a class or an object to be uncoupled into functions. | ||
*/ | ||
declare const uncouple: <T>({ prototype }: Constructor<T>) => UncoupledMethodsOf<T>; | ||
export default uncouple; | ||
export declare const uncoupleMethods: <T>(constructor: Constructor<T>) => UncoupledMethodsOf<T>; | ||
/** | ||
* Uncoupled methods from `T`. | ||
*/ | ||
declare type UncoupledMethodsAsCurriesOf<T> = { | ||
[K in KeyOf<T>]: T[K] extends Method ? (...args: Parameters<T[K]>) => (instance: T) => ReturnType<T[K]> : never; | ||
}; | ||
/** | ||
* Uncouple methods from function constructor, a class or an object into functions. | ||
* @example ```js | ||
* const { filter: createFilter } = uncoupleMethodsAsCurries(Array); | ||
* const filter((value) => value % 2 === 0); | ||
* filter([ 1, 2, 3, 4 ]); | ||
* //=> [ 2, 4 ] | ||
* ``` | ||
* @param constructor - A function constructor, a class or an object. | ||
*/ | ||
export declare const uncoupleMethodsAsCurries: <T>(constructor: Constructor<T>) => UncoupledMethodsAsCurriesOf<T>; | ||
/** | ||
* Uncoupled getters from `T`. | ||
*/ | ||
declare type UncoupledGettersOf<T> = { | ||
[name: string]: (instance: T) => any; | ||
}; | ||
/** | ||
* Uncouple getters from function constructor, a class or an object into functions. | ||
* @example ```js | ||
* const { getName } = uncoupleGetters({ | ||
* _name: 'Vitor', | ||
* get name () { | ||
* return this._name; | ||
* } | ||
* }); | ||
* getName({ _name: 'Lucas' }) | ||
* //=> 'Lucas' | ||
* ``` | ||
* @param constructor - A function constructor, a class or an object | ||
*/ | ||
export declare const uncoupleGetters: <T>(constructor: Constructor<T>) => UncoupledGettersOf<T>; | ||
/** | ||
* Uncoupled setters from `T`. | ||
*/ | ||
declare type UncoupledSettersOf<T> = { | ||
[name: string]: (instance: T, value: any) => void; | ||
}; | ||
/** | ||
* Uncouple setters from function constructor, a class or an object into functions. | ||
* @example ```js | ||
* const { setName } = uncoupleGetters({ | ||
* _name: 'Vitor', | ||
* set name (name) { | ||
* this._name = name; | ||
* } | ||
* }); | ||
* | ||
* const user = { | ||
* _name: 'Vitor' | ||
* }; | ||
* | ||
* setName(user, 'Lucas'); | ||
* | ||
* user._name; | ||
* //=> 'Lucas' | ||
* ``` | ||
* @param constructor - A function constructor, a class or an object | ||
*/ | ||
export declare const uncoupleSetters: <T>(constructor: Constructor<T>) => UncoupledSettersOf<T>; | ||
export {}; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
23579
10
482
177
7