aboutsummaryrefslogtreecommitdiff
path: root/src/lib/common
diff options
context:
space:
mode:
authorIRONM00N <64110067+IRONM00N@users.noreply.github.com>2022-05-28 12:43:42 -0400
committerIRONM00N <64110067+IRONM00N@users.noreply.github.com>2022-05-28 12:43:42 -0400
commit31d339b46b9fc11e50a0cdbf53e937e575531efd (patch)
tree05148c87a0be4992ad41b1829f17ffb6b75f7a3c /src/lib/common
parente37904bdf74b2c21d97d4ceb89a2a38a74b08919 (diff)
downloadtanzanite-31d339b46b9fc11e50a0cdbf53e937e575531efd.tar.gz
tanzanite-31d339b46b9fc11e50a0cdbf53e937e575531efd.tar.bz2
tanzanite-31d339b46b9fc11e50a0cdbf53e937e575531efd.zip
feat: custom stripIndent
Diffstat (limited to 'src/lib/common')
-rw-r--r--src/lib/common/tags.ts34
1 files changed, 34 insertions, 0 deletions
diff --git a/src/lib/common/tags.ts b/src/lib/common/tags.ts
new file mode 100644
index 0000000..098cf29
--- /dev/null
+++ b/src/lib/common/tags.ts
@@ -0,0 +1,34 @@
+/* these functions are adapted from the common-tags npm package which is licensed under the MIT license */
+/* the js docs are adapted from the @types/common-tags npm package which is licensed under the MIT license */
+
+/**
+ * Strips the **initial** indentation from the beginning of each line in a multiline string.
+ */
+export function stripIndent(strings: TemplateStringsArray, ...expressions: any[]) {
+ const str = format(strings, ...expressions);
+ // remove the shortest leading indentation from each line
+ const match = str.match(/^[^\S\n]*(?=\S)/gm);
+ const indent = match && Math.min(...match.map((el) => el.length));
+ if (indent) {
+ const regexp = new RegExp(`^.{${indent}}`, 'gm');
+ return str.replace(regexp, '');
+ }
+ return str;
+}
+
+/**
+ * Strips **all** of the indentation from the beginning of each line in a multiline string.
+ */
+export function stripIndents(strings: TemplateStringsArray, ...expressions: any[]) {
+ const str = format(strings, ...expressions);
+ // remove all indentation from each line
+ return str.replace(/^[^\S\n]+/gm, '');
+}
+
+function format(strings: TemplateStringsArray, ...expressions: any[]) {
+ const str = strings
+ .reduce((result, string, index) => ''.concat(result, expressions[index - 1], string))
+ .replace(/[^\S\n]+$/gm, '')
+ .replace(/^\n/, '');
+ return str;
+}