objectmodel 4.4.6 → 4.4.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.eslintignore CHANGED
@@ -1,9 +1,9 @@
1
- docs/examples/common.js
2
- dist
3
- test/lib
4
- docs/**/lib
5
- docs/v1
6
- docs/v2
7
- docs/node_modules
8
- *.compiled.js
1
+ docs/examples/common.js
2
+ dist
3
+ test/lib
4
+ docs/**/lib
5
+ docs/v1
6
+ docs/v2
7
+ docs/node_modules
8
+ *.compiled.js
9
9
  *.html
package/.eslintrc.json CHANGED
@@ -1,26 +1,26 @@
1
- {
2
- "extends": "eslint:recommended",
3
- "env": {
4
- "browser": true,
5
- "node": true,
6
- "es6": true
7
- },
8
- "parserOptions": {
9
- "ecmaVersion": 2018,
10
- "sourceType": "module"
11
- },
12
- "rules": {
13
- "indent": [
14
- "error",
15
- "tab",
16
- {
17
- "SwitchCase": 1
18
- }
19
- ],
20
- "no-mixed-spaces-and-tabs": [
21
- "error",
22
- "smart-tabs"
23
- ],
24
- "no-console": "off"
25
- }
1
+ {
2
+ "extends": "eslint:recommended",
3
+ "env": {
4
+ "browser": true,
5
+ "node": true,
6
+ "es6": true
7
+ },
8
+ "parserOptions": {
9
+ "ecmaVersion": 2018,
10
+ "sourceType": "module"
11
+ },
12
+ "rules": {
13
+ "indent": [
14
+ "error",
15
+ "tab",
16
+ {
17
+ "SwitchCase": 1
18
+ }
19
+ ],
20
+ "no-mixed-spaces-and-tabs": [
21
+ "error",
22
+ "smart-tabs"
23
+ ],
24
+ "no-console": "off"
25
+ }
26
26
  }
package/LICENSE CHANGED
@@ -1,22 +1,22 @@
1
- Copyright (c) 2015 Sylvain Pollet-Villard
2
-
3
- Permission is hereby granted, free of charge, to any person
4
- obtaining a copy of this software and associated documentation
5
- files (the "Software"), to deal in the Software without
6
- restriction, including without limitation the rights to use,
7
- copy, modify, merge, publish, distribute, sublicense, and/or sell
8
- copies of the Software, and to permit persons to whom the
9
- Software is furnished to do so, subject to the following
10
- conditions:
11
-
12
- The above copyright notice and this permission notice shall be
13
- included in all copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
- OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
- HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
- WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
- OTHER DEALINGS IN THE SOFTWARE.
1
+ Copyright (c) 2015 Sylvain Pollet-Villard
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
package/README.md CHANGED
@@ -1,68 +1,68 @@
1
- <p align="center"><a href="http://objectmodel.js.org" target="_blank"><img width="400" src="http://objectmodel.js.org/docs/res/logo.png"></a></p>
2
-
3
- <p align="center">Strong Dynamically Typed Object Modeling for JavaScript</p>
4
- <p align="center">
5
- <a href="https://www.npmjs.com/package/objectmodel"><img src="https://img.shields.io/npm/dt/objectmodel.svg" alt="Downloads"></a>
6
- <a href="https://www.npmjs.com/package/objectmodel"><img src="https://img.shields.io/npm/v/objectmodel.svg" alt="Version"></a>
7
- <a href="https://travis-ci.org/sylvainpolletvillard/ObjectModel"><img src="https://travis-ci.org/sylvainpolletvillard/ObjectModel.svg?branch=master" alt="Status"></a>
8
- <a href="https://www.npmjs.com/package/objectmodel"><img src="https://img.shields.io/npm/l/objectmodel.svg" alt="License"></a>
9
- </p>
10
-
11
- ---
12
-
13
- ## What is this library ?
14
-
15
- ObjectModel intends to bring **strong dynamic type checking** to your web applications. Contrary to static type-checking solutions like [TypeScript] or [Flow], ObjectModel can also validate data at runtime: JSON from the server, form inputs, content from localStorage, external libraries...
16
-
17
- By leveraging **ES6 Proxies**, this library ensures that your variables always match the model definition and validation constraints you added to them. Thanks to the generated exceptions, it will help you spot potential bugs and save you time spent on debugging. ObjectModel is also very easy to master: *no new language to learn, no new tools, no compilation step, just a minimalist and intuitive API in a plain old JS micro-library*.
18
-
19
- Validating at runtime also brings many other benefits: you can define your own types, use them in complex model definitions with custom assertions that can even change depending on your application state. Actually it goes much further than just type safety. Go on and see for yourself.
20
-
21
- ## Installation
22
- Add the library to your project dependencies with NPM:
23
- ```bash
24
- $ npm install objectmodel
25
- ```
26
-
27
- or just [download the library from Github][github-releases]
28
-
29
- ## Basic usage example
30
-
31
- ```javascript
32
- import { ObjectModel } from "objectmodel"
33
-
34
- const Order = new ObjectModel({
35
- product: { name: String, quantity: Number },
36
- orderDate: Date
37
- });
38
-
39
- const myOrder = new Order({
40
- product: { name: "Apple Pie", quantity: 1 },
41
- orderDate: new Date()
42
- });
43
-
44
- myOrder.product.quantity = 2; // no exceptions thrown
45
- myOrder.product.quantity = false; //try to assign a Boolean
46
- // ❌ TypeError: expecting product.quantity to be Number, got Boolean false
47
- ```
48
-
49
- ## Documentation
50
-
51
- For more examples, documentation and questions, please refer to the project website: [objectmodel.js.org][website]
52
-
53
- ## Changelog and Release History
54
-
55
- Please refer to [Github Releases][github-releases]
56
-
57
- *Bug reports and pull requests are welcome.*
58
-
59
- Distributed under the MIT license. See ``LICENSE`` for more information.
60
-
61
- [website]:http://objectmodel.js.org
62
- [TypeScript]:https://www.typescriptlang.org/
63
- [Flow]:https://flowtype.org/
64
- [github-releases]:https://github.com/sylvainpolletvillard/ObjectModel/releases
65
- [npm-url]: https://npmjs.org/package/objectmodel
66
- [npm-image]: https://img.shields.io/npm/v/objectmodel.svg
67
- [npm-downloads]: https://img.shields.io/npm/dm/objectmodel.svg
1
+ <p align="center"><a href="http://objectmodel.js.org" target="_blank"><img width="400" src="http://objectmodel.js.org/docs/res/logo.png"></a></p>
2
+
3
+ <p align="center">Strong Dynamically Typed Object Modeling for JavaScript</p>
4
+ <p align="center">
5
+ <a href="https://www.npmjs.com/package/objectmodel"><img src="https://img.shields.io/npm/dt/objectmodel.svg" alt="Downloads"></a>
6
+ <a href="https://www.npmjs.com/package/objectmodel"><img src="https://img.shields.io/npm/v/objectmodel.svg" alt="Version"></a>
7
+ <a href="https://travis-ci.org/sylvainpolletvillard/ObjectModel"><img src="https://travis-ci.org/sylvainpolletvillard/ObjectModel.svg?branch=master" alt="Status"></a>
8
+ <a href="https://www.npmjs.com/package/objectmodel"><img src="https://img.shields.io/npm/l/objectmodel.svg" alt="License"></a>
9
+ </p>
10
+
11
+ ---
12
+
13
+ ## What is this library ?
14
+
15
+ ObjectModel intends to bring **strong dynamic type checking** to your web applications. Contrary to static type-checking solutions like [TypeScript] or [Flow], ObjectModel can also validate data at runtime: JSON from the server, form inputs, content from localStorage, external libraries...
16
+
17
+ By leveraging **ES6 Proxies**, this library ensures that your variables always match the model definition and validation constraints you added to them. Thanks to the generated exceptions, it will help you spot potential bugs and save you time spent on debugging. ObjectModel is also very easy to master: *no new language to learn, no new tools, no compilation step, just a minimalist and intuitive API in a plain old JS micro-library*.
18
+
19
+ Validating at runtime also brings many other benefits: you can define your own types, use them in complex model definitions with custom assertions that can even change depending on your application state. Actually it goes much further than just type safety. Go on and see for yourself.
20
+
21
+ ## Installation
22
+ Add the library to your project dependencies with NPM:
23
+ ```bash
24
+ $ npm install objectmodel
25
+ ```
26
+
27
+ or just [download the library from Github][github-releases]
28
+
29
+ ## Basic usage example
30
+
31
+ ```javascript
32
+ import { ObjectModel } from "objectmodel"
33
+
34
+ const Order = new ObjectModel({
35
+ product: { name: String, quantity: Number },
36
+ orderDate: Date
37
+ });
38
+
39
+ const myOrder = new Order({
40
+ product: { name: "Apple Pie", quantity: 1 },
41
+ orderDate: new Date()
42
+ });
43
+
44
+ myOrder.product.quantity = 2; // no exceptions thrown
45
+ myOrder.product.quantity = false; //try to assign a Boolean
46
+ // ❌ TypeError: expecting product.quantity to be Number, got Boolean false
47
+ ```
48
+
49
+ ## Documentation
50
+
51
+ For more examples, documentation and questions, please refer to the project website: [objectmodel.js.org][website]
52
+
53
+ ## Changelog and Release History
54
+
55
+ Please refer to [Github Releases][github-releases]
56
+
57
+ *Bug reports and pull requests are welcome.*
58
+
59
+ Distributed under the MIT license. See ``LICENSE`` for more information.
60
+
61
+ [website]:http://objectmodel.js.org
62
+ [TypeScript]:https://www.typescriptlang.org/
63
+ [Flow]:https://flowtype.org/
64
+ [github-releases]:https://github.com/sylvainpolletvillard/ObjectModel/releases
65
+ [npm-url]: https://npmjs.org/package/objectmodel
66
+ [npm-image]: https://img.shields.io/npm/v/objectmodel.svg
67
+ [npm-downloads]: https://img.shields.io/npm/dm/objectmodel.svg
68
68
  [license-badge]:https://img.shields.io/badge/license-MIT-blue.svg
@@ -1,3 +1,3 @@
1
- import "../src/devtool-formatter"
2
-
1
+ import "../src/devtool-formatter"
2
+
3
3
  export * from "./bundle-entry"
@@ -1,12 +1,12 @@
1
- import { Model, BasicModel, ObjectModel, ArrayModel, FunctionModel, MapModel, SetModel, Any } from "../src/index"
2
-
3
- export {
4
- Model,
5
- BasicModel,
6
- ObjectModel,
7
- ArrayModel,
8
- FunctionModel,
9
- MapModel,
10
- SetModel,
11
- Any
1
+ import { Model, BasicModel, ObjectModel, ArrayModel, FunctionModel, MapModel, SetModel, Any } from "../src/index"
2
+
3
+ export {
4
+ Model,
5
+ BasicModel,
6
+ ObjectModel,
7
+ ArrayModel,
8
+ FunctionModel,
9
+ MapModel,
10
+ SetModel,
11
+ Any
12
12
  }
@@ -1,50 +1,50 @@
1
- // ObjectModel v4.4.5 - http://objectmodel.js.org
2
- // MIT License - Sylvain Pollet-Villard
3
- const
4
- ObjectProto = Object.prototype,
5
- bettertypeof = x => ObjectProto.toString.call(x).match(/\s([a-zA-Z]+)/)[1],
6
- getProto = Object.getPrototypeOf,
7
- setProto = Object.setPrototypeOf,
8
-
9
- has = (o, prop) => ObjectProto.hasOwnProperty.call(o, prop),
10
- is = (Constructor, obj) => obj instanceof Constructor,
11
- isFunction = f => typeof f === "function",
12
- isObject = o => o && typeof o === "object",
13
- isString = s => typeof s === "string",
14
- isPlainObject = o => isObject(o) && getProto(o) === ObjectProto,
15
- isIterable = x => x && isFunction(x[Symbol.iterator]),
16
-
17
- proxify = (val, traps) => new Proxy(val, traps),
18
-
19
- merge = (target, src = {}) => {
20
- for (let key in src) {
21
- if (isPlainObject(src[key])) {
22
- const o = {};
23
- merge(o, target[key]);
24
- merge(o, src[key]);
25
- target[key] = o;
26
- } else {
27
- target[key] = src[key];
28
- }
29
- }
30
- return target
31
- },
32
-
33
- define = (obj, key, value, enumerable = false) => {
34
- Object.defineProperty(obj, key, { value, enumerable, writable: true, configurable: true });
35
- },
36
-
37
- extend = (child, parent, props) => {
38
- child.prototype = Object.assign(Object.create(parent.prototype, {
39
- constructor: {
40
- value: child,
41
- writable: true,
42
- configurable: true
43
- }
44
- }), props);
45
- setProto(child, parent);
46
- };
47
-
1
+ // ObjectModel v4.4.7 - http://objectmodel.js.org
2
+ // MIT License - Sylvain Pollet-Villard
3
+ const
4
+ ObjectProto = Object.prototype,
5
+ bettertypeof = x => ObjectProto.toString.call(x).match(/\s([a-zA-Z]+)/)[1],
6
+ getProto = Object.getPrototypeOf,
7
+ setProto = Object.setPrototypeOf,
8
+
9
+ has = (o, prop) => ObjectProto.hasOwnProperty.call(o, prop),
10
+ is = (Constructor, obj) => obj instanceof Constructor,
11
+ isFunction = f => typeof f === "function",
12
+ isObject = o => o && typeof o === "object",
13
+ isString = s => typeof s === "string",
14
+ isPlainObject = o => isObject(o) && getProto(o) === ObjectProto,
15
+ isIterable = x => x && isFunction(x[Symbol.iterator]),
16
+
17
+ proxify = (val, traps) => new Proxy(val, traps),
18
+
19
+ merge = (target, src = {}) => {
20
+ for (let key in src) {
21
+ if (isPlainObject(src[key])) {
22
+ const o = {};
23
+ merge(o, target[key]);
24
+ merge(o, src[key]);
25
+ target[key] = o;
26
+ } else {
27
+ target[key] = src[key];
28
+ }
29
+ }
30
+ return target
31
+ },
32
+
33
+ define = (obj, key, value, enumerable = false) => {
34
+ Object.defineProperty(obj, key, { value, enumerable, writable: true, configurable: true });
35
+ },
36
+
37
+ extend = (child, parent, props) => {
38
+ child.prototype = Object.assign(Object.create(parent.prototype, {
39
+ constructor: {
40
+ value: child,
41
+ writable: true,
42
+ configurable: true
43
+ }
44
+ }), props);
45
+ setProto(child, parent);
46
+ };
47
+
48
48
  const
49
49
  _check = Symbol(),
50
50
  _checked = Symbol(), // used to skip validation at instanciation for perf
@@ -519,49 +519,49 @@ Any.remaining = function (def) { this.definition = def; };
519
519
  extend(Any.remaining, Any, {
520
520
  toString() { return "..." + formatDefinition(this.definition) }
521
521
  });
522
- Any[Symbol.iterator] = function* () { yield new Any.remaining(this.definition); };
523
-
524
- const initListModel = (base, constructor, def, init, clone, mutators, otherTraps) => {
525
-
526
- return initModel(def, constructor, base, init, model => Object.assign({
527
- getPrototypeOf: () => model.prototype,
528
- get(l, key) {
529
- if (key === _original) return l
530
-
531
- const val = l[key];
532
- return isFunction(val) ? proxify(val, {
533
- apply(fn, ctx, args) {
534
- if (has(mutators, key)) {
535
- // indexes of arguments to check def + cast
536
- const [begin, end = args.length - 1, getArgDef] = mutators[key];
537
- for (let i = begin; i <= end; i++) {
538
- const argDef = getArgDef ? getArgDef(i) : model.definition;
539
- args[i] = checkDefinition(
540
- args[i],
541
- argDef,
542
- `${base.name}.${key} arguments[${i}]`,
543
- model.errors,
544
- [],
545
- true
546
- );
547
- }
548
-
549
- if (model.assertions.length > 0) {
550
- const testingClone = clone(l);
551
- fn.apply(testingClone, args);
552
- checkAssertions(testingClone, model, `after ${key} mutation`);
553
- }
554
-
555
- unstackErrors(model);
556
- }
557
-
558
- return fn.apply(l, args)
559
- }
560
- }) : val
561
- }
562
- }, otherTraps))
563
- };
564
-
522
+ Any[Symbol.iterator] = function* () { yield new Any.remaining(this.definition); };
523
+
524
+ const initListModel = (base, constructor, def, init, clone, mutators, otherTraps) => {
525
+
526
+ return initModel(def, constructor, base, init, model => Object.assign({
527
+ getPrototypeOf: () => model.prototype,
528
+ get(l, key) {
529
+ if (key === _original) return l
530
+
531
+ const val = l[key];
532
+ return isFunction(val) ? proxify(val, {
533
+ apply(fn, ctx, args) {
534
+ if (has(mutators, key)) {
535
+ // indexes of arguments to check def + cast
536
+ const [begin, end = args.length - 1, getArgDef] = mutators[key];
537
+ for (let i = begin; i <= end; i++) {
538
+ const argDef = getArgDef ? getArgDef(i) : model.definition;
539
+ args[i] = checkDefinition(
540
+ args[i],
541
+ argDef,
542
+ `${base.name}.${key} arguments[${i}]`,
543
+ model.errors,
544
+ [],
545
+ true
546
+ );
547
+ }
548
+
549
+ if (model.assertions.length > 0) {
550
+ const testingClone = clone(l);
551
+ fn.apply(testingClone, args);
552
+ checkAssertions(testingClone, model, `after ${key} mutation`);
553
+ }
554
+
555
+ unstackErrors(model);
556
+ }
557
+
558
+ return fn.apply(l, args)
559
+ }
560
+ }) : val
561
+ }
562
+ }, otherTraps))
563
+ };
564
+
565
565
  function ArrayModel(initialDefinition) {
566
566
  const model = initListModel(
567
567
  Array,
@@ -623,8 +623,8 @@ const controlMutation = (model, array, key, value, applyMutation, canBeExtended)
623
623
  const isSuccess = !unstackErrors(model);
624
624
  if (isSuccess) applyMutation(array, value);
625
625
  return isSuccess
626
- };
627
-
626
+ };
627
+
628
628
  function SetModel(initialDefinition) {
629
629
  const model = initListModel(
630
630
  Set,
@@ -659,8 +659,8 @@ extend(SetModel, Model, {
659
659
  extend(...newParts) {
660
660
  return extendModel(new SetModel(extendDefinition(this.definition, newParts)), this)
661
661
  }
662
- });
663
-
662
+ });
663
+
664
664
  function MapModel(initialKeyDefinition, initialValueDefinition) {
665
665
  const getDef = i => i === 0 ? model.definition.key : model.definition.value;
666
666
  const model = initListModel(
@@ -702,8 +702,8 @@ extend(MapModel, Model, {
702
702
  extendDefinition(this.definition.value, valueParts)
703
703
  ), this)
704
704
  }
705
- });
706
-
705
+ });
706
+
707
707
  function FunctionModel(...argsDef) {
708
708
  return initModel({ arguments: argsDef }, FunctionModel, Function, null, model => ({
709
709
  getPrototypeOf: () => model.prototype,
@@ -756,8 +756,8 @@ extend(FunctionModel, Model, {
756
756
  [_check](f, path, errors) {
757
757
  if (!isFunction(f)) stackError(errors, "Function", f, path);
758
758
  }
759
- });
760
-
759
+ });
760
+
761
761
  const styles = {
762
762
  list: "list-style-type: none; padding: 0; margin: 0;",
763
763
  listItem: "padding: 0 0 0 1em;",
@@ -949,16 +949,16 @@ const ModelInstanceFormatter = {
949
949
  if (typeof window !== "undefined") {
950
950
  window.devtoolsFormatters = (window.devtoolsFormatters || [])
951
951
  .concat(ModelFormatter, ModelInstanceFormatter);
952
- }
953
-
954
- ;
955
- //# sourceMappingURL=object-model.js.map
956
-
957
- exports.Any=Any;
958
- exports.ArrayModel=ArrayModel;
959
- exports.BasicModel=BasicModel;
960
- exports.FunctionModel=FunctionModel;
961
- exports.MapModel=MapModel;
962
- exports.Model=Model;
963
- exports.ObjectModel=ObjectModel;
952
+ }
953
+
954
+ ;
955
+ //# sourceMappingURL=object-model.js.map
956
+
957
+ exports.Any=Any;
958
+ exports.ArrayModel=ArrayModel;
959
+ exports.BasicModel=BasicModel;
960
+ exports.FunctionModel=FunctionModel;
961
+ exports.MapModel=MapModel;
962
+ exports.Model=Model;
963
+ exports.ObjectModel=ObjectModel;
964
964
  exports.SetModel=SetModel;