chi-datapackage
Advanced tools
Comparing version 5.0.1 to 5.0.2
@@ -9,2 +9,5 @@ CHANGELOG | ||
## 5.0.2 (2016-10-07) | ||
* Fix errors when data is defined inline | ||
## 5.0.1 (2016-09-30) | ||
@@ -11,0 +14,0 @@ * Fix Block-scoped declarations (let, const, function, class) not yet supported outside strict mode |
'use strict'; | ||
module.exports.normalizeObject = function (obj) { | ||
function normalizeObject(obj) { | ||
if (typeof obj === 'string') { | ||
@@ -10,2 +10,23 @@ return { path: obj }; | ||
return Object.assign({}, obj); | ||
} | ||
function makeNonEnumerable(object, propNames) { | ||
for (var i = 0; i < propNames.length; i++) { | ||
addHiddenProp(object, propNames[i], object[propNames[i]]); | ||
} | ||
} | ||
function addHiddenProp(object, propName, value) { | ||
Object.defineProperty(object, propName, { | ||
enumerable: false, | ||
writable: true, | ||
configurable: true, | ||
value: value | ||
}); | ||
} | ||
module.exports = { | ||
normalizeObject: normalizeObject, | ||
makeNonEnumerable: makeNonEnumerable, | ||
addHiddenProp: addHiddenProp | ||
}; |
@@ -13,2 +13,3 @@ 'use strict'; | ||
var DataPackageService = require('./service'); | ||
var makeNonEnumerable = require('./lib/utils').makeNonEnumerable; | ||
@@ -30,17 +31,2 @@ var dataPackageService = new DataPackageService(); | ||
function makeNonEnumerable(object, propNames) { | ||
for (var i = 0; i < propNames.length; i++) { | ||
addHiddenProp(object, propNames[i], object[propNames[i]]); | ||
} | ||
} | ||
function addHiddenProp(object, propName, value) { | ||
Object.defineProperty(object, propName, { | ||
enumerable: false, | ||
writable: true, | ||
configurable: true, | ||
value: value | ||
}); | ||
} | ||
var makeResources = function makeResources(datapackage, resources) { | ||
@@ -60,2 +46,5 @@ return (resources || datapackage.resources).map(function (resource) { | ||
var _data_ = _resource_.data; | ||
delete _resource_.data; | ||
var store = extendObservable(this, _resource_, { | ||
@@ -67,26 +56,26 @@ content: _resource_.content || '', | ||
$processedCount: 0, | ||
data: asReference(''), | ||
update: action(function (data) { | ||
debug('update', data.name); | ||
data = isObservable(data) ? toJS(data) : data; | ||
Object.assign(data, { | ||
data: asReference(_data_), | ||
update: action(function (state) { | ||
debug('update', state.name); | ||
state = isObservable(state) ? toJS(state) : state; | ||
Object.assign(state, { | ||
name: store.name | ||
}, data); | ||
data = normalize.resource(_datapackage_, data); | ||
schemaProcessor.normalizeResource(_datapackage_, data); | ||
return Object.assign(_this, data); | ||
}, state); | ||
state = normalize.resource(_datapackage_, state); | ||
schemaProcessor.normalizeResource(_datapackage_, state); | ||
return Object.assign(_this, state); | ||
// return this.updateData(Object.assign(this, data)); | ||
}), | ||
updateData: action(function (data) { | ||
debug('updateData', data.name); | ||
updateData: action(function (state) { | ||
debug('updateData', state.name); | ||
try { | ||
_this.$processedCount++; | ||
data = processor.resource(data); | ||
if (data.errors && data.errors.length > 0) { | ||
data.$error = new Error('Errors processing resource ' + data.name); | ||
state = processor.resource(state); | ||
if (state.errors && state.errors.length > 0) { | ||
state.$error = new Error('Errors processing resource ' + state.name); | ||
} | ||
} catch (err) { | ||
data.$error = err; | ||
data.errors = data.errors || []; | ||
data.errors.shift({ | ||
state.$error = err; | ||
state.errors = state.errors || []; | ||
state.errors.shift({ | ||
code: 'Parsing', | ||
@@ -98,5 +87,5 @@ type: err.name, | ||
if (data.$error || data.errors.length > 0) { | ||
_this.errors = data.errors; | ||
_this.$error = data.$error; | ||
if (state.$error || state.errors.length > 0) { | ||
_this.errors = state.errors; | ||
_this.$error = state.$error; | ||
_this.$error.message = _this.name; | ||
@@ -107,3 +96,3 @@ _this.$valid = false; | ||
_this.$error = null; | ||
_this.data = data.data; | ||
_this.data = state.data; | ||
_this.$lastValidContent = _this.content; | ||
@@ -115,3 +104,5 @@ } | ||
return { | ||
name: store.name, | ||
name: mobx.untracked(function () { | ||
return store.name; | ||
}), | ||
content: store.content, | ||
@@ -124,3 +115,5 @@ schema: store.schema, | ||
errors: [], | ||
data: [] | ||
data: mobx.untracked(function () { | ||
return store.data; | ||
}) | ||
}; | ||
@@ -133,12 +126,25 @@ } | ||
store.update(store); | ||
reaction(function () { | ||
return store.core; | ||
}, function (data) { | ||
debug('update *reaction*', data.name); | ||
store.updateData(data); | ||
}, true); | ||
store.updateData(store.core); | ||
store.start(); | ||
} | ||
_createClass(Resource, [{ | ||
key: 'start', | ||
value: function start() { | ||
var _this2 = this; | ||
this.stop = reaction(function () { | ||
return _this2.core; | ||
}, function (data) { | ||
return _this2.updateData(data); | ||
}); | ||
return this.stop; | ||
} | ||
}, { | ||
key: 'stop', | ||
value: function stop() { | ||
// noop | ||
} | ||
}, { | ||
key: 'load', | ||
@@ -161,3 +167,3 @@ value: function load() { | ||
function Package(_datapackage_) { | ||
var _this2 = this; | ||
var _this3 = this; | ||
@@ -172,2 +178,5 @@ _classCallCheck(this, Package); | ||
var resources = _datapackage_.resources; | ||
var schemas = _datapackage_.schemas; | ||
delete _datapackage_.resources; | ||
@@ -180,4 +189,4 @@ delete _datapackage_.schemas; | ||
$isLoadingResources: false, | ||
resources: asFlat(makeResources(_datapackage_, [])), | ||
// schemas: asReference({}), | ||
resources: asFlat(makeResources(_datapackage_, resources)), | ||
schemas: asReference(schemas), | ||
get $resourcesByName() { | ||
@@ -190,23 +199,23 @@ return Normalizer.index(this); | ||
load: action(function () { | ||
_this2.$isLoadingPackage = true; | ||
return loader.datapackage(_this2).then(_this2.update); | ||
_this3.$isLoadingPackage = true; | ||
return loader.datapackage(_this3).then(_this3.update); | ||
}), | ||
loadResources: action(function () { | ||
_this2.$isLoadingResources = true; | ||
return loader.resources(_this2.resources).then(_this2.replaceResources); | ||
_this3.$isLoadingResources = true; | ||
return loader.resources(_this3.resources).then(_this3.replaceResources); | ||
}), | ||
update: action(function (data) { | ||
data = isObservable(data) ? toJS(data) : data; | ||
Object.assign(_this2, data); | ||
_this2.normalize(); | ||
_this2.$isLoadingPackage = false; | ||
return _this2.loadResources(); | ||
Object.assign(_this3, data); | ||
_this3.normalize(); | ||
_this3.$isLoadingPackage = false; | ||
return _this3.loadResources(); | ||
}), | ||
replaceResources: action(function (arr) { | ||
_this2.$isLoadingResources = false; | ||
_this2.resources.replace(makeResources(_this2, arr)); | ||
return _this2; | ||
_this3.$isLoadingResources = false; | ||
_this3.resources.replace(makeResources(_this3, arr)); | ||
return _this3; | ||
}), | ||
normalize: action(function () { | ||
var p = normalize.datapackage(_this2); | ||
var p = normalize.datapackage(_this3); | ||
@@ -231,6 +240,6 @@ // normalize resources | ||
return Object.assign(_this2, p, { resources: resources }); | ||
return Object.assign(_this3, p, { resources: resources }); | ||
}), | ||
addResource: action(function (resource) { | ||
_this2.resources.push(makeResource(_this2, resource)); | ||
_this3.resources.push(makeResource(_this3, resource)); | ||
}) | ||
@@ -241,6 +250,12 @@ }); | ||
this.stop$resourcesByName = keepAlive(store, '$resourcesByName'); | ||
store.start(); | ||
} | ||
_createClass(Package, [{ | ||
key: 'start', | ||
value: function start() { | ||
this.stop = keepAlive(this, '$resourcesByName'); | ||
return this.stop; | ||
} | ||
}, { | ||
key: 'updateResource', | ||
@@ -247,0 +262,0 @@ value: function updateResource(resource) { |
{ | ||
"name": "chi-datapackage", | ||
"version": "5.0.1", | ||
"version": "5.0.2", | ||
"description": "Normalize datapackage and datapackage resources", | ||
"main": "index.js", | ||
"main": "src/index.js", | ||
"browser": "dist/index.js", | ||
"files": [ | ||
@@ -7,0 +8,0 @@ "src", |
@@ -6,3 +6,3 @@ # chi-datapackage | ||
> A utility library for working with [datapackage files](http://frictionlessdata.io/guides/data-package/) in Node and the browser. | ||
> A utility library for working with [Data Package files](http://frictionlessdata.io/guides/data-package/) in Node and the browser. | ||
> Designed for use in [Project χ](https://github.com/Hypercubed/Project-Chi). | ||
@@ -17,2 +17,3 @@ | ||
* Customizable loader, mime-types, translators, and data-types. | ||
* Generates an observable Data Package data store. | ||
@@ -42,3 +43,3 @@ ## Goals | ||
dp.load('//datapackage/path/or/url') | ||
dp.makePackage('//datapackage/path/or/url').load() | ||
.then(datapackage => { | ||
@@ -45,0 +46,0 @@ /* so something */ |
module.exports.normalizeObject = function (obj) { | ||
function normalizeObject (obj) { | ||
if (typeof obj === 'string') { | ||
@@ -9,2 +9,23 @@ return {path: obj}; | ||
return Object.assign({}, obj); | ||
} | ||
function makeNonEnumerable (object, propNames) { | ||
for (let i = 0; i < propNames.length; i++) { | ||
addHiddenProp(object, propNames[i], object[propNames[i]]); | ||
} | ||
} | ||
function addHiddenProp (object, propName, value) { | ||
Object.defineProperty(object, propName, { | ||
enumerable: false, | ||
writable: true, | ||
configurable: true, | ||
value | ||
}); | ||
} | ||
module.exports = { | ||
normalizeObject, | ||
makeNonEnumerable, | ||
addHiddenProp | ||
}; |
@@ -9,2 +9,3 @@ 'use strict'; | ||
const DataPackageService = require('./service'); | ||
const makeNonEnumerable = require('./lib/utils').makeNonEnumerable; | ||
@@ -26,17 +27,2 @@ const dataPackageService = new DataPackageService(); | ||
function makeNonEnumerable (object, propNames) { | ||
for (let i = 0; i < propNames.length; i++) { | ||
addHiddenProp(object, propNames[i], object[propNames[i]]); | ||
} | ||
} | ||
function addHiddenProp (object, propName, value) { | ||
Object.defineProperty(object, propName, { | ||
enumerable: false, | ||
writable: true, | ||
configurable: true, | ||
value | ||
}); | ||
} | ||
const makeResources = (datapackage, resources) => | ||
@@ -49,2 +35,5 @@ (resources || datapackage.resources).map(resource => makeResource(datapackage, resource)); | ||
const _data_ = _resource_.data; | ||
delete _resource_.data; | ||
const store = extendObservable(this, _resource_, { | ||
@@ -56,26 +45,26 @@ content: _resource_.content || '', | ||
$processedCount: 0, | ||
data: asReference(''), | ||
update: action(data => { | ||
debug('update', data.name); | ||
data = isObservable(data) ? toJS(data) : data; | ||
Object.assign(data, { | ||
data: asReference(_data_), | ||
update: action(state => { | ||
debug('update', state.name); | ||
state = isObservable(state) ? toJS(state) : state; | ||
Object.assign(state, { | ||
name: store.name | ||
}, data); | ||
data = normalize.resource(_datapackage_, data); | ||
schemaProcessor.normalizeResource(_datapackage_, data); | ||
return Object.assign(this, data); | ||
}, state); | ||
state = normalize.resource(_datapackage_, state); | ||
schemaProcessor.normalizeResource(_datapackage_, state); | ||
return Object.assign(this, state); | ||
// return this.updateData(Object.assign(this, data)); | ||
}), | ||
updateData: action(data => { | ||
debug('updateData', data.name); | ||
updateData: action(state => { | ||
debug('updateData', state.name); | ||
try { | ||
this.$processedCount++; | ||
data = processor.resource(data); | ||
if (data.errors && data.errors.length > 0) { | ||
data.$error = new Error(`Errors processing resource ${data.name}`); | ||
state = processor.resource(state); | ||
if (state.errors && state.errors.length > 0) { | ||
state.$error = new Error(`Errors processing resource ${state.name}`); | ||
} | ||
} catch (err) { | ||
data.$error = err; | ||
data.errors = data.errors || []; | ||
data.errors.shift({ | ||
state.$error = err; | ||
state.errors = state.errors || []; | ||
state.errors.shift({ | ||
code: 'Parsing', | ||
@@ -87,5 +76,5 @@ type: err.name, | ||
if (data.$error || data.errors.length > 0) { | ||
this.errors = data.errors; | ||
this.$error = data.$error; | ||
if (state.$error || state.errors.length > 0) { | ||
this.errors = state.errors; | ||
this.$error = state.$error; | ||
this.$error.message = this.name; | ||
@@ -96,3 +85,3 @@ this.$valid = false; | ||
this.$error = null; | ||
this.data = data.data; | ||
this.data = state.data; | ||
this.$lastValidContent = this.content; | ||
@@ -104,3 +93,3 @@ } | ||
return { | ||
name: store.name, | ||
name: mobx.untracked(() => store.name), | ||
content: store.content, | ||
@@ -113,3 +102,3 @@ schema: store.schema, | ||
errors: [], | ||
data: [] | ||
data: mobx.untracked(() => store.data) | ||
}; | ||
@@ -128,13 +117,19 @@ } | ||
store.update(store); | ||
store.updateData(store.core); | ||
store.start(); | ||
} | ||
reaction( | ||
() => (store.core), | ||
data => { | ||
debug('update *reaction*', data.name); | ||
store.updateData(data); | ||
}, | ||
true | ||
start () { | ||
this.stop = reaction( | ||
() => (this.core), | ||
data => this.updateData(data) | ||
); | ||
return this.stop; | ||
} | ||
stop () { | ||
// noop | ||
} | ||
load () { // TODO: test | ||
@@ -158,2 +153,5 @@ return loader.resource(this) | ||
const resources = _datapackage_.resources; | ||
const schemas = _datapackage_.schemas; | ||
delete _datapackage_.resources; | ||
@@ -166,4 +164,4 @@ delete _datapackage_.schemas; | ||
$isLoadingResources: false, | ||
resources: asFlat(makeResources(_datapackage_, [])), | ||
// schemas: asReference({}), | ||
resources: asFlat(makeResources(_datapackage_, resources)), | ||
schemas: asReference(schemas), | ||
get $resourcesByName () { | ||
@@ -232,5 +230,10 @@ return Normalizer.index(this); | ||
this.stop$resourcesByName = keepAlive(store, '$resourcesByName'); | ||
store.start(); | ||
} | ||
start () { | ||
this.stop = keepAlive(this, '$resourcesByName'); | ||
return this.stop; | ||
} | ||
updateResource (resource) { | ||
@@ -237,0 +240,0 @@ this.resources.find(p => resource.name === p.name).update(resource); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
78315
37
2136
94