clickable-json
Version:
Interactive JSON and JSON CRDT viewer and editor
86 lines • 2.93 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.typeahead = exports.inputStyle = exports.inputColor = exports.valueBg = exports.valueColor = void 0;
const nano_theme_1 = require("nano-theme");
const css = require("../css");
const valueColor = (isDark, value) => {
switch (typeof value) {
case 'boolean':
return css.ValueColor.bool[~~isDark];
case 'string':
return css.ValueColor.str[~~isDark];
case 'number':
return !value
? css.ValueColor.zero[~~isDark]
: value === Math.round(value)
? css.ValueColor.num[~~isDark]
: css.ValueColor.float[~~isDark];
case 'bigint':
return css.ValueColor.float[~~isDark];
case 'object':
return value === null ? css.ValueColor.nil[~~isDark] : undefined;
case 'undefined':
return css.ValueColor.undef[~~isDark];
}
return;
};
exports.valueColor = valueColor;
const valueBg = (value) => {
return value === false || (typeof value === 'number' && value < 0) ? nano_theme_1.theme.red(0.06) : undefined;
};
exports.valueBg = valueBg;
const inputColor = (isDark, input) => {
if (!input)
return;
input = input.trim();
if (input === 'true' || input === 'false')
return css.ValueColor.bool[~~isDark];
if (input === 'null')
return css.ValueColor.nil[~~isDark];
if (input === 'undefined')
return css.ValueColor.undef[~~isDark];
if (input === '0')
return css.ValueColor.zero[~~isDark];
if (input.length < 24) {
if (input[0] === '-' || (input[0] >= '0' && input[0] <= '9')) {
try {
const parsed = JSON.parse(input);
if (typeof parsed === 'number') {
if (parsed === Math.round(parsed))
return css.ValueColor.num[~~isDark];
else
return css.ValueColor.float[~~isDark];
}
}
catch (_a) { }
}
}
if (input[0] === '[' || input[0] === '{')
return undefined;
return css.ValueColor.str[~~isDark];
};
exports.inputColor = inputColor;
const inputStyle = (theme, isDark, input) => {
return {
color: (0, exports.inputColor)(isDark, input) || theme.g(0.1),
background: theme.bg,
borderColor: theme.g(0.7),
};
};
exports.inputStyle = inputStyle;
const typeahead = (value) => {
if (!value)
return '';
const length = value.length;
if (length >= 5)
return '';
if ('true'.startsWith(value))
return 'true'.slice(length);
if ('false'.startsWith(value))
return 'false'.slice(length);
if ('null'.startsWith(value))
return 'null'.slice(length);
return '';
};
exports.typeahead = typeahead;
//# sourceMappingURL=utils.js.map
;