Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ddf-time-utils

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ddf-time-utils - npm Package Compare versions

Comparing version 0.1.1 to 0.1.2

66

index.js
'use strict';
const _ = require('lodash');
const moment = require('moment');

@@ -26,7 +27,22 @@ const TWO_DIGITS_MIN_NUMBER = 10;

const TIME_TYPE_PATTERNS = {
[YEAR_TYPE]: YEAR_PATTERN,
[QUARTER_TYPE]: QUARTER_PATTERN,
[MONTH_TYPE]: MONTH_PATTERN,
[WEEK_TYPE]: WEEK_PATTERN,
[DATE_TYPE]: DATE_PATTERN
[YEAR_TYPE]: {
regularExp: YEAR_PATTERN,
patternDate: 'YYYY'
},
[QUARTER_TYPE]: {
regularExp: QUARTER_PATTERN,
patternDate: 'YYYY Q'
},
[MONTH_TYPE]: {
regularExp: MONTH_PATTERN,
patternDate: 'YYYYMM'
},
[WEEK_TYPE]: {
regularExp: WEEK_PATTERN,
patternDate: 'YYYY W'
},
[DATE_TYPE]: {
regularExp: DATE_PATTERN,
patternDate: 'YYYYMMDD'
}
};

@@ -36,4 +52,4 @@

function parse(option) {
const matchStart = TIME_TYPE_PATTERNS[type].exec(option[0]);
const matchEnd = TIME_TYPE_PATTERNS[type].exec(option[1]);
const matchStart = TIME_TYPE_PATTERNS[type].regularExp.exec(option[0]);
const matchEnd = TIME_TYPE_PATTERNS[type].regularExp.exec(option[1]);

@@ -123,3 +139,3 @@ return {

.map(queryDetail => Object.getOwnPropertySymbols(TIME_TYPE_PATTERNS)
.filter(type => TIME_TYPE_PATTERNS[type].test(queryDetail)))
.filter(type => TIME_TYPE_PATTERNS[type].regularExp.test(queryDetail)))
));

@@ -158,2 +174,32 @@

/*
*
* Structure parseTimeResult, Object
* @attribute time, Int UTC milliseconds
* @attribute type, String One of available values "YEAR_TYPE"|"QUARTER_TYPE"|"MONTH_TYPE"|"WEEK_TYPE"|"DATE_TYPE"
*
* @param timeString, String
* @return parseTimeResult
*/
function parseTime(timeString) {
const type = detectTimeType(timeString);
if (!type) {
return null;
}
const patternDate = TIME_TYPE_PATTERNS[type].patternDate;
const timeMoment = moment.utc(timeString, patternDate);
const timeType = Symbol.keyFor(type);
if (!timeMoment.isValid()) {
return null;
}
return {
type: timeType,
time: timeMoment.valueOf()
};
}
exports.YEAR_TYPE = YEAR_TYPE;

@@ -166,1 +212,5 @@ exports.QUARTER_TYPE = QUARTER_TYPE;

exports.detectTimeType = detectTimeType;
exports.parseTime = parseTime;
exports.TIME_TYPES = Object.getOwnPropertySymbols(TIME_TYPE_PATTERNS);
exports.TIME_TYPES_AS_STRINGS = exports.TIME_TYPES.map(Symbol.keyFor);

3

