configorama 0.4.10 → 0.5.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.
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Split a string by comma while preserving quoted content
3
+ * @param {string} str - String to split
4
+ * @param {string} splitter - Optional custom splitter (defaults to ',')
5
+ * @returns {string[]} Array of split strings
6
+ */
7
+ function splitCsv(str, splitter) {
8
+ const splitSyntax = splitter || ','
9
+ // Split at comma SPACE ", "
10
+ return str.split(splitSyntax).reduce(
11
+ (accum, curr) => {
12
+ if (accum.isConcatting) {
13
+ accum.soFar[accum.soFar.length - 1] += ',' + curr
14
+ } else {
15
+ accum.soFar.push(curr)
16
+ }
17
+ if (curr.split('"').length % 2 == 0) {
18
+ accum.isConcatting = !accum.isConcatting
19
+ }
20
+ return accum
21
+ },
22
+ {
23
+ soFar: [],
24
+ isConcatting: false,
25
+ },
26
+ ).soFar
27
+ }
28
+
29
+ module.exports = { splitCsv }
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Get text after first occurrence of search string
3
+ * @param {string} str - Source string
4
+ * @param {string} search - String to search for
5
+ * @returns {string} Text after search string or empty string if not found
6
+ */
7
+ function getTextAfterOccurance(str, search) {
8
+ const index = str.indexOf(search)
9
+ if (index === -1) return ''
10
+ return str.substring(index)
11
+ }
12
+
13
+ /**
14
+ * Find nested variable in split array that exists in original source
15
+ * @param {string[]} split - Array of potential variables
16
+ * @param {string} originalSource - Original source string
17
+ * @returns {string|undefined} Found nested variable or undefined
18
+ */
19
+ function findNestedVariable(split, originalSource) {
20
+ return split.find((thing) => {
21
+ if (originalSource && typeof originalSource === 'string') {
22
+ return originalSource.indexOf(`\${${thing}}`) > -1
23
+ }
24
+ return false
25
+ })
26
+ }
27
+
28
+ module.exports = {
29
+ getTextAfterOccurance,
30
+ findNestedVariable
31
+ }
@@ -0,0 +1,46 @@
1
+ /**
2
+ * Encode unknown variable for passthrough
3
+ */
4
+ function encodeUnknown(v) {
5
+ return `>passthrough[_[${Buffer.from(v).toString('base64')}]_]`
6
+ }
7
+
8
+ /**
9
+ * Decode unknown variable from passthrough
10
+ */
11
+ function decodeUnknown(rawValue) {
12
+ const x = findUnknownValues(rawValue)
13
+ let val = rawValue.replace(/>passthrough/g, '')
14
+ if (x.length) {
15
+ x.forEach(({ match, value }) => {
16
+ const decodedValue = Buffer.from(value, 'base64').toString('ascii')
17
+ val = val.replace(match, decodedValue)
18
+ })
19
+ }
20
+ return val
21
+ }
22
+
23
+ /**
24
+ * Find base64 encoded unknown values in text
25
+ */
26
+ function findUnknownValues(text) {
27
+ const base64WrapperRegex = /\[_\[([A-Za-z0-9+/=\s]*)\]_\]/g
28
+ let matches
29
+ const links = []
30
+ while ((matches = base64WrapperRegex.exec(text)) !== null) {
31
+ if (matches.index === base64WrapperRegex.lastIndex) {
32
+ base64WrapperRegex.lastIndex++
33
+ }
34
+ links.push({
35
+ match: matches[0],
36
+ value: matches[1],
37
+ })
38
+ }
39
+ return links
40
+ }
41
+
42
+ module.exports = {
43
+ encodeUnknown,
44
+ decodeUnknown,
45
+ findUnknownValues
46
+ }
@@ -0,0 +1,52 @@
1
+ /**
2
+ * Get fallback variable string
3
+ * @param {string[]} split - Array from split at comma
4
+ * @param {string} nestedVar - Fallback variable to reconstruct variable string from
5
+ * @returns {string} New ${variable, string}
6
+ */
7
+ function getFallbackString(split, nestedVar) {
8
+ let isSet = false
9
+ const newVar = split
10
+ .reduce((acc, curr) => {
11
+ if (curr === nestedVar || isSet) {
12
+ acc = acc.concat(curr)
13
+ isSet = true
14
+ }
15
+ return acc
16
+ }, [])
17
+ .join(', ')
18
+ const cleanC = `\${${newVar.replace(/^\${/, '').replace(/}$/, '')}}`
19
+ return cleanC
20
+ }
21
+
22
+ /**
23
+ * Verify if variable string is valid
24
+ */
25
+ function verifyVariable(variableString, valueObject, variableTypes, config) {
26
+ const isRealVariable = variableTypes.some((r) => {
27
+ if (r.match instanceof RegExp && variableString.match(r.match)) {
28
+ return true
29
+ } else if (typeof r.match === 'function') {
30
+ if (r.match(variableString, config, valueObject)) {
31
+ return true
32
+ }
33
+ }
34
+ return false
35
+ })
36
+
37
+ if (!isRealVariable && variableString.match(/:/)) {
38
+ throw new Error(`
39
+ Variable \${${variableString}} is invalid variable syntax.
40
+ Value Path: ${valueObject.path ? valueObject.path.join('.') : 'na'}
41
+ Original Value: ${valueObject.originalSource}
42
+
43
+ Remove or update the \${${variableString}} to fix
44
+ `)
45
+ }
46
+ return isRealVariable
47
+ }
48
+
49
+ module.exports = {
50
+ getFallbackString,
51
+ verifyVariable
52
+ }
package/package.json CHANGED
@@ -1,18 +1,23 @@
1
1
  {
2
2
  "name": "configorama",
3
- "version": "0.4.10",
3
+ "version": "0.5.0",
4
4
  "description": "Variable support for configuration files",
5
5
  "main": "lib/index.js",
6
6
  "files": [
7
+ "cli.js",
7
8
  "lib",
8
9
  "package.json",
9
10
  "package-lock.json",
10
11
  "README.md"
11
12
  ],
13
+ "bin": {
14
+ "configorama": "./cli.js"
15
+ },
12
16
  "scripts": {
13
17
  "docs": "node ./scripts/docs.js",
14
- "test": "uvu tests \".*\\.test.js$\"",
18
+ "test": "npm run testlib && uvu tests \".*\\.test.js$\" ",
15
19
  "test:api": "uvu tests/api api.test.js",
20
+ "testlib": "uvu lib \".*\\.test.js$\"",
16
21
  "watch": "watchlist tests -- npm test",
17
22
  "publish": "git push origin && git push origin --tags",
18
23
  "release:patch": "npm version patch && npm publish",
@@ -41,7 +46,6 @@
41
46
  "lodash.isregexp": "^4.0.1",
42
47
  "lodash.isfunction": "^3.0.9",
43
48
  "lodash.isempty": "^4.4.0",
44
- "lodash.trim": "^4.5.1",
45
49
  "lodash.camelcase": "^4.3.0",
46
50
  "lodash.kebabcase": "^4.1.1",
47
51
  "lodash.capitalize": "^4.2.1",
@@ -49,12 +53,10 @@
49
53
  "lodash.map": "^4.6.0",
50
54
  "lodash.mapvalues": "^4.6.0",
51
55
  "lodash.assign": "^4.2.0",
52
- "lodash.set": "^4.3.2",
53
56
  "lodash.clonedeep": "^4.5.0",
54
57
  "lodash.includes": "^4.3.0",
55
58
  "lodash.flatten": "^4.4.0",
56
59
  "promise.prototype.finally": "^3.1.8",
57
- "replaceall": "^0.1.6",
58
60
  "sync-rpc": "^1.3.6",
59
61
  "traverse": "^0.6.8"
60
62
  },