chi-datapackage
Advanced tools
Comparing version 2.0.0 to 2.1.0
'use strict'; | ||
var d3time = require('d3-time-format'); | ||
var parseIsoDuration = require('parse-iso-duration'); | ||
var jsonParse = require('./json'); | ||
var TRUE_VALUES = ['yes', 'y', 'true', 't', '1']; | ||
var typeToCast = { | ||
string: { | ||
default: String | ||
/* todo: format, uri, email, binary: */ | ||
/* todo: format, uri, email, binary? */ | ||
}, | ||
@@ -15,12 +20,33 @@ integer: { | ||
default: parseFloat | ||
// todo: format, example currency | ||
// todo: currency? | ||
}, | ||
datetime: { | ||
default: d3time.utcParse('%Y-%m-%dT%H:%M:%SZ'), | ||
fmt: d3time.utcParse, | ||
any: function any(d) { | ||
return new Date(d); | ||
} | ||
}, | ||
date: { | ||
default: function _default(d) { | ||
default: d3time.utcParse('%Y-%m-%d'), | ||
fmt: d3time.utcParse, | ||
yyyy: d3time.utcParse('%Y'), | ||
any: function any(d) { | ||
return new Date(d); | ||
} | ||
// todo: format, example "yyyy" | ||
}, | ||
time: { | ||
default: d3time.utcParse('%H:%M:%S'), | ||
fmt: d3time.utcParse, | ||
any: function any(d) { | ||
return new Date(d); | ||
} | ||
}, | ||
duration: { | ||
default: parseIsoDuration | ||
}, | ||
boolean: { | ||
default: Boolean | ||
default: function _default(d) { | ||
return d === true || TRUE_VALUES.indexOf(String(d).toLowerCase()) !== -1; | ||
} | ||
}, | ||
@@ -33,6 +59,3 @@ object: { | ||
typeToCast.array = typeToCast.object; | ||
typeToCast.time = typeToCast.date; | ||
typeToCast.datetime = typeToCast.date; | ||
typeToCast.date.any = typeToCast.date.default; | ||
module.exports = typeToCast; |
@@ -29,6 +29,5 @@ 'use strict'; | ||
schema.fields.forEach(function (field) { | ||
field.format = field.format || 'default'; | ||
if (field.type in _this.types) { | ||
var type = _this.types[field.type]; | ||
castMap[field.name] = type[field.format] || type.default; | ||
var fn = _this.generateCastFn(field); | ||
if (fn) { | ||
castMap[field.name] = fn; | ||
} | ||
@@ -39,2 +38,19 @@ }); | ||
}, { | ||
key: 'generateCastFn', | ||
value: function generateCastFn(field) { | ||
if (!field.type || !Object.prototype.hasOwnProperty.call(this.types, field.type)) { | ||
return null; | ||
} | ||
var type = this.types[field.type]; | ||
var format = field.format || 'default'; | ||
var pattern = null; | ||
if (format.indexOf(':') !== -1) { | ||
var s = format.split(':'); | ||
format = s[0]; | ||
pattern = s[1]; | ||
} | ||
var map = type[format] || type.default; | ||
return pattern ? map(pattern) : map; | ||
} | ||
}, { | ||
key: 'process', | ||
@@ -41,0 +57,0 @@ value: function process(resource) { |
{ | ||
"name": "chi-datapackage", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "Normalize datapackage and datapackage resources", | ||
@@ -14,3 +14,4 @@ "main": "index.js", | ||
"coverage": "nyc --reporter=lcov --reporter=text ava", | ||
"prepublish": "npm run build" | ||
"prepublish": "npm run build", | ||
"version": "chg release -y && git add -A CHANGELOG.md" | ||
}, | ||
@@ -20,7 +21,7 @@ "author": "J. Harshbarger", | ||
"devDependencies": { | ||
"ava": "^0.15.2", | ||
"ava": "^0.16.0", | ||
"babel-cli": "^6.11.4", | ||
"babel-preset-es2015": "^6.9.0", | ||
"babel-preset-es2015": "^6.13.2", | ||
"eslint-plugin-node": "^2.0.0", | ||
"nyc": "^7.0.0", | ||
"nyc": "^7.1.0", | ||
"xo": "^0.16.0" | ||
@@ -75,2 +76,3 @@ }, | ||
"crlf-helper": "^0.1.0", | ||
"d3-time-format": "^2.0.2", | ||
"datapackage-identifier": "^0.4.1", | ||
@@ -83,2 +85,3 @@ "debug": "^2.2.0", | ||
"mime-lookup": "0.0.2", | ||
"parse-iso-duration": "^1.0.0", | ||
"urijs": "^1.18.1" | ||
@@ -85,0 +88,0 @@ }, |
@@ -6,18 +6,40 @@ # chi-datapackage | ||
> Normalize datapackage and datapackage resources | ||
> A utility library for working with [datapackage files](http://frictionlessdata.io/guides/data-package/) in Node and the browser. | ||
> Designed for use in [Project χ](https://github.com/Hypercubed/Project-Chi). | ||
## Features | ||
* DataPackage and resource loader (node and browser compatible). | ||
* DataPackage and resource normalizer. | ||
* Resource translators (tsv, csv, yaml, json, etc). | ||
* JSON Table Schema processor (include type casting). | ||
* Customizable loader, mime-types, translators, and data-types. | ||
## Goals | ||
* A set of utilities for working with DataPackages and JSON Table Schema. | ||
* Be a core component of [Project χ](https://github.com/Hypercubed/Project-Chi) but work independently. | ||
* Maximize compliance with the the DataPackages and JSON Table Schema specifications. | ||
## Install | ||
```sh | ||
npm i -D chi-datapackage | ||
npm install --save chi-datapackage | ||
``` | ||
or | ||
```sh | ||
jspm npm:chi-datapackage | ||
``` | ||
## Basic usage | ||
```js | ||
import {load} from 'chi-datapackage'; | ||
import dp from 'chi-datapackage'; | ||
load('//datapackage/path/or/url').then(datapackage => { | ||
/* so something */ | ||
}); | ||
dp.load('//datapackage/path/or/url') | ||
.then(datapackage => { | ||
/* so something */ | ||
}); | ||
``` | ||
@@ -30,4 +52,4 @@ | ||
const Normalizer = require('chi-datapackagesrc/normalizer'); | ||
const Processor = require('chi-datapackage/src/processor.js'); | ||
const Normalizer = require('chi-datapackage/src/normalizer'); | ||
const Processor = require('chi-datapackage/src/processor'); | ||
const SchemaProcessor = require('chi-datapackage/src/schema'); | ||
@@ -49,7 +71,7 @@ const Loader = require('chi-datapackage/src/loader'); | ||
.datapackage('//datapackage/path/or/url') | ||
.then(datapackage => normalize.datapackage(p)) | ||
.then(datapackage => normalize.resources(p)) | ||
.then(datapackage => load.resources(p)) | ||
.then(datapackage => process.datapackage(p)) | ||
.then(datapackage => { | ||
.then(p => normalize.datapackage(p)) | ||
.then(p => normalize.resources(p)) | ||
.then(p => load.resources(p)) | ||
.then(p => process.datapackage(p)) | ||
.then(p => { | ||
/* so something */ | ||
@@ -56,0 +78,0 @@ }); |
@@ -0,8 +1,12 @@ | ||
const d3time = require('d3-time-format'); | ||
const parseIsoDuration = require('parse-iso-duration'); | ||
const jsonParse = require('./json'); | ||
const TRUE_VALUES = ['yes', 'y', 'true', 't', '1']; | ||
const typeToCast = { | ||
string: { | ||
default: String | ||
/* todo: format, uri, email, binary: */ | ||
/* todo: format, uri, email, binary? */ | ||
}, | ||
@@ -14,10 +18,25 @@ integer: { | ||
default: parseFloat | ||
// todo: format, example currency | ||
// todo: currency? | ||
}, | ||
datetime: { | ||
default: d3time.utcParse('%Y-%m-%dT%H:%M:%SZ'), | ||
fmt: d3time.utcParse, | ||
any: d => new Date(d) | ||
}, | ||
date: { | ||
default: d => new Date(d) | ||
// todo: format, example "yyyy" | ||
default: d3time.utcParse('%Y-%m-%d'), | ||
fmt: d3time.utcParse, | ||
yyyy: d3time.utcParse('%Y'), | ||
any: d => new Date(d) | ||
}, | ||
time: { | ||
default: d3time.utcParse('%H:%M:%S'), | ||
fmt: d3time.utcParse, | ||
any: d => new Date(d) | ||
}, | ||
duration: { | ||
default: parseIsoDuration | ||
}, | ||
boolean: { | ||
default: Boolean | ||
default: d => d === true || TRUE_VALUES.indexOf(String(d).toLowerCase()) !== -1 | ||
}, | ||
@@ -30,6 +49,3 @@ object: { | ||
typeToCast.array = typeToCast.object; | ||
typeToCast.time = typeToCast.date; | ||
typeToCast.datetime = typeToCast.date; | ||
typeToCast.date.any = typeToCast.date.default; | ||
module.exports = typeToCast; |
@@ -19,6 +19,5 @@ 'use strict'; | ||
schema.fields.forEach(field => { | ||
field.format = field.format || 'default'; | ||
if (field.type in this.types) { | ||
const type = this.types[field.type]; | ||
castMap[field.name] = type[field.format] || type.default; | ||
const fn = this.generateCastFn(field); | ||
if (fn) { | ||
castMap[field.name] = fn; | ||
} | ||
@@ -29,2 +28,18 @@ }); | ||
generateCastFn (field) { | ||
if (!field.type || !Object.prototype.hasOwnProperty.call(this.types, field.type)) { | ||
return null; | ||
} | ||
const type = this.types[field.type]; | ||
let format = field.format || 'default'; | ||
let pattern = null; | ||
if (format.indexOf(':') !== -1) { | ||
const s = format.split(':'); | ||
format = s[0]; | ||
pattern = s[1]; | ||
} | ||
const map = type[format] || type.default; | ||
return pattern ? map(pattern) : map; | ||
} | ||
process (resource) { | ||
@@ -31,0 +46,0 @@ let data = resource.data; |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
37844
26
964
93
4
12
+ Addedd3-time-format@^2.0.2
+ Addedparse-iso-duration@^1.0.0
+ Addedd3-time@1.1.0(transitive)
+ Addedd3-time-format@2.3.0(transitive)
+ Addedparse-iso-duration@1.1.0(transitive)