@jdeighan/coffee-utils 11.0.9 → 11.0.10

Sign up to get free protection for your applications and to get access to all the features.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@jdeighan/coffee-utils",
3
3
  "type": "module",
4
- "version": "11.0.9",
4
+ "version": "11.0.10",
5
5
  "description": "A set of utility functions for CoffeeScript",
6
6
  "main": "coffee_utils.js",
7
7
  "exports": {
@@ -58,6 +58,6 @@
58
58
  "svelte": "^3.51.0"
59
59
  },
60
60
  "devDependencies": {
61
- "@jdeighan/unit-tester": "^2.0.31"
61
+ "@jdeighan/unit-tester": "^2.0.32"
62
62
  }
63
63
  }
package/src/fs.coffee CHANGED
@@ -5,8 +5,7 @@ import urllib from 'url'
5
5
  import fs from 'fs'
6
6
  import NReadLines from 'n-readlines'
7
7
 
8
- import {assert, croak} from '@jdeighan/exceptions'
9
- import {LOG} from '@jdeighan/exceptions/log'
8
+ import {assert, croak, LOG, fromTAML} from '@jdeighan/exceptions'
10
9
  import {
11
10
  undef, pass, defined, rtrim, isEmpty, nonEmpty,
12
11
  isString, isArray, isHash, isRegExp, isFunction, OL,
@@ -411,3 +410,11 @@ export backup = (file, from, to, report=false) ->
411
410
  LOG "MISSING #{src}"
412
411
  else
413
412
  fs.copyFileSync(src, dest)
413
+
414
+ # ---------------------------------------------------------------------------
415
+ # slurpTAML - read TAML from a file
416
+
417
+ export slurpTAML = (filepath) ->
418
+
419
+ contents = slurp(filepath)
420
+ return fromTAML(contents)
package/src/fs.js CHANGED
@@ -12,13 +12,11 @@ import NReadLines from 'n-readlines';
12
12
 
13
13
  import {
14
14
  assert,
15
- croak
15
+ croak,
16
+ LOG,
17
+ fromTAML
16
18
  } from '@jdeighan/exceptions';
17
19
 
18
- import {
19
- LOG
20
- } from '@jdeighan/exceptions/log';
21
-
22
20
  import {
23
21
  undef,
24
22
  pass,
@@ -487,3 +485,11 @@ export var backup = function(file, from, to, report = false) {
487
485
  return fs.copyFileSync(src, dest);
488
486
  }
489
487
  };
488
+
489
+ // ---------------------------------------------------------------------------
490
+ // slurpTAML - read TAML from a file
491
+ export var slurpTAML = function(filepath) {
492
+ var contents;
493
+ contents = slurp(filepath);
494
+ return fromTAML(contents);
495
+ };
package/src/html.coffee CHANGED
@@ -203,14 +203,12 @@ export tag2str = (hToken, type='begin') ->
203
203
  export elem = (tagName, hAttr=undef, text=undef, oneIndent="\t") ->
204
204
 
205
205
  if isEmpty(text)
206
- return undef
207
- hToken = {
208
- tagName
209
- hAttr
210
- text
211
- }
212
- return toBlock([
213
- tag2str(hToken, 'begin')
214
- indented(text, 1, oneIndent)
215
- tag2str(hToken, 'end')
216
- ])
206
+ hToken = {tagName, hAttr}
207
+ return tag2str(hToken, 'begin') + tag2str(hToken, 'end')
208
+ else
209
+ hToken = {tagName, hAttr, text}
210
+ return toBlock([
211
+ tag2str(hToken, 'begin')
212
+ indented(text, 1, oneIndent)
213
+ tag2str(hToken, 'end')
214
+ ])
package/src/html.js CHANGED
@@ -210,8 +210,10 @@ export var tag2str = function(hToken, type = 'begin') {
210
210
  export var elem = function(tagName, hAttr = undef, text = undef, oneIndent = "\t") {
211
211
  var hToken;
212
212
  if (isEmpty(text)) {
213
- return undef;
213
+ hToken = {tagName, hAttr};
214
+ return tag2str(hToken, 'begin') + tag2str(hToken, 'end');
215
+ } else {
216
+ hToken = {tagName, hAttr, text};
217
+ return toBlock([tag2str(hToken, 'begin'), indented(text, 1, oneIndent), tag2str(hToken, 'end')]);
214
218
  }
215
- hToken = {tagName, hAttr, text};
216
- return toBlock([tag2str(hToken, 'begin'), indented(text, 1, oneIndent), tag2str(hToken, 'end')]);
217
219
  };
package/src/taml.coffee CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  import yaml from 'js-yaml'
4
4
 
5
- import {assert, croak} from '@jdeighan/exceptions'
5
+ import {assert, croak, isTAML, fromTAML, toTAML} from '@jdeighan/exceptions'
6
6
  import {
7
7
  undef, defined, notdefined, OL, chomp, escapeStr,
8
8
  isString, isObject, isEmpty,
@@ -11,84 +11,6 @@ import {splitLine} from '@jdeighan/coffee-utils/indent'
11
11
  import {
12
12
  firstLine, toArray, toBlock,
13
13
  } from '@jdeighan/coffee-utils/block'
14
- import {slurp} from '@jdeighan/coffee-utils/fs'
14
+ import {slurpTAML} from '@jdeighan/coffee-utils/fs'
15
15
 
16
- # ---------------------------------------------------------------------------
17
- # isTAML - is the string valid TAML?
18
-
19
- export isTAML = (text) ->
20
-
21
- return isString(text) && (firstLine(text).indexOf('---') == 0)
22
-
23
- # ---------------------------------------------------------------------------
24
- # taml - convert valid TAML string to a JavaScript value
25
-
26
- export fromTAML = (text) ->
27
-
28
- if notdefined(text)
29
- return undef
30
- assert isTAML(text), "string #{OL(text)} isn't TAML"
31
-
32
- lLines = for line in toArray(text)
33
- [level, str] = splitLine(line)
34
- prefix = ' '.repeat(level)
35
- if lMatches = line.match(///^
36
- ([A-Za-z_][A-Za-z0-9_]*) # the key
37
- \s*
38
- :
39
- \s*
40
- (.*)
41
- $///)
42
- [_, key, text] = lMatches
43
- if isEmpty(text) || text.match(/\d+(?:\.\d*)$/)
44
- prefix + str
45
- else
46
- prefix + key + ':' + ' ' + squote(text)
47
- else
48
- prefix + str
49
-
50
- return yaml.load(toBlock(lLines), {skipInvalid: true})
51
-
52
- # ---------------------------------------------------------------------------
53
- # --- a replacer is (key, value) -> newvalue
54
-
55
- myReplacer = (name, value) ->
56
-
57
- if isString(value)
58
- return escapeStr(value)
59
- else if isObject(value, ['tamlReplacer'])
60
- return value.tamlReplacer()
61
- else
62
- return value
63
-
64
- # ---------------------------------------------------------------------------
65
-
66
- export toTAML = (obj, hOptions={}) ->
67
-
68
- {useTabs, sortKeys, escape, replacer} = hOptions
69
- if notdefined(replacer)
70
- replacer = myReplacer
71
- str = yaml.dump(obj, {
72
- skipInvalid: true
73
- indent: 3
74
- sortKeys: !!sortKeys
75
- lineWidth: -1
76
- replacer
77
- })
78
- if useTabs
79
- str = str.replace(/ /g, "\t")
80
- return "---\n" + chomp(str)
81
-
82
- # ---------------------------------------------------------------------------
83
-
84
- squote = (text) ->
85
-
86
- return "'" + text.replace(/'/g, "''") + "'"
87
-
88
- # ---------------------------------------------------------------------------
89
- # slurpTAML - read TAML from a file
90
-
91
- export slurpTAML = (filepath) ->
92
-
93
- contents = slurp(filepath)
94
- return fromTAML(contents)
16
+ export {isTAML, fromTAML, toTAML, slurpTAML}
package/src/taml.js CHANGED
@@ -1,12 +1,13 @@
1
1
  // Generated by CoffeeScript 2.7.0
2
2
  // taml.coffee
3
- var myReplacer, squote;
4
-
5
3
  import yaml from 'js-yaml';
6
4
 
7
5
  import {
8
6
  assert,
9
- croak
7
+ croak,
8
+ isTAML,
9
+ fromTAML,
10
+ toTAML
10
11
  } from '@jdeighan/exceptions';
11
12
 
12
13
  import {
@@ -32,90 +33,12 @@ import {
32
33
  } from '@jdeighan/coffee-utils/block';
33
34
 
34
35
  import {
35
- slurp
36
+ slurpTAML
36
37
  } from '@jdeighan/coffee-utils/fs';
37
38
 
38
- // ---------------------------------------------------------------------------
39
- // isTAML - is the string valid TAML?
40
- export var isTAML = function(text) {
41
- return isString(text) && (firstLine(text).indexOf('---') === 0);
42
- };
43
-
44
- // ---------------------------------------------------------------------------
45
- // taml - convert valid TAML string to a JavaScript value
46
- export var fromTAML = function(text) {
47
- var _, key, lLines, lMatches, level, line, prefix, str;
48
- if (notdefined(text)) {
49
- return undef;
50
- }
51
- assert(isTAML(text), `string ${OL(text)} isn't TAML`);
52
- lLines = (function() {
53
- var i, len, ref, results;
54
- ref = toArray(text);
55
- results = [];
56
- for (i = 0, len = ref.length; i < len; i++) {
57
- line = ref[i];
58
- [level, str] = splitLine(line);
59
- prefix = ' '.repeat(level);
60
- if (lMatches = line.match(/^([A-Za-z_][A-Za-z0-9_]*)\s*:\s*(.*)$/)) { // the key
61
- [_, key, text] = lMatches;
62
- if (isEmpty(text) || text.match(/\d+(?:\.\d*)$/)) {
63
- results.push(prefix + str);
64
- } else {
65
- results.push(prefix + key + ':' + ' ' + squote(text));
66
- }
67
- } else {
68
- results.push(prefix + str);
69
- }
70
- }
71
- return results;
72
- })();
73
- return yaml.load(toBlock(lLines), {
74
- skipInvalid: true
75
- });
76
- };
77
-
78
- // ---------------------------------------------------------------------------
79
- // --- a replacer is (key, value) -> newvalue
80
- myReplacer = function(name, value) {
81
- if (isString(value)) {
82
- return escapeStr(value);
83
- } else if (isObject(value, ['tamlReplacer'])) {
84
- return value.tamlReplacer();
85
- } else {
86
- return value;
87
- }
88
- };
89
-
90
- // ---------------------------------------------------------------------------
91
- export var toTAML = function(obj, hOptions = {}) {
92
- var escape, replacer, sortKeys, str, useTabs;
93
- ({useTabs, sortKeys, escape, replacer} = hOptions);
94
- if (notdefined(replacer)) {
95
- replacer = myReplacer;
96
- }
97
- str = yaml.dump(obj, {
98
- skipInvalid: true,
99
- indent: 3,
100
- sortKeys: !!sortKeys,
101
- lineWidth: -1,
102
- replacer
103
- });
104
- if (useTabs) {
105
- str = str.replace(/ /g, "\t");
106
- }
107
- return "---\n" + chomp(str);
108
- };
109
-
110
- // ---------------------------------------------------------------------------
111
- squote = function(text) {
112
- return "'" + text.replace(/'/g, "''") + "'";
113
- };
114
-
115
- // ---------------------------------------------------------------------------
116
- // slurpTAML - read TAML from a file
117
- export var slurpTAML = function(filepath) {
118
- var contents;
119
- contents = slurp(filepath);
120
- return fromTAML(contents);
39
+ export {
40
+ isTAML,
41
+ fromTAML,
42
+ toTAML,
43
+ slurpTAML
121
44
  };