@louis.jln/extract-date 3.0.1 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. package/package.json +21 -65
  2. package/src/calculateSpecificity.d.ts +2 -0
  3. package/src/calculateSpecificity.js +16 -0
  4. package/src/createFormats.d.ts +13 -0
  5. package/src/createFormats.js +140 -0
  6. package/src/createMovingChunks.d.ts +2 -0
  7. package/src/createMovingChunks.js +12 -0
  8. package/src/days.json +22005 -0
  9. package/src/extractDate.d.ts +3 -0
  10. package/src/extractDate.js +157 -0
  11. package/src/extractRelativeDate.d.ts +2 -0
  12. package/src/extractRelativeDate.js +23 -0
  13. package/src/index.d.ts +2 -0
  14. package/src/index.js +2 -0
  15. package/src/months.json +35738 -0
  16. package/src/normalizeInput.d.ts +2 -0
  17. package/src/normalizeInput.js +17 -0
  18. package/src/resolveLocalizedNames.d.ts +2 -0
  19. package/src/resolveLocalizedNames.js +82 -0
  20. package/src/types.d.ts +20 -0
  21. package/src/types.js +1 -0
  22. package/test/extract-date/calculateSpecificity.test.d.ts +1 -0
  23. package/test/extract-date/calculateSpecificity.test.js +39 -0
  24. package/test/extract-date/createMovingChunks.test.d.ts +1 -0
  25. package/test/extract-date/createMovingChunks.test.js +28 -0
  26. package/test/extract-date/extractDate/configuration.test.d.ts +1 -0
  27. package/test/extract-date/extractDate/configuration.test.js +36 -0
  28. package/test/extract-date/extractDate/edge-cases.test.d.ts +1 -0
  29. package/test/extract-date/extractDate/edge-cases.test.js +23 -0
  30. package/test/extract-date/extractDate/fixtures.test.d.ts +1 -0
  31. package/test/extract-date/extractDate/fixtures.test.js +42 -0
  32. package/test/extract-date/extractDate/general-formats.test.d.ts +1 -0
  33. package/test/extract-date/extractDate/general-formats.test.js +45 -0
  34. package/test/extract-date/extractDate/implied-year.test.d.ts +1 -0
  35. package/test/extract-date/extractDate/implied-year.test.js +105 -0
  36. package/test/extract-date/extractDate/localised.test.d.ts +1 -0
  37. package/test/extract-date/extractDate/localised.test.js +75 -0
  38. package/test/extract-date/extractDate/multiple-dates.test.d.ts +1 -0
  39. package/test/extract-date/extractDate/multiple-dates.test.js +24 -0
  40. package/test/extract-date/extractDate/relative-dates.test.d.ts +1 -0
  41. package/test/extract-date/extractDate/relative-dates.test.js +47 -0
  42. package/test/extract-date/extractRelativeDate.test.d.ts +1 -0
  43. package/test/extract-date/extractRelativeDate.test.js +29 -0
  44. package/test/extract-date/normalizeInput.test.d.ts +1 -0
  45. package/test/extract-date/normalizeInput.test.js +14 -0
  46. package/test/fixtures/dates.json +22574 -0
  47. package/calculateSpecificity.js +0 -23
  48. package/calculateSpecificity.js.flow +0 -21
  49. package/calculateSpecificity.js.map +0 -1
  50. package/createFormats.js +0 -165
  51. package/createFormats.js.flow +0 -373
  52. package/createFormats.js.map +0 -1
  53. package/createMovingChunks.js +0 -20
  54. package/createMovingChunks.js.flow +0 -19
  55. package/createMovingChunks.js.map +0 -1
  56. package/dictionary.json +0 -3792
  57. package/extractDate.js +0 -148
  58. package/extractDate.js.flow +0 -214
  59. package/extractDate.js.map +0 -1
  60. package/extractRelativeDate.js +0 -32
  61. package/extractRelativeDate.js.flow +0 -34
  62. package/extractRelativeDate.js.map +0 -1
  63. package/index.js +0 -11
  64. package/index.js.flow +0 -6
  65. package/index.js.map +0 -1
  66. package/louis.jln-extract-date-3.0.0.tgz +0 -0
  67. package/normalizeInput.js +0 -22
  68. package/normalizeInput.js.flow +0 -26
  69. package/normalizeInput.js.map +0 -1
  70. package/types.js +0 -2
  71. package/types.js.flow +0 -23
  72. package/types.js.map +0 -1
