summaryrefslogtreecommitdiff
path: root/std/strings
diff options
context:
space:
mode:
Diffstat (limited to 'std/strings')
-rw-r--r--std/strings/README.md26
-rw-r--r--std/strings/decode.ts7
-rw-r--r--std/strings/encode.ts7
-rw-r--r--std/strings/mod.ts5
-rw-r--r--std/strings/pad.ts75
-rw-r--r--std/strings/pad_test.ts73
6 files changed, 193 insertions, 0 deletions
diff --git a/std/strings/README.md b/std/strings/README.md
new file mode 100644
index 000000000..8b41fee67
--- /dev/null
+++ b/std/strings/README.md
@@ -0,0 +1,26 @@
+# Strings
+
+This module provides a few basic utilities to manipulate strings.
+
+## Usage
+
+### pad
+
+Input string is processed to output a string with a minimal length.
+If the parameter `strict` is set to true, the output string length
+is equal to the `strLen` parameter.
+
+Basic usage:
+
+```ts
+import { pad } from "https://deno.land/std/strings/pad.ts";
+pad("deno", 6, { char: "*", side: "left" }) // output : "**deno"
+pad("deno", 6, { char: "*", side: "right"}) // output : "deno**"
+pad("denosorusrex", 6 {
+ char: "*",
+ side: "left",
+ strict: true,
+ strictSide: "right",
+ strictChar: "..."
+}) // output : "den..."
+```
diff --git a/std/strings/decode.ts b/std/strings/decode.ts
new file mode 100644
index 000000000..2e161d7af
--- /dev/null
+++ b/std/strings/decode.ts
@@ -0,0 +1,7 @@
+/** A default TextDecoder instance */
+export const decoder = new TextDecoder();
+
+/** Shorthand for new TextDecoder().decode() */
+export function decode(input?: Uint8Array): string {
+ return decoder.decode(input);
+}
diff --git a/std/strings/encode.ts b/std/strings/encode.ts
new file mode 100644
index 000000000..285305613
--- /dev/null
+++ b/std/strings/encode.ts
@@ -0,0 +1,7 @@
+/** A default TextEncoder instance */
+export const encoder = new TextEncoder();
+
+/** Shorthand for new TextEncoder().encode() */
+export function encode(input?: string): Uint8Array {
+ return encoder.encode(input);
+}
diff --git a/std/strings/mod.ts b/std/strings/mod.ts
new file mode 100644
index 000000000..2acc32600
--- /dev/null
+++ b/std/strings/mod.ts
@@ -0,0 +1,5 @@
+// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
+
+export * from "./encode.ts";
+export * from "./decode.ts";
+export * from "./pad.ts";
diff --git a/std/strings/pad.ts b/std/strings/pad.ts
new file mode 100644
index 000000000..267839a39
--- /dev/null
+++ b/std/strings/pad.ts
@@ -0,0 +1,75 @@
+// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
+
+/** FillOption Object */
+export interface FillOption {
+ /** Char to fill in */
+ char?: string;
+ /** Side to fill in */
+ side?: "left" | "right";
+ /** If strict, output string can't be greater than strLen*/
+ strict?: boolean;
+ /** char/string used to specify the string has been truncated */
+ strictChar?: string;
+ /** Side of truncate */
+ strictSide?: "left" | "right";
+}
+
+/**
+ * Pad helper for strings.
+ * Input string is processed to output a string with a minimal length.
+ * If the parameter `strict` is set to true, the output string length
+ * is equal to the `strLen` parameter.
+ * Example:
+ *
+ * pad("deno", 6, { char: "*", side: "left" }) // output : "**deno"
+ * pad("deno", 6, { char: "*", side: "right"}) // output : "deno**"
+ * pad("denosorusrex", 6 {
+ * char: "*",
+ * side: "left",
+ * strict: true,
+ * strictSide: "right",
+ * strictChar: "..."
+ * }) // output : "den..."
+ *
+ * @param input Input string
+ * @param strLen Output string lenght
+ * @param opts Configuration object
+ * @param [opts.char=" "] Character used to fill in
+ * @param [opts.side="left"] Side to fill in
+ * @param [opts.strict=false] Flag to truncate the string if length > strLen
+ * @param [opts.strictChar=""] Character to add if string is truncated
+ * @param [opts.strictSide="right"] Side to truncate
+ */
+export function pad(
+ input: string,
+ strLen: number,
+ opts: FillOption = {
+ char: " ",
+ strict: false,
+ side: "left",
+ strictChar: "",
+ strictSide: "right"
+ }
+): string {
+ let out = input;
+ const outL = out.length;
+ if (outL < strLen) {
+ if (!opts.side || opts.side === "left") {
+ out = out.padStart(strLen, opts.char);
+ } else {
+ out = out.padEnd(strLen, opts.char);
+ }
+ } else if (opts.strict && outL > strLen) {
+ const addChar = opts.strictChar ? opts.strictChar : "";
+ if (opts.strictSide === "left") {
+ let toDrop = outL - strLen;
+ if (opts.strictChar) {
+ toDrop += opts.strictChar.length;
+ }
+ out = `${addChar}${out.slice(toDrop, outL)}`;
+ } else {
+ out = `${out.substring(0, strLen - addChar.length)}${addChar}`;
+ }
+ }
+ return out;
+}
diff --git a/std/strings/pad_test.ts b/std/strings/pad_test.ts
new file mode 100644
index 000000000..e0364cf38
--- /dev/null
+++ b/std/strings/pad_test.ts
@@ -0,0 +1,73 @@
+import { test } from "../testing/mod.ts";
+import { assertEquals } from "../testing/asserts.ts";
+import { pad } from "./pad.ts";
+
+test(function padTest(): void {
+ const expected1 = "**deno";
+ const expected2 = "deno";
+ const expected3 = "deno**";
+ const expected4 = "denosorusrex";
+ const expected5 = "denosorus";
+ const expected6 = "sorusrex";
+ const expected7 = "den...";
+ const expected8 = "...rex";
+ assertEquals(pad("deno", 6, { char: "*", side: "left" }), expected1);
+ assertEquals(pad("deno", 4, { char: "*", side: "left" }), expected2);
+ assertEquals(pad("deno", 6, { char: "*", side: "right" }), expected3);
+ assertEquals(
+ pad("denosorusrex", 4, {
+ char: "*",
+ side: "right",
+ strict: false
+ }),
+ expected4
+ );
+ assertEquals(
+ pad("denosorusrex", 9, {
+ char: "*",
+ side: "left",
+ strict: true,
+ strictSide: "right"
+ }),
+ expected5
+ );
+ assertEquals(
+ pad("denosorusrex", 8, {
+ char: "*",
+ side: "left",
+ strict: true,
+ strictSide: "left"
+ }),
+ expected6
+ );
+ assertEquals(
+ pad("denosorusrex", 6, {
+ char: "*",
+ side: "left",
+ strict: true,
+ strictSide: "right",
+ strictChar: "..."
+ }),
+ expected7
+ );
+ assertEquals(
+ pad("denosorusrex", 6, {
+ char: "*",
+ side: "left",
+ strict: true,
+ strictSide: "left",
+ strictChar: "..."
+ }),
+ expected8
+ );
+ assertEquals(
+ pad("deno", 4, {
+ char: "*",
+ side: "left",
+ strict: true,
+ strictSide: "right",
+ strictChar: "..."
+ }),
+ expected2
+ );
+});