/
index.js
60 lines (43 loc) · 1.56 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
var isObject = require('lodash.isObject')
var Object = require('./object');
module.exports = Type
var type_defaults = {
'type': 'type',
}
// A Type is one of the two Transformer objects.
// Types are descriptions of kinds of objects. They uniquely identify
// a "kind", "type", or "category" of data. If raw data is just a bunch of
// bytes, Types give that data semantic meaning.
// (e.g. number, text, date, email-address, phone-number, geometry)
// Types are very simple, expressed as json-ld documents that giving the type
// a simple, meaningful name.
// Types are the way we give data semantic meaning. Types are related (linked)
// by Conversions. Transformer leverages this to simplify transforming one
// stream of bytes into another.
function Type(src) {
if (src instanceof Type)
return src;
if (!(this instanceof Type))
return new Type(src);
// if it has been seen before, return that.
if (src && Type.all[src])
return Type.all[src];
if (src && src.id && Type.all[src.id])
return Type.all[src.id];
// if not a full type, just schema, wrap it.
if (isObject(src) && !src['schema'])
src = {'schema': src};
// if without a description, use id
src.description = src.description || src.id;
this.src = Object(src, type_defaults);
this.type = Type;
}
Type.all = {};
Type.check = function typeCheck(typeA, typeB) {
if (!typeB) // ensuring typeA is a Type.
return typeA instanceof Type;
// for now, ensure they're the same thing. handle abstraction later.
return typeA === typeB;
};
// export object
Type.Object = Object