Skip to content

Instantly share code, notes, and snippets.

@emmaly
Created September 22, 2023 19:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emmaly/66f0b29a160cf0ec0bdfccea121399a1 to your computer and use it in GitHub Desktop.
Save emmaly/66f0b29a160cf0ec0bdfccea121399a1 to your computer and use it in GitHub Desktop.
BuildShip Node: KV to JSON
{
"meta": {
"id": "f803c041-7f48-48d0-852c-22adff386327",
"icon": {
"url": null,
"type": "URL"
},
"description": "Converts Key/Value list to JSON",
"name": "KV to JSON"
},
"script": "export default async ({kv, delimiter='=', returnAsObject=true},{logging}) => {\n // logging.log(\"Hello World\")\n\n if (typeof kv !== 'string') {\n throw new TypeError('kv must be a string');\n }\n\n if (delimiter && typeof delimiter !== 'string') {\n throw new TypeError('delimiter must be a string');\n }\n\n const result = {};\n\n kv.split(/(\\r?\\n)+/)\n .forEach(line => {\n const pair = line.split(delimiter);\n\n if (!pair) return; // unexpected\n if (pair.length < 2) return; // no delimiter was hit?\n \tif (pair[0].trim().length < 1) return; // no key on left side of delimiter\n\n // trim(key) = trim(remaining portion of the \"pair\" reassembled by delimiter)\n\t result[pair[0].trim()] = pair.slice(1).join(delimiter).trim();\n });\n\n if (!returnAsObject) return JSON.stringify(result);\n return result;\n}\n",
"inputs": [
{
"description": "",
"required": true,
"label": "KV Raw String",
"key": "kv",
"type": "string",
"validation": ""
},
{
"type": "string",
"required": true,
"label": "Pair Delimiter",
"description": "",
"key": "delimiter",
"validation": "",
"defaultValue": "="
},
{
"description": "If not, it'll be stringified JSON.",
"type": "boolean",
"key": "returnAsObject",
"defaultValue": true,
"label": "Return As Object",
"validation": ""
}
],
"output": {
"label": "KV to JSON",
"type": "object",
"parameters": []
},
"type": "script",
"name": "KV to JSON"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment