You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
1.3 KiB
JavaScript
46 lines
1.3 KiB
JavaScript
"use strict";
|
|
|
|
const isTemporalTime = require("./value-checks/temporal/is-compatible-time");
|
|
const isTemporalDuration = require("./value-checks/temporal/is-duration");
|
|
|
|
// NOTE: We use custom type-checking implementations here (instead of existing libraries) because we want the type names and checks to *exactly* match up with our internal storage rules, and third-party validation libraries typically don't provide such strong guarantees
|
|
module.exports = function getValueType(value) {
|
|
let primitiveType = typeof value;
|
|
|
|
switch (primitiveType) {
|
|
case "string":
|
|
case "boolean":
|
|
case "symbol":
|
|
case "function":
|
|
return primitiveType;
|
|
case "undefined":
|
|
return "null";
|
|
case "number":
|
|
if (Number.isInteger(value)) {
|
|
return "integer";
|
|
} else {
|
|
return "float";
|
|
}
|
|
case "bigint":
|
|
return "bigint";
|
|
default:
|
|
if (value == null) {
|
|
return "null";
|
|
} else if (Buffer.isBuffer(value)) {
|
|
// FIXME: Utf8Array and friends?
|
|
return "bytes";
|
|
} else if (Array.isArray(value)) {
|
|
return "array";
|
|
} else if (value instanceof Date) {
|
|
return "date";
|
|
} else if (isTemporalTime(value)) {
|
|
return "temporalTime";
|
|
} else if (isTemporalDuration(value)) {
|
|
// FIXME: Balancing is needed for these, prior to storage!
|
|
return "temporalDuration";
|
|
} else {
|
|
return "object";
|
|
}
|
|
}
|
|
};
|