-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbufx.js
More file actions
149 lines (146 loc) · 3.64 KB
/
bufx.js
File metadata and controls
149 lines (146 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
const fs = require('fs')
const R = require('ramda')
const yaml = require('js-yaml')
const prettier = require('prettier')
const stripBom = require('strip-bom')
// Numeric value of a string or number
let num = x => (typeof x === 'number' ? x : Number.parseFloat(x))
// String sort comparison function
let acmp = R.comparator((a, b) => a < b)
// Numeric string sort comparison function
let ncmp = R.comparator((a, b) => num(a) < num(b))
// alphanumeric sort
let sorta = R.sort(acmp)
// numeric sort
let sortn = R.sort(ncmp)
// sort -u equivalent
let sortu = a => R.sort(acmp, R.uniq(a))
// Print an array
let printa = a => R.forEach(x => print(x), a)
// Print a sorted, unique array
let printu = a => printa(sortu(a))
// Split a string into lines that may have Windows, Unix, or old Mac line endings.
let lines = s => s.split(/\r\n|\n|\r/)
// Print a string to the console
let print = s => {
console.log(s)
}
// Print a string to the console
let pr = print
// Read a UTF-8 file into a string. Strip the BOM if present.
let read = pathname => stripBom(fs.readFileSync(pathname, 'utf8'))
// Write a string to a UTF-8 file
let write = (s, pathname) => {
fs.writeFileSync(pathname, s, { encoding: 'utf8' })
}
// Print an array or object as JSON
let printJson = o => {
print(JSON.stringify(o))
}
// Write an array or object as JSON to a file
let writeJson = (o, pathname) => {
write(JSON.stringify(o), pathname)
}
// Print an array or object as YAML
let printYaml = o => {
print(yaml.dump(o))
}
// Parse a YAMl string into an object.
let loadYaml = s => {
return yaml.load(s)
}
// Read a YAML file into an array or object.
let readYaml = pathname => {
let s = read(pathname)
return loadYaml(s)
}
// Write an array or object as YAML to a file
let writeYaml = (o, pathname) => {
write(yaml.dump(o), pathname)
}
// Output buffer
let bufs = { _: '' }
// Open a buffer for writing
let open = channel => (bufs[channel] = '')
// Emit a string to a buffer
let emit = (a, channel = null) => {
channel = channel || '_'
bufs[channel] += a
}
// Emit a string to a buffer terminated by a newline
let emitLine = (a, channel = null) => {
channel = channel || '_'
bufs[channel] += a + '\n'
}
// Emit an object to a buffer as compact JSON
let emitJson = (o, channel = null) => {
channel = channel || '_'
bufs[channel] += JSON.stringify(o)
}
// Emit an object to a buffer as formatted JSON
let emitPrettyJson = (o, channel = null) => {
let json = JSON.stringify(o)
channel = channel || '_'
bufs[channel] += prettier.format(json, { parser: 'json' })
}
// Emit a JavaScript string to a buffer with formatting
let emitJs = (js, opts = null) => {
let options = opts || { semi: false, singleQuote: true, parser: 'babel' }
let s = prettier.format(js, options)
emit(s)
}
// Emit an object to a buffer as YAML
let emitYaml = (o, channel = null) => {
channel = channel || '_'
bufs[channel] += yaml.dump(o)
}
// Print a buffer to the console
let printBuf = (channel = null) => {
channel = channel || '_'
print(bufs[channel])
}
// Write a buffer to a file
let writeBuf = (pathname, channel = null) => {
channel = channel || '_'
write(bufs[channel], pathname)
}
// Get buffer contents as a string
let getBuf = (channel = null) => {
channel = channel || '_'
return bufs[channel]
}
// Clear a buffer
let clearBuf = (channel = null) => {
channel = channel || '_'
bufs[channel] = ''
}
module.exports = {
clearBuf,
emit,
emitJs,
emitJson,
emitLine,
emitPrettyJson,
emitYaml,
getBuf,
lines,
loadYaml,
num,
open,
pr,
print,
printa,
printBuf,
printJson,
printYaml,
printu,
read,
readYaml,
sorta,
sortn,
sortu,
write,
writeBuf,
writeJson,
writeYaml
}