-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
v0.5.1
- Loading branch information
Showing
9 changed files
with
134 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/** Normalizes any input that might represent a hex color. Returns undefined if no match was found */ | ||
export declare function normalizeHexColor(color: string): string; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
// accept a bunch of stuff that follows 2 rules | ||
// 1. ends with 6 hex digits | ||
// 2. the remainder does not contain letters or digits | ||
const hexColorRegex = /^[^a-zA-Z0-9]*([a-fA-F0-9]{6})$/; | ||
/** Normalizes any input that might represent a hex color. Returns undefined if no match was found */ | ||
function normalizeHexColor(color) { | ||
const match = hexColorRegex.exec(color); | ||
if (match && match.length > 1) | ||
return match[1]; | ||
} | ||
exports.normalizeHexColor = normalizeHexColor; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { expect } from "chai"; | ||
import { normalizeHexColor } from "./colors"; | ||
// tslint:disable:no-unused-expression | ||
|
||
describe.only("lib/colors => normalizeHexColor() =>", () => { | ||
|
||
it("should not change valid hex colors without #", () => { | ||
const sampleColors = [ | ||
"000000", "0a0a0a", "A5A5A5", "FFEEDD", "abcdef", | ||
]; | ||
for (const hex of sampleColors) { | ||
expect(normalizeHexColor(hex)).to.equal(hex); | ||
} | ||
}); | ||
|
||
it("should strip the prefix off strings that might represent hex colors", () => { | ||
const sampleColors = [ | ||
"#000000", ".,-0a0a0a", "####A5A5A5", "%FFEEDD", "$$$abcdef", | ||
]; | ||
for (const hex of sampleColors) { | ||
expect(normalizeHexColor(hex)).to.equal(hex.substr(-6)); | ||
} | ||
}); | ||
|
||
it("should return undefined for stuff that's not almost a hex string", () => { | ||
const sampleColors = [ | ||
"A000000", "Z0a0a0a", "6A5A5A5", "aFFEEDD", "a_abcdef", | ||
]; | ||
for (const hex of sampleColors) { | ||
expect(normalizeHexColor(hex)).to.be.undefined; | ||
} | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// accept a bunch of stuff that follows 2 rules | ||
// 1. ends with 6 hex digits | ||
// 2. the remainder does not contain letters or digits | ||
const hexColorRegex = /^[^a-zA-Z0-9]*([a-fA-F0-9]{6})$/; | ||
/** Normalizes any input that might represent a hex color. Returns undefined if no match was found */ | ||
export function normalizeHexColor(color: string): string { | ||
const match = hexColorRegex.exec(color); | ||
if (match && match.length > 1) return match[1]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters