aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/shared/commands/properties.js31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/shared/commands/properties.js b/src/shared/commands/properties.js
new file mode 100644
index 0000000..8a3213d
--- /dev/null
+++ b/src/shared/commands/properties.js
@@ -0,0 +1,31 @@
+const mustNumber = (v) => {
+ let num = Number(v);
+ if (isNaN(num)) {
+ throw new Error('Not number: ' + v);
+ }
+ return num;
+};
+
+const parseProperty = (word, types) => {
+ let [key, value] = word.split('=');
+ if (!value) {
+ value = !key.startsWith('no');
+ key = value ? key : key.slice(2);
+ }
+ let type = types[key];
+ if (!type) {
+ throw new Error('Unknown property: ' + key);
+ }
+ if (type === 'boolean' && typeof value !== 'boolean' ||
+ type !== 'boolean' && typeof value === 'boolean') {
+ throw new Error('Invalid argument: ' + word);
+ }
+
+ switch (type) {
+ case 'string': return [key, value];
+ case 'number': return [key, mustNumber(value)];
+ case 'boolean': return [key, value];
+ }
+};
+
+export { parseProperty };