Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion lib/scheme.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ function reverseHex(hex) {
return `${m[3]}${m[2]}${m[1]}`;
}

// all other keys are assumed to be base00 colors or aliases
RESERVED_KEYS = [
"scheme",
"author",
]

const MAX_ALIAS_DEPTH = 10
const HEXCOLOR = /^([0-9A-F]){6}$/i
class AmbiguousAlias extends Error {}
class AliasRecursionOverflow extends Error {}

/**
* Represents a Base16 scheme definition.
*/
Expand All @@ -41,9 +52,29 @@ class Scheme {
* for use with Base16 mustache templates.
*/
parseScheme (data) {
const resolveAliases = (key, data) => {
let maxdepth = MAX_ALIAS_DEPTH;

let alias = data[key];
while (maxdepth--) {
if (!data[alias]) { return };

if (HEXCOLOR.test(alias)) {
let err = `Alias '${alias}' is ambiguous (could also be a hexcolor) and not allowed.`
throw new AmbiguousAlias(err);
}

data[key] = data[alias]
alias = data[alias]
}

throw new AliasRecursionOverflow(`Key ${key} led to alias recursion overflow.`)
}
const hexDefinitions = Object.keys(data).reduce((accumulator, key) => {
resolveAliases(key, data);

// we're only interested in the hex bases at this point
if (!/^base/i.test(key)) return accumulator
if (RESERVED_KEYS.includes(key)) return accumulator;

const hex = data[key]
// strip leading # if present for 0.10.0 spec
Expand Down