package.json
{
"name": "ddf-time-utils",
"version": "0.1.1",
"version": "0.1.2",
"description": "Gapminder DDF time utils",

@@ -27,2 +27,3 @@ "main": "index.js",

"dependencies": {
"moment": "2.14.1",
"lodash": "4.7.0"

@@ -29,0 +30,0 @@ },

@@ -1,6 +0,125 @@

# Gapminder DDF Time Tools
> This is the gapminder DDF time tools library
# Gapminder DDF Time Utils
### How to install
## Install
`npm i ddf-time-tool`
`npm i ddf-time-utils`
## Description
`ddf-time-utils` is created for DDF time format checking and manipulation.
## Exported constants
* YEAR_TYPE
* QUARTER_TYPE
* MONTH_TYPE
* WEEK_TYPE
* DATE_TYPE
These constants describes five different kinds of time supported by DDF.
## Exported functions
* getTimeRange(timeRangeQuery): timeRange
* detectTimeType(time): timeType
More info - in `Examples` section.
## Examples
### Time type detection
```
const ddfTimeUtils = require('ddf-time-utils');
const resultYear = ddfTimeUtils.detectTimeType('2000');
const resultQuarter = ddfTimeUtils.detectTimeType('2000q1');
const resultMonth = ddfTimeUtils.detectTimeType('200001');
const resultWeek = ddfTimeUtils.detectTimeType('2000w01');
const resultDate = ddfTimeUtils.detectTimeType('2000102');
console.log(resultYear === ddfTimeUtils.YEAR_TYPE);
console.log(resultQuarter === ddfTimeUtils.QUARTER_TYPE);
console.log(resultMonth === ddfTimeUtils.MONTH_TYPE);
console.log(resultWeek === ddfTimeUtils.WEEK_TYPE);
console.log(resultDate === ddfTimeUtils.DATE_TYPE);
```
### Time type detection (incorrect time)
```
const ddfTimeUtils = require('ddf-time-utils');
const result = ddfTimeUtils.detectTimeType('200x0102');
console.log(result === null);
```
### Time range detection
```
const ddfTimeUtils = require('ddf-time-utils');
const simpleResult = ddfTimeUtils.getTimeRange(['1990']);
const multipleResult = ddfTimeUtils.getTimeRange(['1800', ['2013', '2015'], '2017', '980']);
console.log(simpleResult);
console.log(multipleResult);
```
Result:
```
[ '1990' ]
[ '1800', '2013', '2014', '2015', '2017', '980' ]
```
```
const ddfTimeUtils = require('ddf-time-utils');
const result = ddfTimeUtils.getTimeRange([['1990q1', '1992q2'], '1993q4']);
console.log(result);
```
Result:
```
[ '1990q1',
'1990q2',
'1990q3',
'1990q4',
'1991q1',
'1991q2',
'1991q3',
'1991q4',
'1992q1',
'1992q2',
'1993q4' ]
```
### Time range detection (incorrect way)
```
const ddfTimeUtils = require('ddf-time-utils');
// time range detection is possible only for same kind of time
// we have `year` and `quarter` types in same query
const result = ddfTimeUtils.getTimeRange(['2015', '2011q1']);
console.log(result);
```
Result:
```
null
```
```
const ddfTimeUtils = require('ddf-time-utils');
// unexpected time
const result = ddfTimeUtils.getTimeRange(['foo']);
console.log(result);
```
Result:
```
null
```

@@ -13,7 +13,7 @@ 'use strict';

describe('DDF time utils', () => {
describe('when correct sets of data (in fixtures/good-cases)', () => {
describe('when correct sets of time ranges (in fixtures/good-cases)', () => {
Object.getOwnPropertySymbols(goodCasesFixtures).forEach(timeType => {
describe(`for '${Symbol.keyFor(timeType)}' type`, () => {
it(`result for simple
query '${JSON.stringify(goodCasesFixtures[timeType].simple)}' should be expected`, () => {
it(`should time range for simple
query '${JSON.stringify(goodCasesFixtures[timeType].simple)}' be expected`, () => {
const simpleResult = ddfTimeUtils.getTimeRange(goodCasesFixtures[timeType].simple);

@@ -24,4 +24,4 @@

it(`result for multiple
query '${JSON.stringify(goodCasesFixtures[timeType].multiple.query)}' should be expected`, () => {
it(`should time range for multiple
query '${JSON.stringify(goodCasesFixtures[timeType].multiple.query)}' be expected`, () => {
const multipleResult = ddfTimeUtils.getTimeRange(goodCasesFixtures[timeType].multiple.query);

@@ -35,3 +35,3 @@

describe('when incorrect query', () => {
describe('when incorrect time ranges query', () => {
Object.getOwnPropertySymbols(incorrectQueries.queries).forEach(queryType => {

@@ -41,3 +41,3 @@ describe(`and type is '${Symbol.keyFor(queryType)}'`, () => {

it(`error '${errorDescription}' should be thrown`, () => {
it(`should error '${errorDescription}' be thrown`, () => {
const result = ddfTimeUtils.getTimeRange(incorrectQueries.queries[queryType]);

@@ -50,2 +50,149 @@

});
describe('when detect time type', () => {
it('should year detection be correct', () => {
const result = ddfTimeUtils.detectTimeType('2000');
expect(result).to.equal(ddfTimeUtils.YEAR_TYPE);
});
it('should quarter detection be correct', () => {
const result = ddfTimeUtils.detectTimeType('2000q1');
expect(result).to.equal(ddfTimeUtils.QUARTER_TYPE);
});
it('should month detection be correct', () => {
const result = ddfTimeUtils.detectTimeType('200001');
expect(result).to.equal(ddfTimeUtils.MONTH_TYPE);
});
it('should week detection be correct', () => {
const result = ddfTimeUtils.detectTimeType('2000w01');
expect(result).to.equal(ddfTimeUtils.WEEK_TYPE);
});
it('should date detection be correct', () => {
const result = ddfTimeUtils.detectTimeType('2000102');
expect(result).to.equal(ddfTimeUtils.DATE_TYPE);
});
it('should incorrect time detection be null', () => {
const result = ddfTimeUtils.detectTimeType('200x0102');
expect(result).to.equal(null);
});
});
describe('when parse time string', () => {
describe('in Year Type Format', () => {
it('should correctly detect year-time string', () => {
const timeExample = '1991';
const timeResult = 662688000000;
const result = ddfTimeUtils.parseTime(timeExample);
expect(result.type).to.equal(Symbol.keyFor(ddfTimeUtils.YEAR_TYPE));
expect(result.time).to.equal(timeResult);
});
});
describe('in Quarter Type Format', () => {
it('should correctly detect quarter-time string for correct value', () => {
const timeExample = '1990q1';
const timeResult = 631152000000;
const result = ddfTimeUtils.parseTime(timeExample);
expect(result.type).to.equal(Symbol.keyFor(ddfTimeUtils.QUARTER_TYPE));
expect(result.time).to.equal(timeResult);
});
it('should return null for incorrect time-string', () => {
const timeExample = '1990q5';
const result = ddfTimeUtils.parseTime(timeExample);
expect(result).to.be.null;
});
});
describe('in Month Type Format', () => {
it('should correctly detect month-time string when month contain two chars', () => {
const timeExample = '198311';
const timeResult = 436492800000;
const result = ddfTimeUtils.parseTime(timeExample);
expect(result.type).to.equal(Symbol.keyFor(ddfTimeUtils.MONTH_TYPE));
expect(result.time).to.equal(timeResult);
});
it('should correctly detect month-time string when month contain one char', () => {
const timeExample = '19832';
const timeResult = 412905600000;
const result = ddfTimeUtils.parseTime(timeExample);
expect(result.type).to.equal(Symbol.keyFor(ddfTimeUtils.MONTH_TYPE));
expect(result.time).to.equal(timeResult);
});
it('should correctly detect month-time string when month contain two chars with lead zero', () => {
const timeExample = '198302';
const timeResult = 412905600000;
const result = ddfTimeUtils.parseTime(timeExample);
expect(result.type).to.equal(Symbol.keyFor(ddfTimeUtils.MONTH_TYPE));
expect(result.time).to.equal(timeResult);
});
it('should return null for incorrect time-string', () => {
const timeExample = '198344';
const result = ddfTimeUtils.parseTime(timeExample);
expect(result).to.be.null;
});
});
describe('in Date Type Format', () => {
it('should correctly detect date-time string', () => {
const timeExample = '20101125';
const timeResult = 1290643200000;
const result = ddfTimeUtils.parseTime(timeExample);
expect(result.type).to.equal(Symbol.keyFor(ddfTimeUtils.DATE_TYPE));
expect(result.time).to.equal(timeResult);
});
it('should correctly detect date-time string when day contain one char', () => {
const timeExample = '2010125';
const timeResult = 1291507200000;
const result = ddfTimeUtils.parseTime(timeExample);
expect(result.type).to.equal(Symbol.keyFor(ddfTimeUtils.DATE_TYPE));
expect(result.time).to.equal(timeResult);
});
it('should return null for incorrect date-time string', () => {
const timeExample = '2010148';
const result = ddfTimeUtils.parseTime(timeExample);
expect(result).to.be.null;
});
});
describe('in Week Type Format', () => {
it('should correctly detect week-time string', () => {
const timeExample = '1825w2';
const timeResult = -4574966400000;
const result = ddfTimeUtils.parseTime(timeExample);
expect(result.type).to.equal(Symbol.keyFor(ddfTimeUtils.WEEK_TYPE));
expect(result.time).to.equal(timeResult);
});
it('should correctly detect date-time string when day contain one char', () => {
const timeExample = '1825w30';
const timeResult = -4558032000000;
const result = ddfTimeUtils.parseTime(timeExample);
expect(result.type).to.equal(Symbol.keyFor(ddfTimeUtils.WEEK_TYPE));
expect(result.time).to.equal(timeResult);
});
it('should return null for incorrect date-time string', () => {
const timeExample = '2000w53';
const result = ddfTimeUtils.parseTime(timeExample);
expect(result).to.be.null;
});
});
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc