moddle
Advanced tools
Comparing version 7.0.0-exp.3 to 7.0.0
@@ -1,7 +0,3 @@ | ||
'use strict'; | ||
import { forEach, bind, pick, assign, isString, isObject, set } from 'min-dash'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
var minDash = require('min-dash'); | ||
/** | ||
@@ -40,3 +36,3 @@ * Moddle base element. | ||
// initialize default values | ||
minDash.forEach(descriptor.properties, function(p) { | ||
forEach(descriptor.properties, function(p) { | ||
if (!p.isMany && p.default !== undefined) { | ||
@@ -60,3 +56,3 @@ prototype[p.name] = p.default; | ||
minDash.forEach(attrs, minDash.bind(function(val, key) { | ||
forEach(attrs, bind(function(val, key) { | ||
this.set(key, val); | ||
@@ -143,9 +139,11 @@ }, this)); | ||
prefix = defaultPrefix; | ||
} else | ||
} | ||
// prefix + local name | ||
if (parts.length === 2) { | ||
else if (parts.length === 2) { | ||
localName = parts[1]; | ||
prefix = parts[0]; | ||
} else { | ||
} | ||
else { | ||
throw new Error('expected <prefix:localName> or <localName>, got ' + name); | ||
@@ -177,3 +175,3 @@ } | ||
DescriptorBuilder.prototype.build = function() { | ||
return minDash.pick(this, [ | ||
return pick(this, [ | ||
'ns', | ||
@@ -326,2 +324,13 @@ 'name', | ||
DescriptorBuilder.prototype.assertNotTrait = function(typeDescriptor) { | ||
const _extends = typeDescriptor.extends || []; | ||
if (_extends.length) { | ||
throw new Error( | ||
`cannot create <${ typeDescriptor.name }> extending <${ typeDescriptor.extends }>` | ||
); | ||
} | ||
}; | ||
DescriptorBuilder.prototype.assertNotDefined = function(p, name) { | ||
@@ -345,2 +354,6 @@ var propertyName = p.name, | ||
if (inherited) { | ||
this.assertNotTrait(t); | ||
} | ||
var typesByName = this.allTypesByName, | ||
@@ -355,6 +368,6 @@ types = this.allTypes; | ||
minDash.forEach(t.properties, minDash.bind(function(p) { | ||
forEach(t.properties, bind(function(p) { | ||
// clone property to allow extensions | ||
p = minDash.assign({}, p, { | ||
p = assign({}, p, { | ||
name: p.ns.localName, | ||
@@ -403,3 +416,3 @@ inherited: inherited | ||
minDash.forEach(packages, minDash.bind(this.registerPackage, this)); | ||
forEach(packages, bind(this.registerPackage, this)); | ||
} | ||
@@ -420,3 +433,3 @@ | ||
// copy package | ||
pkg = minDash.assign({}, pkg); | ||
pkg = assign({}, pkg); | ||
@@ -429,3 +442,3 @@ var pkgMap = this.packageMap; | ||
// register types | ||
minDash.forEach(pkg.types, minDash.bind(function(descriptor) { | ||
forEach(pkg.types, bind(function(descriptor) { | ||
this.registerType(descriptor, pkg); | ||
@@ -444,7 +457,7 @@ }, this)); | ||
type = minDash.assign({}, type, { | ||
type = assign({}, type, { | ||
superClass: (type.superClass || []).slice(), | ||
extends: (type.extends || []).slice(), | ||
properties: (type.properties || []).slice(), | ||
meta: minDash.assign((type.meta || {})) | ||
meta: assign((type.meta || {})) | ||
}); | ||
@@ -457,3 +470,3 @@ | ||
// parse properties | ||
minDash.forEach(type.properties, minDash.bind(function(p) { | ||
forEach(type.properties, bind(function(p) { | ||
@@ -469,3 +482,3 @@ // namespace property names | ||
minDash.assign(p, { | ||
assign(p, { | ||
ns: propertyNs, | ||
@@ -479,3 +492,3 @@ name: propertyName | ||
// update ns + name | ||
minDash.assign(type, { | ||
assign(type, { | ||
ns: ns, | ||
@@ -486,5 +499,7 @@ name: name, | ||
minDash.forEach(type.extends, minDash.bind(function(extendsName) { | ||
var extended = this.typeMap[extendsName]; | ||
forEach(type.extends, bind(function(extendsName) { | ||
var extendsNameNs = parseName(extendsName, ns.prefix); | ||
var extended = this.typeMap[extendsNameNs.name]; | ||
extended.traits = extended.traits || []; | ||
@@ -518,2 +533,13 @@ extended.traits.push(name); | ||
/** | ||
* Traverse the selected super type or trait | ||
* | ||
* @param {String} cls | ||
* @param {Boolean} [trait=false] | ||
*/ | ||
function traverse(cls, trait) { | ||
var parentNs = parseName(cls, isBuiltIn(cls) ? '' : nsName.prefix); | ||
self.mapTypes(parentNs, iterator, trait); | ||
} | ||
/** | ||
* Traverse the selected trait. | ||
@@ -524,14 +550,12 @@ * | ||
function traverseTrait(cls) { | ||
return traverseSuper(cls, true); | ||
return traverse(cls, true); | ||
} | ||
/** | ||
* Traverse the selected super type or trait | ||
* Traverse the selected super type | ||
* | ||
* @param {String} cls | ||
* @param {Boolean} [trait=false] | ||
*/ | ||
function traverseSuper(cls, trait) { | ||
var parentNs = parseName(cls, isBuiltIn(cls) ? '' : nsName.prefix); | ||
self.mapTypes(parentNs, iterator, trait); | ||
function traverseSuper(cls) { | ||
return traverse(cls, false); | ||
} | ||
@@ -543,3 +567,3 @@ | ||
minDash.forEach(type.superClass, trait ? traverseTrait : traverseSuper); | ||
forEach(type.superClass, trait ? traverseTrait : traverseSuper); | ||
@@ -549,3 +573,3 @@ // call iterator with (type, inherited=!trait) | ||
minDash.forEach(type.traits, traverseTrait); | ||
forEach(type.traits, traverseTrait); | ||
}; | ||
@@ -617,7 +641,7 @@ | ||
if (!minDash.isString(name) || !name.length) { | ||
if (!isString(name) || !name.length) { | ||
throw new TypeError('property name must be a non-empty string'); | ||
} | ||
var property = this.model.getPropertyDescriptor(target, name); | ||
var property = this.getProperty(target, name); | ||
@@ -633,3 +657,3 @@ var propertyName = property && property.name; | ||
} else { | ||
delete target.$attrs[name]; | ||
delete target.$attrs[stripGlobal(name)]; | ||
} | ||
@@ -647,3 +671,3 @@ } else { | ||
} else { | ||
target.$attrs[name] = value; | ||
target.$attrs[stripGlobal(name)] = value; | ||
} | ||
@@ -663,6 +687,6 @@ } | ||
var property = this.model.getPropertyDescriptor(target, name); | ||
var property = this.getProperty(target, name); | ||
if (!property) { | ||
return target.$attrs[name]; | ||
return target.$attrs[stripGlobal(name)]; | ||
} | ||
@@ -696,3 +720,3 @@ | ||
// cf. https://github.com/bpmn-io/internal-docs/issues/386 | ||
options = minDash.assign({}, options, { | ||
options = assign({}, options, { | ||
get: function() { return value; } | ||
@@ -722,3 +746,41 @@ }); | ||
/** | ||
* Return property with the given name on the element. | ||
* | ||
* @param {any} target | ||
* @param {string} name | ||
* | ||
* @return {object | null} property | ||
*/ | ||
Properties.prototype.getProperty = function(target, name) { | ||
var model = this.model; | ||
var property = model.getPropertyDescriptor(target, name); | ||
if (property) { | ||
return property; | ||
} | ||
if (name.includes(':')) { | ||
return null; | ||
} | ||
const strict = model.config.strict; | ||
if (typeof strict !== 'undefined') { | ||
const error = new TypeError(`unknown property <${ name }> on <${ target.$type }>`); | ||
if (strict) { | ||
throw error; | ||
} else { | ||
// eslint-disable-next-line no-undef | ||
typeof console !== 'undefined' && console.warn(error); | ||
} | ||
} | ||
return null; | ||
}; | ||
function isUndefined(val) { | ||
@@ -737,2 +799,6 @@ return typeof val === 'undefined'; | ||
function stripGlobal(name) { | ||
return name.replace(/^:/, ''); | ||
} | ||
// Moddle implementation ///////////////////////////////////////////////// | ||
@@ -760,4 +826,6 @@ | ||
* @param {Array<Package>} packages the packages to contain | ||
* | ||
* @param { { strict?: boolean } } [config] moddle configuration | ||
*/ | ||
function Moddle(packages) { | ||
function Moddle(packages, config = {}) { | ||
@@ -770,2 +838,4 @@ this.properties = new Properties(this); | ||
this.typeCache = {}; | ||
this.config = config; | ||
} | ||
@@ -816,3 +886,3 @@ | ||
var name = minDash.isString(descriptor) ? descriptor : descriptor.ns.name; | ||
var name = isString(descriptor) ? descriptor : descriptor.ns.name; | ||
@@ -865,2 +935,8 @@ var type = cache[name]; | ||
return type === this.$type; | ||
}, | ||
get: function(key) { | ||
return this[key]; | ||
}, | ||
set: function(key, value) { | ||
set(this, [ key ], value); | ||
} | ||
@@ -881,7 +957,9 @@ }; | ||
this.properties.defineModel(element, this); | ||
this.properties.define(element, 'get', { enumerable: false, writable: true }); | ||
this.properties.define(element, 'set', { enumerable: false, writable: true }); | ||
this.properties.define(element, '$parent', { enumerable: false, writable: true }); | ||
this.properties.define(element, '$instanceOf', { enumerable: false, writable: true }); | ||
minDash.forEach(properties, function(a, key) { | ||
if (minDash.isObject(a) && a.value !== undefined) { | ||
forEach(properties, function(a, key) { | ||
if (isObject(a) && a.value !== undefined) { | ||
element[a.name] = a.value; | ||
@@ -952,7 +1030,3 @@ } else { | ||
exports.Moddle = Moddle; | ||
exports.coerceType = coerceType; | ||
exports.isBuiltInType = isBuiltIn; | ||
exports.isSimpleType = isSimple; | ||
exports.parseNameNS = parseName; | ||
export { Moddle, coerceType, isBuiltIn as isBuiltInType, isSimple as isSimpleType, parseName as parseNameNS }; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "moddle", | ||
"version": "7.0.0-exp.3", | ||
"version": "7.0.0", | ||
"description": "A library for importing meta-model based file formats into JS", | ||
"scripts": { | ||
"all": "run-s lint test distro", | ||
"all": "run-s lint test test:build", | ||
"lint": "eslint .", | ||
"pretest": "run-s build", | ||
"dev": "npm test -- --watch", | ||
"test": "mocha --reporter=spec --recursive test/spec", | ||
"distro": "run-s build test:build", | ||
"build": "rollup -c", | ||
"prepare": "npm run build", | ||
"test:build": "mocha --reporter=spec --recursive test/distro", | ||
"prepublishOnly": "run-s distro" | ||
"test:build": "mocha --reporter=spec --recursive test/distro" | ||
}, | ||
"type": "module", | ||
"exports": { | ||
".": { | ||
"import": "./dist/index.js", | ||
"require": "./dist/index.cjs" | ||
}, | ||
"./package.json": "./package.json" | ||
}, | ||
"repository": { | ||
@@ -21,6 +27,2 @@ "type": "git", | ||
}, | ||
"main": "dist/index.cjs", | ||
"module": "dist/index.esm.js", | ||
"umd:main": "dist/moddle.umd.cjs", | ||
"source": "lib/index.js", | ||
"keywords": [ | ||
@@ -47,18 +49,18 @@ "model", | ||
"devDependencies": { | ||
"@rollup/plugin-commonjs": "^24.0.1", | ||
"@rollup/plugin-node-resolve": "^15.0.1", | ||
"@rollup/plugin-terser": "^0.4.0", | ||
"ajv": "^8.12.0", | ||
"chai": "^4.3.7", | ||
"eslint": "^8.34.0", | ||
"eslint": "^8.57.0", | ||
"eslint-plugin-bpmn-io": "^1.0.0", | ||
"fast-glob": "^3.3.2", | ||
"mocha": "^10.2.0", | ||
"npm-run-all": "^4.1.2", | ||
"rollup": "^3.17.2" | ||
"rollup": "^4.12.0" | ||
}, | ||
"dependencies": { | ||
"min-dash": "^4.0.0" | ||
"min-dash": "^4.2.1" | ||
}, | ||
"files": [ | ||
"dist" | ||
"dist", | ||
"docs/moddle.json" | ||
] | ||
} |
@@ -10,6 +10,9 @@ # moddle | ||
__moddle__ offers you a concise way to define [meta models](https://en.wikipedia.org/wiki/Metamodeling) in JavaScript. You can use these models to consume documents, create model elements and perform model validation. | ||
[moddle](https://github.com/bpmn-io/moddle) offers you a concise way to define [meta models](https://en.wikipedia.org/wiki/Metamodeling) in JavaScript. You can use these models to consume documents, create model elements, and perform model validation. | ||
A moddle description is a simple [JSON](http://json.org/) file that describes types, their properties and relationships: | ||
### Define a schema | ||
You start by creating a [moddle schema](./docs/descriptor.md). It is a [JSON](http://json.org/) file which describes types, their properties, and relationships: | ||
```json | ||
@@ -40,3 +43,4 @@ { | ||
{ "name": "power", "type": "Integer", "isAttr": true }, | ||
{ "name": "similar", "type": "Car", "isMany": true, "isReference": true } | ||
{ "name": "similar", "type": "Car", "isMany": true, "isReference": true }, | ||
{ "name": "trunk", "type": "Element", "isMany": true } | ||
] | ||
@@ -48,4 +52,7 @@ } | ||
__moddle__ allows you to instantiate that definition and create objects from it: | ||
### Instantiate moddle | ||
You can instantiate a moddle instance with a set of defined schemas: | ||
```javascript | ||
@@ -55,3 +62,10 @@ import { Moddle } from 'moddle'; | ||
var cars = new Moddle([ carsJSON ]); | ||
``` | ||
### Create objects | ||
Use a [moddle](https://github.com/bpmn-io/moddle) instance to create objects of your defined types: | ||
```javascript | ||
var taiga = cars.create('c:Car', { name: 'Taiga' }); | ||
@@ -73,4 +87,7 @@ | ||
Then again, __moddle__ allows you to perform introspection on model instances, too. | ||
### Introspect things | ||
Then again, given the knowledge [moddle](https://github.com/bpmn-io/moddle) has, you can perform deep introspection: | ||
```javascript | ||
@@ -83,2 +100,27 @@ var carDescriptor = cheapCar.$descriptor; | ||
### Access extensions | ||
moddle is friendly towards extensions and keeps unknown _any_ properties around: | ||
```javascript | ||
taiga.set('specialProperty', 'not known to moddle'); | ||
console.log(taiga.get('specialProperty')); | ||
// 'not known to moddle' | ||
``` | ||
It also allows you to create _any_ elements for namespaces that you did not explicitly define: | ||
```javascript | ||
var screwdriver = cars.createAny('tools:Screwdriver', 'http://tools', { | ||
make: 'ScrewIt!' | ||
}); | ||
car.trunk.push(screwdriver); | ||
``` | ||
### There is more | ||
Have a look at our [test coverage](https://github.com/bpmn-io/moddle/blob/master/test/spec) to learn about everything that is currently supported. | ||
@@ -85,0 +127,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
8
0
138
151116
8
1684
Updatedmin-dash@^4.2.1