From 0b39b2c95a51227e01a94678928ba334c3d27961 Mon Sep 17 00:00:00 2001 From: Nishchal Gautam Date: Thu, 12 Dec 2019 14:24:55 +0700 Subject: [PATCH 1/3] fix(padStart): fix padStart call for older browser --- src/adapters/TinyNumberCompressor.ts | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/adapters/TinyNumberCompressor.ts b/src/adapters/TinyNumberCompressor.ts index 0c4d0b0..4f09ced 100644 --- a/src/adapters/TinyNumberCompressor.ts +++ b/src/adapters/TinyNumberCompressor.ts @@ -13,6 +13,20 @@ export class TinyNumberCompressor implements IAdapter { return (36 ** numOfChars) - 1; } + private static leftStart(text: string, targetLength: number, padString: string = ""): string { + // tslint:disable-next-line:no-bitwise + targetLength = targetLength >> 0; // truncate if number, or convert non-number to 0; + if (this.length >= targetLength) { + return String(text); + } else { + targetLength = targetLength - text.length; + if (targetLength > padString.length) { + padString += padString.repeat(targetLength / padString.length); // append to original to ensure we are longer than needed + } + return padString.slice(0, targetLength) + String(text); + } + } + public deserialize(text: string, schema: ISchema): IObject[] { const charLengthForSchema = this.getCharLengthForSchema(schema); if (text.length % charLengthForSchema !== 0) { @@ -44,7 +58,8 @@ export class TinyNumberCompressor implements IAdapter { private encodeObject(object: IObject): string { return Object.keys(object).sort().map((x) => { const length = this.keyLengthFactory(x); - return (object[x] as number).toString(36).padStart(length, "0"); + const str = (object[x] as number).toString(36); + return TinyNumberCompressor.leftStart(str, length, "0"); }).join(""); } From c0e8368f408f8df6abad15ab799f7a21febe4d54 Mon Sep 17 00:00:00 2001 From: Nishchal Gautam Date: Thu, 12 Dec 2019 14:29:23 +0700 Subject: [PATCH 2/3] style(TinyNumberCompressor): rename method --- src/adapters/TinyNumberCompressor.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/adapters/TinyNumberCompressor.ts b/src/adapters/TinyNumberCompressor.ts index 4f09ced..dcb5089 100644 --- a/src/adapters/TinyNumberCompressor.ts +++ b/src/adapters/TinyNumberCompressor.ts @@ -13,7 +13,7 @@ export class TinyNumberCompressor implements IAdapter { return (36 ** numOfChars) - 1; } - private static leftStart(text: string, targetLength: number, padString: string = ""): string { + public static leftPad(text: string, targetLength: number, padString: string = ""): string { // tslint:disable-next-line:no-bitwise targetLength = targetLength >> 0; // truncate if number, or convert non-number to 0; if (this.length >= targetLength) { @@ -59,7 +59,7 @@ export class TinyNumberCompressor implements IAdapter { return Object.keys(object).sort().map((x) => { const length = this.keyLengthFactory(x); const str = (object[x] as number).toString(36); - return TinyNumberCompressor.leftStart(str, length, "0"); + return TinyNumberCompressor.leftPad(str, length, "0"); }).join(""); } From 18fdc970f62f973af8f47ee3b1dd66dcbda46d84 Mon Sep 17 00:00:00 2001 From: Nishchal Gautam Date: Thu, 12 Dec 2019 14:30:13 +0700 Subject: [PATCH 3/3] test(TinyNumberCompressor): add test --- src/adapters/TinyNumberCompressor.spec.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/adapters/TinyNumberCompressor.spec.ts b/src/adapters/TinyNumberCompressor.spec.ts index 700e3fc..28c0966 100644 --- a/src/adapters/TinyNumberCompressor.spec.ts +++ b/src/adapters/TinyNumberCompressor.spec.ts @@ -2,6 +2,10 @@ import {ValueType} from "../Searilie"; import {TinyNumberCompressor} from "./TinyNumberCompressor"; describe("TinyNumberCompressor", () => { + it("PadStart should work ", () => { + expect(TinyNumberCompressor.leftPad("0", 3, "a")).toBe("aa0"); + }); + it("should be defined", () => { expect(TinyNumberCompressor).toBeDefined(); });