@hydre/commons
Advanced tools
Comparing version 1.0.12 to 1.0.13
@@ -7,2 +7,3 @@ "use strict"; | ||
exports.cache = cache; | ||
exports.mixin = mixin; | ||
@@ -23,2 +24,30 @@ function cache(...a) { | ||
}; | ||
} | ||
function mixin(behaviour, sharedBehaviour = {}) { | ||
const instanceKeys = Reflect.ownKeys(behaviour); | ||
const sharedKeys = Reflect.ownKeys(sharedBehaviour); | ||
const typeTag = Symbol('isa'); | ||
function _mixin(clazz) { | ||
for (let property of instanceKeys) Object.defineProperty(clazz.prototype, property, { | ||
value: behaviour[property], | ||
writable: true | ||
}); | ||
Object.defineProperty(clazz.prototype, typeTag, { | ||
value: true | ||
}); | ||
return clazz; | ||
} | ||
for (let property of sharedKeys) Object.defineProperty(_mixin, property, { | ||
value: sharedBehaviour[property], | ||
enumerable: sharedBehaviour.propertyIsEnumerable(property) | ||
}); | ||
Object.defineProperty(_mixin, Symbol.hasInstance, { | ||
value: i => !!i[typeTag] | ||
}); | ||
return _mixin; | ||
} |
{ | ||
"name": "@hydre/commons", | ||
"version": "1.0.12", | ||
"version": "1.0.13", | ||
"description": "You need some milk ?", | ||
@@ -35,3 +35,3 @@ "author": "Hydre", | ||
"@babel/core": "^7.4.5", | ||
"@babel/node": "^7.4.5", | ||
"@babel/node": "^7.2.2", | ||
"@babel/plugin-proposal-class-properties": "^7.4.4", | ||
@@ -41,5 +41,12 @@ "@babel/plugin-proposal-decorators": "^7.4.4", | ||
"@babel/plugin-proposal-function-bind": "^7.2.0", | ||
"@babel/plugin-proposal-logical-assignment-operators": "^7.2.0", | ||
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.4.4", | ||
"@babel/plugin-proposal-numeric-separator": "^7.2.0", | ||
"@babel/plugin-proposal-optional-catch-binding": "^7.2.0", | ||
"@babel/plugin-proposal-optional-chaining": "^7.2.0", | ||
"@babel/plugin-proposal-pipeline-operator": "^7.3.2", | ||
"@babel/plugin-proposal-private-methods": "^7.4.4", | ||
"@babel/preset-env": "^7.4.5", | ||
"@babel/plugin-proposal-throw-expressions": "^7.2.0", | ||
"@babel/plugin-syntax-dynamic-import": "^7.2.0", | ||
"@babel/preset-env": "^7.4.4", | ||
"@hydre/doubt": "^1.0.12", | ||
@@ -46,0 +53,0 @@ "tap-nyan": "^1.1.0", |
@@ -20,9 +20,11 @@ ![][licence] [![][npm]][npmlink] [![][travis]][travislink] [![][npmdl]][npmlink] | ||
### Install | ||
## Install | ||
`npm i @hydre/commons` | ||
``` | ||
npm i @hydre/commons | ||
``` | ||
### Use: | ||
## Use: | ||
#### Deferred Promise | ||
### Deferred Promise | ||
Execute a promise outside scope | ||
@@ -41,5 +43,5 @@ | ||
#### Decorators | ||
### Decorators | ||
> @cache | ||
#### @cache | ||
@@ -65,1 +67,33 @@ Call the function/getter only once and keep the result in memory | ||
``` | ||
### Helpers | ||
#### mixin | ||
Multiple class inheritance *see [exploring-es7-decorators](https://medium.com/google-developers/exploring-es7-decorators-76ecb65fb841)* | ||
```js | ||
import { mixin } from '@hydre/commons' | ||
const TimeStone = mixin({ | ||
get canRewind() { | ||
return true | ||
} | ||
}) | ||
const PlaysFortnite = mixin({ | ||
isDumb() { | ||
return true | ||
} | ||
}) | ||
@TimeStone | ||
@PlaysFortnite | ||
class Thanos { | ||
isDumb() { | ||
return false | ||
} | ||
} | ||
new Thanos().isDumb() |> console.log // print true | ||
``` |
@@ -1,42 +0,1 @@ | ||
import '@hydre/doubt' | ||
import { dPromise, cache } from '../src' | ||
class Foo { | ||
i = 0 | ||
z = 0 | ||
@cache | ||
bar() { | ||
return ++this.i | ||
} | ||
@cache | ||
get baz() { | ||
return ++this.z | ||
} | ||
} | ||
'A deferred promise'.doubt(async () => { | ||
const dp = new dPromise() | ||
await (200).ms() | ||
dp.resolve() | ||
'can be resolved from outside scope'.because(dp.promise).succeeds() | ||
const dp2 = new dPromise() | ||
await (200).ms() | ||
dp2.reject() | ||
'can be rejected from outside scope'.because(dp2.promise).fails() | ||
}) | ||
'A cached function'.doubt(() => { | ||
const foo = new Foo() | ||
foo.bar() | ||
foo.bar() | ||
'should be called only once'.because(foo.bar()).isEqualTo(1) | ||
}) | ||
'A cached getter'.doubt(() => { | ||
const foo = new Foo() | ||
foo.baz | ||
foo.baz | ||
'should be called only once'.because(foo.baz).isEqualTo(1) | ||
}) | ||
import * as _ from './' |
7964
10
116
96
20