package/package.json CHANGED
@@ -1,19 +1,7 @@
1
1
  {
2
2
  "author": "Gajus Kuizinas <gajus@gajus.com> (http://gajus.com)",
3
- "ava": {
4
- "babel": {
5
- "compileAsTests": [
6
- "test/helpers/**/*"
7
- ]
8
- },
9
- "files": [
10
- "test/extract-date/**/*"
11
- ],
12
- "require": [
13
- "@babel/register"
14
- ]
15
- },
16
3
  "dependencies": {
4
+ "@louis.jln/extract-date": "^3.0.1",
17
5
  "cartesian": "^1.0.1",
18
6
  "date-fns": "^2.9.0",
19
7
  "moment": "^2.24.0",
@@ -22,35 +10,16 @@
22
10
  },
23
11
  "description": "Extracts date from an arbitrary text input.",
24
12
  "devDependencies": {
25
- "@ava/babel": "^1.0.0",
26
- "@babel/cli": "^7.8.4",
27
- "@babel/core": "^7.8.4",
28
- "@babel/node": "^7.8.4",
29
- "@babel/plugin-transform-flow-strip-types": "^7.8.3",
30
- "@babel/preset-env": "^7.8.4",
31
- "@babel/register": "^7.8.3",
32
- "ava": "^3.1.0",
33
- "babel-plugin-istanbul": "^6.0.0",
34
- "coveralls": "^3.0.9",
35
- "esbuild": "^0.24.0",
36
- "eslint": "^6.8.0",
37
- "eslint-config-canonical": "^18.1.0",
38
- "flow-bin": "^0.117.0",
39
- "flow-copy-source": "^2.0.9",
40
- "gitdown": "^3.1.2",
41
- "husky": "^4.2.1",
42
- "nyc": "^15.0.0",
43
- "semantic-release": "^17.0.2",
44
- "sinon": "^8.1.1"
13
+ "@types/node": "^20.0.0",
14
+ "esbuild": "^0.27.3",
15
+ "eslint": "^10.0.2",
16
+ "husky": "^9.1.7",
17
+ "typescript": "^5.9.3",
18
+ "typescript-eslint": "^8.56.1",
19
+ "vitest": "^4.0.18"
45
20
  },
46
21
  "engines": {
47
- "node": ">6"
48
- },
49
- "husky": {
50
- "hooks": {
51
- "pre-commit": "npm run lint && npm run test && npm run build",
52
- "pre-push": "gitdown ./.README/README.md --output-file ./README.md --check"
53
- }
22
+ "node": ">=20"
54
23
  },
55
24
  "keywords": [
56
25
  "date",
@@ -59,39 +28,26 @@
59
28
  "parse"
60
29
  ],
61
30
  "license": "BSD-3-Clause",
62
- "main": "./dist/index.js",
31
+ "main": "./index.js",
63
32
  "name": "@louis.jln/extract-date",
64
- "nyc": {
65
- "include": [
66
- "src/**/*.js"
67
- ],
68
- "instrument": false,
69
- "reporter": [
70
- "text-lcov"
71
- ],
72
- "require": [
73
- "@babel/register"
74
- ],
75
- "sourceMap": false
76
- },
77
33
  "repository": {
78
34
  "type": "git",
79
- "url": "git+https://github.com/gajus/extract-date.git"
35
+ "url": "git+https://github.com/LouisJULIEN/extract-date.git"
80
36
  },
81
37
  "scripts": {
82
- "build": "rm -fr ./dist && NODE_ENV=production babel ./src --out-dir ./dist --copy-files --source-maps && flow-copy-source src dist",
83
- "create-readme": "gitdown ./.README/README.md --output-file ./README.md",
84
- "lint": "eslint ./src ./test && flow",
85
- "test": "NODE_ENV=test ava --verbose --serial",
86
- "bundle": "esbuild dist/index.js --bundle --outfile=bundle/extract-date.js --format=esm",
87
- "npm-publish": "cp package.json dist/package.json; cd dist; npm publish --access public"
88
- },
89
- "version": "3.0.1",
38
+ "build": "rm -fr ./dist && tsc --project tsconfig.json",
39
+ "lint": "eslint ./src ./test",
40
+ "test": "vitest run",
41
+ "typecheck": "tsc --noEmit",
42
+ "npm-publish": "rm -rf build; npm run build; cp package.json dist/package.json; cd dist; npm publish --access public",
43
+ "prepare": "husky"
44
+ },
45
+ "version": "4.0.0",
90
46
  "directories": {
91
47
  "test": "test"
92
48
  },
93
49
  "bugs": {
94
- "url": "https://github.com/gajus/extract-date/issues"
50
+ "url": "https://github.com/LouisJULIEN/extract-date/issues"
95
51
  },
