made with esnextbin
Created
July 29, 2016 00:30
-
-
Save trxcllnt/41ab41904ef14c45fe6fb078e360f9f2 to your computer and use it in GitHub Desktop.
esnextbin sketch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>ESNextbin Sketch</title> | |
<!-- put additional styles and scripts here --> | |
</head> | |
<body> | |
<!-- put markup and other contents here --> | |
<div id="app"></div> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyClass { | |
constructor({ required, fields, ...extraProps }) { | |
console.log(this.constructor.name); | |
console.log(JSON.stringify({ required, fields })); | |
console.log(JSON.stringify(extraProps)); | |
} | |
} | |
const MyCurriedClass = curryClass('required', 'fields', MyClass); | |
const MyCurriedInlineClass = curryClass( | |
'required', 'fields', | |
class MyInlineClass extends MyClass { | |
} | |
); | |
const MyCurriedInlineSubClass = curryClass( | |
'required', 'fields', | |
class MyCurriedInlineSubClass extends MyCurriedInlineClass { | |
} | |
); | |
let curriedWithNew, curriedSansNew, curriedInline, curriedInlineSubclass; | |
const optionalProps1 = { added: 'extra'}; | |
const optionalProps2 = { optional: 'props'}; | |
const requiredProps1 = { required: 'to dance in' }; | |
const requiredProps2 = { fields: 'of lilacs' }; | |
// /* | |
curriedWithNew = new MyCurriedClass(optionalProps1); | |
curriedWithNew = new curriedWithNew(optionalProps2); | |
curriedWithNew = new curriedWithNew(requiredProps1); | |
curriedWithNew = new curriedWithNew(requiredProps2); | |
console.log(curriedWithNew instanceof MyClass); | |
console.log(curriedWithNew instanceof MyCurriedClass); | |
// */ | |
// /* | |
curriedSansNew = MyCurriedClass(optionalProps1); | |
curriedSansNew = curriedSansNew(optionalProps2); | |
curriedSansNew = curriedSansNew(requiredProps1); | |
curriedSansNew = curriedSansNew(requiredProps2); | |
console.log(curriedSansNew instanceof MyClass); | |
console.log(curriedSansNew instanceof MyCurriedClass); | |
// */ | |
// /* | |
curriedInline = MyCurriedInlineClass(optionalProps1); | |
curriedInline = curriedInline(optionalProps2); | |
curriedInline = curriedInline(requiredProps1); | |
curriedInline = curriedInline(requiredProps2); | |
console.log(curriedInline instanceof MyClass); | |
console.log(curriedInline instanceof MyCurriedInlineClass); | |
// */ | |
// /* | |
curriedInlineSubclass = new MyCurriedInlineSubClass(optionalProps1); | |
curriedInlineSubclass = new curriedInlineSubclass(optionalProps2); | |
curriedInlineSubclass = new curriedInlineSubclass(requiredProps1); | |
curriedInlineSubclass = new curriedInlineSubclass(requiredProps2); | |
console.log(curriedInlineSubclass instanceof MyClass); | |
console.log(curriedInlineSubclass instanceof MyCurriedInlineClass); | |
console.log(curriedInlineSubclass instanceof MyCurriedInlineSubClass); | |
// */ | |
function curryClass(/*... requiredArgs, classToCurry */ ...args) { | |
const classToCurry = args.pop(); | |
const requiredArgs = flatten(args); | |
// If no required args, no need to curry. | |
if (requiredArgs.length === 0) { | |
return classToCurry; | |
} | |
CurriedCtor.prototype = Object.create(classToCurry.prototype); | |
return CurriedCtor; | |
function CurriedCtor(properties = {}) { | |
const requiredArgsLeft = requiredArgs.filter((prop) => ( | |
!properties.hasOwnProperty(prop) | |
)); | |
if (requiredArgsLeft.length) { | |
return function curryToCtor(more) { | |
return CurriedCtor.call(this, {...properties, ...more}); | |
}; | |
} else if (!(this instanceof CurriedCtor)) { | |
return new CurriedCtor(properties); | |
} | |
return classToCurry.call(this, properties); | |
} | |
function flatten(xs) { | |
return Array.isArray(xs) ? xs.map(flatten) : xs; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "esnextbin-sketch", | |
"version": "0.0.0", | |
"dependencies": { | |
"babel-runtime": "6.11.6" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var _extends2 = require('babel-runtime/helpers/extends'); | |
var _extends3 = _interopRequireDefault(_extends2); | |
var _create = require('babel-runtime/core-js/object/create'); | |
var _create2 = _interopRequireDefault(_create); | |
var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of'); | |
var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf); | |
var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); | |
var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); | |
var _inherits2 = require('babel-runtime/helpers/inherits'); | |
var _inherits3 = _interopRequireDefault(_inherits2); | |
var _stringify = require('babel-runtime/core-js/json/stringify'); | |
var _stringify2 = _interopRequireDefault(_stringify); | |
var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties'); | |
var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2); | |
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); | |
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); | |
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | |
var MyClass = function MyClass(_ref) { | |
var required = _ref.required; | |
var fields = _ref.fields; | |
var extraProps = (0, _objectWithoutProperties3.default)(_ref, ['required', 'fields']); | |
(0, _classCallCheck3.default)(this, MyClass); | |
console.log(this.constructor.name); | |
console.log((0, _stringify2.default)({ required: required, fields: fields })); | |
console.log((0, _stringify2.default)(extraProps)); | |
}; | |
var MyCurriedClass = curryClass('required', 'fields', MyClass); | |
var MyCurriedInlineClass = curryClass('required', 'fields', function (_MyClass) { | |
(0, _inherits3.default)(MyInlineClass, _MyClass); | |
function MyInlineClass() { | |
(0, _classCallCheck3.default)(this, MyInlineClass); | |
return (0, _possibleConstructorReturn3.default)(this, (0, _getPrototypeOf2.default)(MyInlineClass).apply(this, arguments)); | |
} | |
return MyInlineClass; | |
}(MyClass)); | |
var MyCurriedInlineSubClass = curryClass('required', 'fields', function (_MyCurriedInlineClass) { | |
(0, _inherits3.default)(MyCurriedInlineSubClass, _MyCurriedInlineClass); | |
function MyCurriedInlineSubClass() { | |
(0, _classCallCheck3.default)(this, MyCurriedInlineSubClass); | |
return (0, _possibleConstructorReturn3.default)(this, (0, _getPrototypeOf2.default)(MyCurriedInlineSubClass).apply(this, arguments)); | |
} | |
return MyCurriedInlineSubClass; | |
}(MyCurriedInlineClass)); | |
var curriedWithNew = void 0, | |
curriedSansNew = void 0, | |
curriedInline = void 0, | |
curriedInlineSubclass = void 0; | |
var optionalProps1 = { added: 'extra' }; | |
var optionalProps2 = { optional: 'props' }; | |
var requiredProps1 = { required: 'to dance in' }; | |
var requiredProps2 = { fields: 'of lilacs' }; | |
// /* | |
curriedWithNew = new MyCurriedClass(optionalProps1); | |
curriedWithNew = new curriedWithNew(optionalProps2); | |
curriedWithNew = new curriedWithNew(requiredProps1); | |
curriedWithNew = new curriedWithNew(requiredProps2); | |
console.log(curriedWithNew instanceof MyClass); | |
console.log(curriedWithNew instanceof MyCurriedClass); | |
// */ | |
// /* | |
curriedSansNew = MyCurriedClass(optionalProps1); | |
curriedSansNew = curriedSansNew(optionalProps2); | |
curriedSansNew = curriedSansNew(requiredProps1); | |
curriedSansNew = curriedSansNew(requiredProps2); | |
console.log(curriedSansNew instanceof MyClass); | |
console.log(curriedSansNew instanceof MyCurriedClass); | |
// */ | |
// /* | |
curriedInline = MyCurriedInlineClass(optionalProps1); | |
curriedInline = curriedInline(optionalProps2); | |
curriedInline = curriedInline(requiredProps1); | |
curriedInline = curriedInline(requiredProps2); | |
console.log(curriedInline instanceof MyClass); | |
console.log(curriedInline instanceof MyCurriedInlineClass); | |
// */ | |
// /* | |
curriedInlineSubclass = new MyCurriedInlineSubClass(optionalProps1); | |
curriedInlineSubclass = new curriedInlineSubclass(optionalProps2); | |
curriedInlineSubclass = new curriedInlineSubclass(requiredProps1); | |
curriedInlineSubclass = new curriedInlineSubclass(requiredProps2); | |
console.log(curriedInlineSubclass instanceof MyClass); | |
console.log(curriedInlineSubclass instanceof MyCurriedInlineClass); | |
console.log(curriedInlineSubclass instanceof MyCurriedInlineSubClass); | |
// */ | |
function curryClass() { | |
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | |
args[_key] = arguments[_key]; | |
} | |
var classToCurry = args.pop(); | |
var requiredArgs = flatten(args); | |
// If no required args, no need to curry. | |
if (requiredArgs.length === 0) { | |
return classToCurry; | |
} | |
CurriedCtor.prototype = (0, _create2.default)(classToCurry.prototype); | |
return CurriedCtor; | |
function CurriedCtor() { | |
var properties = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; | |
var requiredArgsLeft = requiredArgs.filter(function (prop) { | |
return !properties.hasOwnProperty(prop); | |
}); | |
if (requiredArgsLeft.length) { | |
return function curryToCtor(more) { | |
return CurriedCtor.call(this, (0, _extends3.default)({}, properties, more)); | |
}; | |
} else if (!(this instanceof CurriedCtor)) { | |
return new CurriedCtor(properties); | |
} | |
return classToCurry.call(this, properties); | |
} | |
function flatten(xs) { | |
return Array.isArray(xs) ? xs.map(flatten) : xs; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment