read-data-file 2.0.1 → 2.0.4

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 (3) hide show
  1. package/package.json +16 -16
  2. package/rdf.mjs +26 -10
  3. package/test/usage.mjs +5 -3
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  { "name": "read-data-file",
2
- "version": "2.0.1",
2
+ "version": "2.0.4",
3
3
  "description": "Read data/config files in various formats (parsers list is configurable).",
4
4
  "keywords": [
5
5
  "read data file",
@@ -25,27 +25,27 @@
25
25
  },
26
26
 
27
27
  "dependencies": {
28
- "ceson": "^0.1.3",
29
- "esmod-pmb": "^0.1.11",
30
- "getown": "^0.1.0",
31
- "if-fun": "^1.0.0",
32
- "ini": "^1.3.5",
33
- "is-string": "^1.0.4",
34
- "js-yaml": "^3.13.1",
28
+ "ceson": "^0.1.5",
29
+ "esmod-pmb": "^0.1.13",
30
+ "getown": "^1.0.0",
31
+ "if-fun": "^1.0.1",
32
+ "ini": "^2.0.0",
33
+ "is-string": "^1.0.7",
35
34
  "json-parse-pmb": "^1.0.0",
36
- "json5": "^2.1.1",
35
+ "json5": "^2.2.0",
37
36
  "map-assoc-core": "^0.1.3",
38
- "merge-options": "^2.0.0",
37
+ "merge-options": "^3.0.4",
39
38
  "nofs": "^0.12.2",
40
- "strip-bom": "^3.0.0",
41
- "toml": "^2.3.6"
39
+ "safeload-yaml-pmb": "^4.210519.0",
40
+ "strip-bom": "^4.0.0",
41
+ "toml": "^3.0.0"
42
42
  },
43
43
  "devDependencies": {
44
44
  "absdir": "^1.0.6",
45
- "equal-pmb": "^0.1.20",
46
- "nodemjs": "^0.1.8",
47
- "p-fatal": "^0.1.2",
48
- "usnam-pmb": "^0.2.4"
45
+ "equal-pmb": "^0.1.24",
46
+ "nodemjs": "^0.1.9",
47
+ "p-fatal": "^0.1.3",
48
+ "usnam-pmb": "^0.2.5"
49
49
  },
50
50
 
51
51
 
package/rdf.mjs CHANGED
@@ -15,7 +15,7 @@ import iniLib from 'ini';
15
15
  import json5Lib from 'json5';
16
16
  import jsonParser from 'json-parse-pmb';
17
17
  import tomlLib from 'toml';
18
- import yamlLib from 'js-yaml';
18
+ import yamlSafeLoad from 'safeload-yaml-pmb';
19
19
 
20
20
 
21
21
  function makeReaderFunc() {
@@ -43,7 +43,7 @@ const defaultImpl = {
43
43
  json: jsonParser,
44
44
  json5(data) { return json5Lib.parse(data); },
45
45
  toml(data) { return tomlLib.parse(data); },
46
- yaml(data) { return yamlLib.safeLoad(data); },
46
+ yaml(data) { return yamlSafeLoad(data); },
47
47
  yml: 'yaml',
48
48
  },
49
49
 
@@ -62,15 +62,28 @@ const defaultImpl = {
62
62
  return custom;
63
63
  },
64
64
 
65
- findBestReaderAndParser(path) {
66
- const { readersByFext, parsersByFext } = this;
65
+ parseFilenameDotParts(path, opt) {
66
+ const fmtOvr = opt.format;
67
+ if (fmtOvr) { return fmtOvr.split(/\./).reverse(); }
68
+ if (fmtOvr === undefined) {
69
+ const m = /^<fmt=([\w\.]+)>(?=[\S\s])/.exec(path);
70
+ if (m) {
71
+ const dp = m[1].split(/\./).reverse();
72
+ dp.realPath = path.slice(m[0].length);
73
+ return dp;
74
+ }
75
+ }
67
76
  const basename = pathLib.parse(path).base;
68
- const dotParts = basename.split(/\./).slice(1).reverse();
77
+ return basename.split(/\./).slice(1).reverse();
78
+ },
79
+
80
+ findBestReaderAndParser(pathDotParts) {
81
+ const { readersByFext, parsersByFext } = this;
69
82
  // ^-- e.g. cats.json.rot13.gz -> [gz, rot13, json]
70
83
  let fext;
71
84
  let reader;
72
85
  let parser;
73
- dotParts.forEach(function lookup(dotPart) {
86
+ pathDotParts.forEach(function lookup(dotPart) {
74
87
  fext = dotPart + (fext ? '.' + fext : '');
75
88
  if (reader === undefined) { reader = dlookup(readersByFext, fext); }
76
89
  if (parser === undefined) { parser = dlookup(parsersByFext, fext); }
@@ -93,11 +106,14 @@ const defaultImpl = {
93
106
  throw err;
94
107
  },
95
108
 
96
- async readFile(path) {
97
- const rop = this.findBestReaderAndParser(path);
98
- if (!rop) { return this.unsupportedFext(path); }
109
+ async readFile(path, origOpt) {
110
+ const opt = { ...origOpt };
111
+ const dotParts = this.parseFilenameDotParts(path, opt);
112
+ const realPath = (dotParts.realPath || path);
113
+ const rop = this.findBestReaderAndParser(dotParts);
114
+ if (!rop) { return this.unsupportedFext(realPath); }
99
115
  const { reader, parser } = rop;
100
- const content = await ifFun(reader, this.defaultReader)(path);
116
+ const content = await ifFun(reader, this.defaultReader)(realPath);
101
117
  const data = await ifFun(parser, identity)(content);
102
118
  return data;
103
119
  },
package/test/usage.mjs CHANGED
@@ -22,9 +22,11 @@ async function shouldReject(pr) {
22
22
 
23
23
 
24
24
  // ¦mjsUsageDemo¦+
25
+ function toNativeObjects(x) { return JSON.parse(JSON.stringify(x)); }
26
+
25
27
  async function verifyOneFixture(filename, customRdf) {
26
28
  const data = await (customRdf || readDataFile)(addTestDirPath(filename));
27
- equal(data, nativeCats);
29
+ equal.named(filename, () => equal(toNativeObjects(data), nativeCats));
28
30
  console.log('+OK correct data for', filename);
29
31
  }
30
32
 
@@ -39,8 +41,8 @@ async function verifyAllFixtures() {
39
41
  await verifyOneFixture('cats.yml');
40
42
 
41
43
  const noIni = readDataFile.cfg({ parsersByFext: { ini: false } });
42
- await shouldReject(verifyOneFixture('cats.ini', noIni)).then(rej => {
43
- equal(rej.name, 'ReadDataFileUnsupportedFext');
44
+ await shouldReject(verifyOneFixture('cats.ini', noIni)).then((rej) => {
45
+ equal.named('noIni', () => equal(rej.name, 'ReadDataFileUnsupportedFext'));
44
46
  console.log('+OK ini parser disabled');
45
47
  });
46
48
  }