Skip to content

Commit

Permalink
feat: remove cldr dependency due to excessive size
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Sep 11, 2019
1 parent 12103cf commit 890e3c1
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 24 deletions.
29 changes: 14 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,30 @@
},
"dependencies": {
"cartesian": "^1.0.1",
"cldr": "^5.4.1",
"moment": "^2.24.0",
"moment-timezone": "^0.5.26",
"roarr": "^2.14.0"
"roarr": "^2.14.1"
},
"description": "Extracts date from an arbitrary text input.",
"devDependencies": {
"@babel/cli": "^7.5.5",
"@babel/core": "^7.5.5",
"@babel/node": "^7.5.5",
"@babel/cli": "^7.6.0",
"@babel/core": "^7.6.0",
"@babel/node": "^7.6.1",
"@babel/plugin-transform-flow-strip-types": "^7.4.4",
"@babel/preset-env": "^7.5.5",
"@babel/register": "^7.5.5",
"ava": "^2.2.0",
"@babel/preset-env": "^7.6.0",
"@babel/register": "^7.6.0",
"ava": "^2.3.0",
"babel-plugin-istanbul": "^5.2.0",
"coveralls": "^3.0.6",
"eslint": "^6.1.0",
"eslint-config-canonical": "^17.3.4",
"flow-bin": "^0.105.2",
"flow-copy-source": "^2.0.7",
"eslint": "^6.3.0",
"eslint-config-canonical": "^17.7.0",
"flow-bin": "^0.107.0",
"flow-copy-source": "^2.0.8",
"gitdown": "^3.1.1",
"husky": "^3.0.3",
"husky": "^3.0.5",
"nyc": "^14.1.1",
"semantic-release": "^15.13.21",
"sinon": "^7.4.1"
"semantic-release": "^15.13.24",
"sinon": "^7.4.2"
},
"engines": {
"node": ">6"
Expand Down
24 changes: 24 additions & 0 deletions src/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env node

/* eslint-disable import/no-commonjs, import/no-extraneous-dependencies, quote-props */

// Usage: `npm i cldr && node ./src/build && npm uninstall cldr`
// @see https://github.com/papandreou/node-cldr/issues/99

const fs = require('fs');
const path = require('path');
const cldr = require('cldr');

const dayNames = {};

for (const localeId of cldr.localeIds) {
const dictionary = cldr.extractFields(localeId);

dayNames[localeId] = {
'-1': dictionary.day.relative['-1'],
'0': dictionary.day.relative['0'],
'1': dictionary.day.relative['1'],
};
}

fs.writeFileSync(path.resolve(__dirname, 'locale.json'), JSON.stringify(dayNames));
1 change: 1 addition & 0 deletions src/dictionary.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/extractDate.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/* eslint-disable no-continue, no-negated-condition */

import moment from 'moment-timezone';
import cldr from 'cldr';
import dictionary from './dictionary.json';
import createMovingChunks from './createMovingChunks';
import extractRelativeDate from './extractRelativeDate';
import createFormats from './createFormats';
Expand Down Expand Up @@ -37,7 +37,7 @@ export default (input: string, userConfiguration: UserConfigurationType = defaul
...userConfiguration,
};

if (configuration.locale && !cldr.localeIds.includes(configuration.locale)) {
if (configuration.locale && !dictionary[configuration.locale]) {
throw new Error('No translation available for the target locale.');
}

Expand Down
14 changes: 7 additions & 7 deletions src/extractRelativeDate.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
// @flow

import moment from 'moment-timezone';
import cldr from 'cldr';
import dictionary from './dictionary.json';

export default (subject: string, locale: string, timezone: string): ?string => {
if (!cldr.localeIds.includes(locale)) {
const translation = dictionary[locale];

if (!translation) {
throw new Error('No translation available for the target locale.');
}

if (!moment.tz.zone(timezone)) {
throw new Error('Unrecognized timezone.');
}

const dictionary = cldr.extractFields(locale);

const normalizedSubject = subject.toLowerCase();

const now = moment();

if (normalizedSubject === dictionary.day.relative['-1']) {
if (normalizedSubject === translation['-1']) {
return now.subtract(1, 'day').format('YYYY-MM-DD');
}

if (normalizedSubject === dictionary.day.relative['0']) {
if (normalizedSubject === translation['0']) {
return now.format('YYYY-MM-DD');
}

if (normalizedSubject === dictionary.day.relative['1']) {
if (normalizedSubject === translation['1']) {
return now.add(1, 'day').format('YYYY-MM-DD');
}

Expand Down

0 comments on commit 890e3c1

Please sign in to comment.