Skip to content

Commit

Permalink
wip: initial content gen
Browse files Browse the repository at this point in the history
  • Loading branch information
btlghrants committed Dec 19, 2023
1 parent 2158ea1 commit 76b9d98
Show file tree
Hide file tree
Showing 112 changed files with 18,616 additions and 29 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist/
node_modules/
29 changes: 0 additions & 29 deletions build.sh

This file was deleted.

1 change: 1 addition & 0 deletions build/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
work/
32 changes: 32 additions & 0 deletions build/heredoc.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Refs:
// - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#tagged_templates

export function heredoc(strings, ...values) {
// shuffle strings & expression values back together
const zipped = strings.reduce((acc, cur, idx) => {
acc.push(cur, values[idx] || '')
return acc
}, []).filter(x => x)

// rebuild as line-oriented
const asLines = zipped.join('').split(/[\r\n]+/)

// strip whitespace-only first & last lines
asLines[0].trim().length === 0 ? asLines.shift() : null
asLines.slice(-1)[0].trim().length === 0 ? asLines.pop() : null

// find smallest indent
const indent = asLines.reduce((acc, cur) => {
const firstAt = cur.search(/\S/)
return Math.min(firstAt !== -1 ? firstAt : Number.POSITIVE_INFINITY, acc)
}, Number.POSITIVE_INFINITY)

// de-indent all lines
const snipped = asLines.map(line => {
if (line.length < indent) { return "" }
return line.slice(indent)
})

// rejoin into multiline string
return snipped.join("\n")
}
20 changes: 20 additions & 0 deletions build/heredoc.unit.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { describe, expect, it } from '@jest/globals';
import { heredoc } from './heredoc.mjs'

describe("heredoc", () => {
it("trims head/tail empty lines & de-indents", async () => {
const actual = heredoc`
---
indented:
like:
- yaml
`
const expected =
`---
indented:
like:
- yaml`

expect(actual).toBe(expected)
})
})
Loading

0 comments on commit 76b9d98

Please sign in to comment.