From 0a56624693ae2153bf6fc4c125d1b6eedff5759b Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Tue, 23 Feb 2021 11:47:48 -0800 Subject: [PATCH] fix: node 6 compatibility, take 2 (#353) (#355) --- src/parseToRgba.ts | 2 +- src/toHex.ts | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/parseToRgba.ts b/src/parseToRgba.ts index 70df320b..860a75be 100644 --- a/src/parseToRgba.ts +++ b/src/parseToRgba.ts @@ -73,7 +73,7 @@ const compressedColorMap = '1q29ehhb 1n09sgk7 1kl1ekf_ _yl4zsno 16z9eiv3 1p29lhp const key = colorToInt(next.substring(0, 3)); const hex = colorToInt(next.substring(3)).toString(16); - // NOTE: pad start could be used here but it breaks Node 6 compat + // NOTE: padStart could be used here but it breaks Node 6 compat // https://github.com/ricokahler/color2k/issues/351 let prefix = ''; for (let i = 0; i < 6 - hex.length; i++) { diff --git a/src/toHex.ts b/src/toHex.ts index e7eadd97..b35acd25 100644 --- a/src/toHex.ts +++ b/src/toHex.ts @@ -6,7 +6,14 @@ import guard from './guard'; */ function toHex(color: string): string { const [r, g, b, a] = parseToRgba(color); - const hex = (x: number) => guard(0, 255, x).toString(16).padStart(2, '0'); + + let hex = (x: number) => { + const h = guard(0, 255, x).toString(16); + // NOTE: padStart could be used here but it breaks Node 6 compat + // https://github.com/ricokahler/color2k/issues/351 + return h.length === 1 ? `0${h}` : h; + }; + return `#${hex(r)}${hex(g)}${hex(b)}${a < 1 ? hex(Math.round(a * 255)) : ''}`; }