96
- "homepage": "https://github.com/gajus/extract-date#readme"
52
+ "homepage": "https://github.com/LouisJULIEN/extract-date"
97
53
  }
@@ -0,0 +1,2 @@
1
+ declare const _default: (dateFnsFormat: string) => number;
2
+ export default _default;
@@ -0,0 +1,16 @@
1
+ export default (dateFnsFormat) => {
2
+ let specificity = 0;
3
+ if (dateFnsFormat.includes('yyyy')) {
4
+ specificity += 40;
5
+ }
6
+ else if (dateFnsFormat.includes('yy')) {
7
+ specificity += 20;
8
+ }
9
+ if (dateFnsFormat.includes('M')) {
10
+ specificity += 20;
11
+ }
12
+ if (/d/.test(dateFnsFormat)) {
13
+ specificity += 20;
14
+ }
15
+ return specificity + dateFnsFormat.length;
16
+ };
@@ -0,0 +1,13 @@
1
+ type FormatEntry = {
2
+ dateFnsFormat: string;
3
+ direction?: string;
4
+ test?: boolean;
5
+ };
6
+ type Format = FormatEntry & {
7
+ localised: boolean;
8
+ specificity: number;
9
+ wordCount: number;
10
+ yearIsExplicit: boolean;
11
+ };
12
+ declare const _default: () => Format[];
13
+ export default _default;
@@ -0,0 +1,140 @@
1
+ import cartesian from 'cartesian';
2
+ import calculateSpecificity from '@/calculateSpecificity';
3
+ const formatCombinaison = (combination) => combination.join(' ').replace(' ,', ',').replace(/ +/, ' ').replace(/ $/, '');
4
+ export default () => {
5
+ const yearFirstDashSeparator = [
6
+ { dateFnsFormat: 'yyyy-MM-dd' },
7
+ { dateFnsFormat: 'yyyy-M-d' },
8
+ ];
9
+ const yearFirstSlashSeparator = [
10
+ { dateFnsFormat: 'yyyy/MM/dd' },
11
+ { dateFnsFormat: 'yyyy/M/d' },
12
+ ];
13
+ const yearFirstDotSeparator = [
14
+ { dateFnsFormat: 'yyyy.MM.dd', direction: 'YMD' },
15
+ { dateFnsFormat: 'yyyy.M.d', direction: 'YMD' },
16
+ { dateFnsFormat: 'yyyy.dd.MM', direction: 'YDM' },
17
+ { dateFnsFormat: 'yyyy.d.M', direction: 'YDM' },
18
+ ];
19
+ const yearLastDashSeparator = [
20
+ { dateFnsFormat: 'dd-MM-yyyy', direction: 'DMY' },
21
+ { dateFnsFormat: 'd-M-yyyy', direction: 'DMY' },
22
+ { dateFnsFormat: 'MM-dd-yyyy', direction: 'MDY' },
23
+ { dateFnsFormat: 'M-d-yyyy', direction: 'MDY' },
24
+ ];
25
+ const yearLastDotSeparator = [
26
+ { dateFnsFormat: 'dd.MM.yyyy', direction: 'DMY' },
27
+ { dateFnsFormat: 'd.M.yyyy', direction: 'DMY' },
28
+ { dateFnsFormat: 'MM.dd.yyyy', direction: 'MDY' },
29
+ { dateFnsFormat: 'M.d.yyyy', direction: 'MDY' },
30
+ { dateFnsFormat: 'dd.MM.yy', direction: 'DMY' },
31
+ { dateFnsFormat: 'd.M.yy', direction: 'DMY' },
32
+ ];
33
+ const yearLastSlashSeparator = [
34
+ { dateFnsFormat: 'dd/MM/yyyy', direction: 'DMY' },
35
+ { dateFnsFormat: 'd/M/yyyy', direction: 'DMY' },
36
+ { dateFnsFormat: 'MM/dd/yyyy', direction: 'MDY' },
37
+ { dateFnsFormat: 'M/d/yyyy', direction: 'MDY' },
38
+ { dateFnsFormat: 'MM/dd/yy', direction: 'MDY' },
39
+ { dateFnsFormat: 'dd/MM/yy', direction: 'DMY' },
40
+ { dateFnsFormat: 'd/M/yy', direction: 'DMY' },
41
+ { dateFnsFormat: 'M/d/yy', direction: 'MDY' },
42
+ ];
43
+ const localised = [
44
+ ...cartesian([
45
+ ['MMMM'],
46
+ ['d', 'do'],
47
+ [',', ''],
48
+ ['yyyy'],
49
+ [',', ''],
50
+ ]).map((combination) => ({ dateFnsFormat: formatCombinaison(combination) })),
51
+ ...cartesian([
52
+ ['do', 'd'],
53
+ ['MMMM', 'MMM'],
54
+ ['yyyy'],
55
+ ]).map((combination) => ({ dateFnsFormat: formatCombinaison(combination) })),
56
+ ...cartesian([
57
+ ['MMMM', 'MMM'],
58
+ ['yyyy'],
59
+ ['do', 'd'],
60
+ ]).map((combination) => ({ dateFnsFormat: formatCombinaison(combination) })),
61
+ { dateFnsFormat: 'MMMM yyyy EEE do' },
62
+ { dateFnsFormat: 'MMMM yyyy EEE d' },
63
+ ];
64
+ const impliedYearLocalised = [
65
+ ...cartesian([
66
+ ['EEEE', 'EEE'],
67
+ ['', ','],
68
+ ['MMMM', 'MMM'],
69
+ ['dd', 'do', 'd'],
70
+ ]).map((combination) => ({ dateFnsFormat: formatCombinaison(combination) })),
71
+ ...cartesian([
72
+ ['EEEE', 'EEE'],
73
+ ['', ','],
74
+ ['dd', 'do', 'd'],
75
+ ['MMMM', 'MMM'],
76
+ ]).map((combination) => ({ dateFnsFormat: formatCombinaison(combination) })),
77
+ ...cartesian([
78
+ ['MMMM', 'MMM'],
79
+ ['dd', 'do', 'd'],
80
+ [',', ''],
81
+ ]).map((combination) => ({ dateFnsFormat: formatCombinaison(combination) })),
82
+ ...cartesian([
83
+ ['dd', 'do', 'd'],
84
+ ['MMMM', 'MMM'],
85
+ [',', ''],
86
+ ]).map((combination) => ({ dateFnsFormat: formatCombinaison(combination) })),
87
+ ];
88
+ const impliedYear = [
89
+ ...cartesian([
90
+ ['dd', 'd'],
91
+ ['/', '-', '.'],
92
+ ['MM', 'M'],
93
+ [',', ''],
94
+ ]).map((combination) => ({
95
+ dateFnsFormat: combination.join('').replace(' ,', ',').replace(/ +/, ' '),
96
+ direction: 'DM',
97
+ })),
98
+ ...cartesian([
99
+ ['MM', 'M'],
100
+ ['/', '-', '.'],
101
+ ['dd', 'd'],
102
+ [',', ''],
103
+ ]).map((combination) => ({
104
+ dateFnsFormat: combination.join('').replace(' ,', ',').replace(/ +/, ' '),
105
+ direction: 'MD',
106
+ })),
107
+ ];
108
+ const relative = [
109
+ { dateFnsFormat: 'R', test: false },
110
+ ];
111
+ return [
112
+ { dateFnsFormat: 'yyyyMMdd' },
113
+ ...yearFirstDashSeparator,
114
+ ...yearFirstDotSeparator,
115
+ ...yearFirstSlashSeparator,
116
+ ...yearLastDashSeparator,
117
+ ...yearLastDotSeparator,
118
+ ...yearLastSlashSeparator,
119
+ ...localised,
120
+ ...impliedYearLocalised,
121
+ ...impliedYear,
122
+ ...relative,
123
+ ]
124
+ .map((format) => ({
125
+ localised: /eee|mmm/i.test(format.dateFnsFormat),
126
+ specificity: calculateSpecificity(format.dateFnsFormat),
127
+ wordCount: format.dateFnsFormat.replace(/[^ ]/g, '').length + 1,
128
+ yearIsExplicit: format.dateFnsFormat.includes('yyyy'),
129
+ ...format,
130
+ }))
131
+ .sort((a, b) => {
132
+ if (a.wordCount !== b.wordCount) {
133
+ return b.wordCount - a.wordCount;
134
+ }
135
+ if (b.specificity === a.specificity) {
136
+ return a.dateFnsFormat.localeCompare(b.dateFnsFormat);
137
+ }
138
+ return b.specificity - a.specificity;
139
+ });
140
+ };
@@ -0,0 +1,2 @@
1
+ declare const _default: (haystack: readonly string[], sliceLength: number) => readonly (readonly string[])[];
2
+ export default _default;
@@ -0,0 +1,12 @@
1
+ export default (haystack, sliceLength) => {
2
+ const slices = [];
3
+ let index = 0;
4
+ while (index < haystack.length) {
5
+ const result = haystack.slice(index, index + sliceLength);
6
+ if (result.length === sliceLength) {
7
+ slices.push(result);
8
+ }
9
+ index++;
10
+ }
11
+ return slices;
12
+ };