feature: vor und nachteile

This commit is contained in:
Hikyu
2026-05-31 13:54:31 +02:00
parent 552a947a1c
commit e3dad17446
23270 changed files with 3024794 additions and 200 deletions
@@ -0,0 +1,19 @@
/**
* @description
* It returns the common maximum indentation length among the lines of the
* provided string. Lines that are only spaces are not taken into account.
*
* It throws error if a `\s` character that is not ` ` (space) is encountered
* in the indentation.
* @example
* commonMinIndentLength(
* ` hello
* world!
* `);
* //returns
* 3;
*/
export declare function commonMaxIndentLength(s: string): number;
export declare const _errorMessages: {
badIndentSpaceCharacter: string;
};
@@ -0,0 +1,40 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports._errorMessages = exports.commonMaxIndentLength = void 0;
/**
* @description
* It returns the common maximum indentation length among the lines of the
* provided string. Lines that are only spaces are not taken into account.
*
* It throws error if a `\s` character that is not ` ` (space) is encountered
* in the indentation.
* @example
* commonMinIndentLength(
* ` hello
* world!
* `);
* //returns
* 3;
*/
function commonMaxIndentLength(s) {
let minIndentLength = Infinity;
s.split("\n").forEach((line) => {
if (/^[ ]+$/.test(line) || line.length === 0)
return;
let newMinIndentLength = 0;
for (let i = 0; i < line.length; i++) {
if (/\s/.test(line[i]) && line[i] !== " ")
throw Error(exports._errorMessages.badIndentSpaceCharacter);
if (line[i] !== " ")
break;
newMinIndentLength++;
}
if (newMinIndentLength < minIndentLength)
minIndentLength = newMinIndentLength;
});
return minIndentLength;
}
exports.commonMaxIndentLength = commonMaxIndentLength;
exports._errorMessages = {
badIndentSpaceCharacter: "Only space characters are allowed in the indented part of the string",
};
@@ -0,0 +1,5 @@
/**
* @description
* Return the last line of the provided string.
*/
export declare function getLastLineOfString(string: string): string;
@@ -0,0 +1,12 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getLastLineOfString = void 0;
/**
* @description
* Return the last line of the provided string.
*/
function getLastLineOfString(string) {
const lines = string.split("\n");
return lines[lines.length - 1];
}
exports.getLastLineOfString = getLastLineOfString;
@@ -0,0 +1 @@
export { tagUnindent } from "./tagUnindent";
@@ -0,0 +1,5 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.tagUnindent = void 0;
var tagUnindent_1 = require("./tagUnindent");
Object.defineProperty(exports, "tagUnindent", { enumerable: true, get: function () { return tagUnindent_1.tagUnindent; } });
@@ -0,0 +1,30 @@
/**
* @description
* Tag function that returns the template literal it is provided as a string, but
* with its common maximum indentation removed.
*
* The placeholders that are provided as single string element arrays are multi-line indented.
*
* It throws if :
*
* - the first line is non empty
* - the last line is not only spaces
* - the string contains `\s` characters that are not space characters in its indentation
*
* @example
* expect(
* tagUnindent`
* path : (${`"./some/where"`})
* index : ${0}
* message :
* ${["hello\nworld"]}
* `
* ).toBe(
* `path : ("./some/where")\n` +
* `index : 0\n` +
* `message : \n` +
* ` hello\n` +
* ` world`
* );
*/
export declare function tagUnindent(stringArray: TemplateStringsArray, ...placeholders: (number | string | [string])[]): string;
@@ -0,0 +1,65 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.tagUnindent = void 0;
const getLastLineOfString_1 = require("./getLastLineOfString");
const unindent_1 = require("./unindent");
/**
* @description
* Tag function that returns the template literal it is provided as a string, but
* with its common maximum indentation removed.
*
* The placeholders that are provided as single string element arrays are multi-line indented.
*
* It throws if :
*
* - the first line is non empty
* - the last line is not only spaces
* - the string contains `\s` characters that are not space characters in its indentation
*
* @example
* expect(
* tagUnindent`
* path : (${`"./some/where"`})
* index : ${0}
* message :
* ${["hello\nworld"]}
* `
* ).toBe(
* `path : ("./some/where")\n` +
* `index : 0\n` +
* `message : \n` +
* ` hello\n` +
* ` world`
* );
*/
function tagUnindent(stringArray, ...placeholders) {
const stringToUnindent = (() => {
if (placeholders.length === 0)
return stringArray[0];
let toReturn = "";
for (let i = 0; i < placeholders.length; i++) {
const currentPlaceholder = placeholders[i];
if (Array.isArray(currentPlaceholder)) {
const lastStringArrayLineLength = getLastLineOfString_1.getLastLineOfString(stringArray[i]).length;
const [placeholderSingleElementArrayString] = currentPlaceholder;
toReturn =
toReturn +
stringArray[i] +
placeholderSingleElementArrayString
.split("\n")
.map((line, i) => {
if (i === 0)
return line;
return " ".repeat(lastStringArrayLineLength) + line;
})
.join("\n");
}
else {
toReturn = toReturn + stringArray[i] + currentPlaceholder;
}
}
return toReturn + stringArray[stringArray.length - 1];
})();
return unindent_1.unindent(stringToUnindent);
}
exports.tagUnindent = tagUnindent;
@@ -0,0 +1,42 @@
/**
* @description
* It removes the first and last line of the provided string. From the remaining lines it
* calculates the common maximum indentation, ignoring empty lines, and then it subtracts it from
* each non empty line.
*
* The subtraction happens also for empty lines that have length bigger than the common minimum
* indentation length. If they have length less than the common minimum indentation length they are
* replaced with an empty string;
*
* - If `\s` characters are encountered in the indentation string that are not ` ` (space), it throws error.
* - If the first line is not an empty string, it throws error.
* - If the last line is not only space characters it throws error.
* @example
* unindent(
* `
* transaction tree string representation
*
* |_ "p"
* |_ "a"
* |_ "t"
* |_ "h"
* | |_ "prop1" -1 => 1
* |_ "t"
* |_ "prop" "11" => "1"
* `);
* //returns
* ` transaction tree string representation
*
* |_ "p"
* |_ "a"
* |_ "t"
* |_ "h"
* | |_ "prop1" -1 => 1
* |_ "t"
* |_ "prop" "11" => "1"`;
*/
export declare function unindent(s: string): string;
export declare const _errorMessage: {
badFirstLine: string;
badLastLine: string;
};
@@ -0,0 +1,63 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports._errorMessage = exports.unindent = void 0;
const commonMaxIndentLength_1 = require("./commonMaxIndentLength");
/**
* @description
* It removes the first and last line of the provided string. From the remaining lines it
* calculates the common maximum indentation, ignoring empty lines, and then it subtracts it from
* each non empty line.
*
* The subtraction happens also for empty lines that have length bigger than the common minimum
* indentation length. If they have length less than the common minimum indentation length they are
* replaced with an empty string;
*
* - If `\s` characters are encountered in the indentation string that are not ` ` (space), it throws error.
* - If the first line is not an empty string, it throws error.
* - If the last line is not only space characters it throws error.
* @example
* unindent(
* `
* transaction tree string representation
*
* |_ "p"
* |_ "a"
* |_ "t"
* |_ "h"
* | |_ "prop1" -1 => 1
* |_ "t"
* |_ "prop" "11" => "1"
* `);
* //returns
* ` transaction tree string representation
*
* |_ "p"
* |_ "a"
* |_ "t"
* |_ "h"
* | |_ "prop1" -1 => 1
* |_ "t"
* |_ "prop" "11" => "1"`;
*/
function unindent(s) {
const lines = s.split("\n");
if (lines[0] !== "")
throw Error(exports._errorMessage.badFirstLine);
if (!/^[ ]*$/.test(lines[lines.length - 1]))
throw Error(exports._errorMessage.badLastLine);
const minIndentLength = commonMaxIndentLength_1.commonMaxIndentLength(s);
return s
.split("\n")
.slice(1, -1)
.map((line) => {
if (/^\s*$/.test(line) && line.length < minIndentLength)
return "";
return line.slice(minIndentLength, line.length);
})
.join("\n");
}
exports.unindent = unindent;
exports._errorMessage = {
badFirstLine: `first line has to be an empty string`,
badLastLine: "last line has to be only space characters",
};