@bscotch/utility
Advanced tools
Comparing version 1.1.2 to 2.0.0
/// <reference types="node" /> | ||
export declare function sortStringsByLength(strings: string[]): string[]; | ||
export declare function getShortestString(strings: string[]): string; | ||
/** | ||
* Shift all lines left by the *smallest* indentation level, | ||
* and remove initial newline and all trailing spaces. | ||
* Does not take into account indentation on lines that only | ||
* have spaces. | ||
* Lines that only have spaces are not used to determine the | ||
* indentation level. | ||
*/ | ||
@@ -8,0 +10,0 @@ export declare function undent(strings: TemplateStringsArray, ...interps: any[]): string; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.strings = exports.explode = exports.capitalize = exports.encodeToBase64JsonString = exports.decodeFromBase64JsonString = exports.decodeFromBase64 = exports.encodeToBase64 = exports.oneline = exports.nodent = exports.undent = void 0; | ||
exports.strings = exports.explode = exports.capitalize = exports.encodeToBase64JsonString = exports.decodeFromBase64JsonString = exports.decodeFromBase64 = exports.encodeToBase64 = exports.oneline = exports.nodent = exports.undent = exports.getShortestString = exports.sortStringsByLength = void 0; | ||
const errors_1 = require("./errors"); | ||
function populateTemplate(strings, ...interps) { | ||
function withoutInitialLinebreaks(string) { | ||
return string.replace(/^[\r\n]+/, ''); | ||
} | ||
function withoutTrailingWhitespace(string) { | ||
return string.replace(/\s+$/, ''); | ||
} | ||
function cleanTemplate(strings, ...interps) { | ||
// Trim these things up | ||
const cleanStrings = [...strings]; | ||
cleanStrings[0] = withoutInitialLinebreaks(cleanStrings[0]); | ||
const lastStringIdx = cleanStrings.length - 1; | ||
cleanStrings[lastStringIdx] = withoutTrailingWhitespace(cleanStrings[lastStringIdx]); | ||
// For each interp, if it has newlines when stringified each | ||
// line after the first needs to inherit the indentation | ||
// level of its starting point. | ||
let string = ''; | ||
for (let i = 0; i < strings.length; i++) { | ||
string += `${strings[i] || ''}${interps[i] || ''}`; | ||
for (let i = 0; i < cleanStrings.length; i++) { | ||
string += cleanStrings[i]; | ||
if (i == lastStringIdx) { | ||
break; | ||
} | ||
let interp = `${interps[i]}`; | ||
const linebreakRegex = /(\r?\n)/; | ||
const interpLines = interp.split(linebreakRegex).filter((x) => x); | ||
if (interpLines.length && i < lastStringIdx) { | ||
// How indented are we? | ||
const indentMatch = string.match(/\n?([^\n]+?)$/); | ||
if (indentMatch) { | ||
// amount of indent to add to each entry that is a break | ||
// (skip the last one, since if it's a newline we don't | ||
// want that to cause an indent on the next line also) | ||
for (let i = 0; i < interpLines.length; i++) { | ||
if (interpLines[i].match(linebreakRegex)) { | ||
interpLines[i] += ' '.repeat(indentMatch[1].length); | ||
} | ||
} | ||
} | ||
} | ||
interp = interpLines.join(''); | ||
string += interp; | ||
} | ||
return string; | ||
} | ||
function sortStringsByLength(strings) { | ||
return strings.sort((string1, string2) => string1.length - string2.length); | ||
} | ||
exports.sortStringsByLength = sortStringsByLength; | ||
function getShortestString(strings) { | ||
return sortStringsByLength(strings)[0]; | ||
} | ||
exports.getShortestString = getShortestString; | ||
/** | ||
* Shift all lines left by the *smallest* indentation level, | ||
* and remove initial newline and all trailing spaces. | ||
* Does not take into account indentation on lines that only | ||
* have spaces. | ||
* Lines that only have spaces are not used to determine the | ||
* indentation level. | ||
*/ | ||
function undent(strings, ...interps) { | ||
var _a; | ||
let string = populateTemplate(strings, ...interps); | ||
const string = cleanTemplate(strings, ...interps); | ||
// Remove initial and final newlines | ||
string = string.replace(/^[\r\n]+/, '').replace(/\s+$/, ''); | ||
// Find all indentations *on lines that are not just whitespace!* | ||
@@ -37,4 +80,3 @@ const indentRegex = /^(?<indent>[ \t]*)(?<nonSpace>[^\s])?/; | ||
} | ||
dents.sort((dent1, dent2) => dent1.length - dent2.length); | ||
const minDent = dents[0]; | ||
const minDent = getShortestString(dents); | ||
if (!minDent) { | ||
@@ -52,3 +94,3 @@ // Then min indentation is 0, no change needed | ||
function nodent(strings, ...interps) { | ||
let string = populateTemplate(strings, ...interps); | ||
let string = cleanTemplate(strings, ...interps); | ||
// Remove initial and final newlines | ||
@@ -66,3 +108,3 @@ string = string.replace(/^[\r\n]+/, '').replace(/\s+$/, ''); | ||
function oneline(strings, ...interps) { | ||
return populateTemplate(strings, ...interps) | ||
return cleanTemplate(strings, ...interps) | ||
.replace(/^\s+/, '') | ||
@@ -69,0 +111,0 @@ .replace(/\s+$/, '') |
@@ -0,1 +1,10 @@ | ||
# [2.0.0](https://github.com/bscotch/node-util/compare/v1.1.2...v2.0.0) (2021-06-04) | ||
### Features | ||
* Change the 'undent' template function to handle interpolated values that have newlines inside them. There's no one-size-fits-all solution here, but having interped values inherit the indent level of the line they start on is a decent solution. BREAKING. ([bdfa7e7](https://github.com/bscotch/node-util/commit/bdfa7e76d6e8f847e7bf44adba684eb3957193fe)) | ||
## [1.1.2](https://github.com/bscotch/node-util/compare/v1.1.1...v1.1.2) (2021-05-18) | ||
@@ -2,0 +11,0 @@ |
{ | ||
"name": "@bscotch/utility", | ||
"version": "1.1.2", | ||
"version": "2.0.0", | ||
"description": "Bscotch Utilities: Methods for common Node.js needs.", | ||
@@ -19,3 +19,3 @@ "engines": { | ||
"version": "npx conventional-changelog -p angular -i CHANGELOG.md -s -r 0 && git add -A", | ||
"postversion": "git push origin develop && git checkout main && git merge develop && git push origin main --follow-tags && npm publish && git checkout develop && git merge main", | ||
"postversion": "git push origin develop --follow-tags && npm publish", | ||
"tsc-live": "npx tsc -w" | ||
@@ -56,3 +56,2 @@ }, | ||
"source-map-support": "^0.5.19", | ||
"ts-node": "^9.1.1", | ||
"typedoc": "^0.20.36", | ||
@@ -59,0 +58,0 @@ "typescript": "^4.2.4" |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
103314
17
1254