Functional and "typed" defineProp
, defineEnumProp
, defineProps
, defineEnumProps
functionality.
This library now lives on as part of the fjl library.
- Requirements
- Getting Started
- Docs
- Motivation
- Development
- Supported Platforms
- License
- Resources
- Change log
- Javascript Ecmascript 5+.
- IE9+, and all other modern day browsers.
- 8+
import {...} from 'fjl-mutable';
const {...} = require('fjl-mutable');
See desired export type below:
- './dist/amd/' - Asynchronous module format.
- './dist/cjs/' - CommonJs module format.
- './dist/umd/' - Universal module definition format.
- './dist/iife/' - Immediately Invoked Function Execution - (exports
fjlMutable
as a global). - './dist/es6-module/' - Ecmascript 6 module format.
JSDocs are here (https://functional-jslib.github.io/fjl-mutable/) [https://functional-jslib.github.io/fjl-mutable/].
createTypedDescriptor, toEnumerableDescriptor, toTargetDescriptorTuple,
defineProp, defineEnumProp, defineEnumProps, defineProps
In-line summary docs follow:
Defines a property on target that is constricted to given Type (TypeRef
).
HaskellType: defineProp :: TypeRef -> String -> TargetOrTargetTuple -> Any -> Descriptor
Defines an enumerable property on target that is constricted to given Type (TypeRef
).
HaskellType: defineEnumProp :: TypeRef -> String -> TargetOrTargetTuple -> Any -> Descriptor
const someDefaultValue =
class User {
static defaultRole = 'guest';
constructor ({firstName, age}) {
defineEnumProp(String, 'firstName', this, firstName); // if `firstName` here is not of type `String`, detailed error thrown
defineEnumProp(Number, 'age', this);
defineEnumProp(String, 'role', this, User.defaultRole);
this.age = age; // This is fine since our setter expects a number
this.age = 'hello world'; // This throws a detailed error ('`#User.age`
// ... expects type: "Number" ... Type received ...' etc.
// )
}
}
// Later..
const user = new User();
user.firstName = "Some name";
user.age = '99'; // Throws (detailed) error since type does not match.
Defines enumerable properties all at once on specified target.
argTuples {Array}
- Array of argument lists of the same type thatdefineEnumProps
takes: E.g.,Array.<TypeRef, String, TargetOrTargetTuple, Any>
with one distinction: 3rd argument is optional (target argument).
const someTarget = {};
defineEnumProps([
[String, 'someProp', someDefaultValue],
[String, 'someProp2', someDefaultValue2],
[String, 'someProp3', someTarget, someDefaultValue3],
[String, 'someProp4', [someTarget, somePropDescriptor], someDefaultValue4],
], someTarget);
target {*}
- Target to define properties on.
Example usage:
const someDefaultValue = 'someValueHere';
class User {
static defaultRole = 'guest';
constructor ({firstName, age}) {
defineEnumProps([
[String, 'firstName', firstName], // if `firstName` here is not of
// type `String`, detailed error thrown
[Number, 'age'],
[String, 'role', User.defaultRole]
], target);
this.age = age; // This is fine since our setter expects a number
this.age = 'hello world'; // This throws a detailed error ('`#User.age`
// ... expects type: "Number" ... Type received ...' etc.
// )
}
}
// Later..
const user = new User();
user.firstName = "Some name";
user.age = '99'; // Throws (detailed) error since type does not match.
Same as defineEnumProps
though doesn't make props enumerable on target. See params description and examples below:
argTuples {Array}
- Array of argument lists of the same type thatdefineEnumProps
takes: E.g.,Array.<TypeRef, String, TargetOrTargetTuple, Any>
with one distinction: 3rd argument is optional (target argument).
const someTarget = {};
defineEnumProps([
[String, 'someProp', someDefaultValue],
[String, 'someProp2', someDefaultValue2],
[String, 'someProp3', someTarget, someDefaultValue3],
[String, 'someProp4', [someTarget, somePropDescriptor], someDefaultValue4],
], someTarget);
target {*}
- Target to define properties on.
Example usage:
const someDefaultValue = 'someValueHere';
class User {
static defaultRole = 'guest';
constructor ({firstName, age}) {
defineProps([
[String, 'firstName', firstName], // if `firstName` here is not of
// type `String`, detailed error thrown
[Number, 'age'],
[String, 'role', User.defaultRole]
], target);
this.age = age; // This is fine since our setter expects a number
this.age = 'hello world'; // This throws a detailed error ('`#User.age`
// ... expects type: "Number" ... Type received ...' etc.
// )
}
}
// Later..
const user = new User();
user.firstName = "Some name";
user.age = '99'; // Throws (detailed) error since type does not match.
- For commands see './package.json' scripts.
- Everything is in './src'.
- Distribution is in './dist'.
- Docs are in './docs'.
Using jest
(see './package.json' scripts).
BSD 3 Clause - Included in sources.
defineProperty
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/definePropertydefineProperties
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperties- Data Types - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures
-
Removed uncurried methods (methods ending with
$
) (use curried methods instead). Removed: -
errorIfNotTypeOnTarget$
- ('fjl' provides this now) -
errorIfNotTypeOnTarget
- ("") -
defineEnumProps$
-
defineProps$
-
Renamed auxillary methods:
_descriptorForSettable
becomescreateTypedDescriptor
._makeDescriptorEnumerable
becomestoEnumerableDescriptor
._targetDescriptorTuple
becomestoTargetDescriptorTuple
.
- Normalized API (removed un-curried methods from exports and non-api specific (un-required) methods).
- Updated build process (using babel7 now).
- Replaced
mocha
andchai
withjest
. - Changed license from "MIT" to "BSD3".
- Version and build tag links to top of readme file.
- Et. al.