UNPKG

2.18 kBJavaScriptView Raw
1
2var Type = require('./type');
3var Val = {};
4Val.is = function(v){ // Valid values are a subset of JSON: null, binary, number (!Infinity), text, or a soul relation. Arrays need special algorithms to handle concurrency, so they are not supported directly. Use an extension that supports them if needed but research their problems first.
5 if(v === u){ return false }
6 if(v === null){ return true } // "deletes", nulling out fields.
7 if(v === Infinity){ return false } // we want this to be, but JSON does not support it, sad face.
8 if(text_is(v) // by "text" we mean strings.
9 || bi_is(v) // by "binary" we mean boolean.
10 || num_is(v)){ // by "number" we mean integers or decimals.
11 return true; // simple values are valid.
12 }
13 return Val.rel.is(v) || false; // is the value a soul relation? Then it is valid and return it. If not, everything else remaining is an invalid data type. Custom extensions can be built on top of these primitives to support other types.
14}
15Val.rel = {_: '#'};
16;(function(){
17 Val.rel.is = function(v){ // this defines whether an object is a soul relation or not, they look like this: {'#': 'UUID'}
18 if(v && v[rel_] && !v._ && obj_is(v)){ // must be an object.
19 var o = {};
20 obj_map(v, map, o);
21 if(o.id){ // a valid id was found.
22 return o.id; // yay! Return it.
23 }
24 }
25 return false; // the value was not a valid soul relation.
26 }
27 function map(s, f){ var o = this; // map over the object...
28 if(o.id){ return o.id = false } // if ID is already defined AND we're still looping through the object, it is considered invalid.
29 if(f == rel_ && text_is(s)){ // the field should be '#' and have a text value.
30 o.id = s; // we found the soul!
31 } else {
32 return o.id = false; // if there exists anything else on the object that isn't the soul, then it is considered invalid.
33 }
34 }
35}());
36Val.rel.ify = function(t){ return obj_put({}, rel_, t) } // convert a soul into a relation and return it.
37var rel_ = Val.rel._, u;
38var bi_is = Type.bi.is;
39var num_is = Type.num.is;
40var text_is = Type.text.is;
41var obj = Type.obj, obj_is = obj.is, obj_put = obj.put, obj_map = obj.map;
42module.exports = Val;
43
\No newline at end of file