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.

148 lines
2.8 KiB
JavaScript

6 years ago
"use strict";
const s = require("../");
let PersonName = s.createType("PersonName", s.string({
matches: /^[a-z0-9 '-]+$/
}));
let Nickname = s.createType("Nickname", s.string({
matches: /^[a-z0-9_]+$/
}));
let EmailAddress = s.createType("EmailAddress", s.string({
matches: /@/
}));
// let OnlineIdentity = s.createType("OnlineIdentity", {
// nickname: Nickname,
// active: s.boolean()
// });
let OnlineIdentity = s.createTrait("OnlineIdentity", {
encode: s.function([], s.string())
});
let NicknameIdentity = s.createType("NicknameIdentity", {
nickname: Nickname
}).implements(OnlineIdentity, {
encode: s.guard([], s.string(), function () {
return `nickname:${this.nickname}`;
})
});
let EmailIdentity = s.createType("EmailIdentity", {
email: EmailAddress
}).implements(OnlineIdentity, {
encode: s.guard([], s.string(), function () {
return `email:${this.email}`;
})
});
let CustomIdentity = s.createType("CustomIdentity", {
}).implements(OnlineIdentity, {
encode: s.slot()
});
let BullshitIdentity = s.createType("BullshitIdentity", {
nickname: Nickname
});
let Person = s.createType("Person", {
name: s.string(),
age: s.number(),
isAlive: s.boolean(),
// identities: s.setOf(OnlineIdentity)
identities: s.mapOf(s.string(), OnlineIdentity),
// friend: s.self().optional(),
friends: s.setOf(s.self()),
getName: function () {
return this.name;
},
compareName: s.guard([s.self()], s.boolean(), function (otherPerson) {
return (otherPerson.name === this.name);
})
});
// MARKER: implement unique paths
let me = Person({
name: "somebody",
age: 42,
isAlive: true,
// identities: new Set([
// OnlineIdentity({
// nickname: "foo",
// active: true
// }),
// OnlineIdentity({
// nickname: "joepie",
// active: false
// })
// ])
friends: new Set(),
identities: new Map([
["foo", EmailIdentity({
email: "foo@bar.baz"
})],
["joepie91", NicknameIdentity({
nickname: "joepie91"
})],
["custom_man", CustomIdentity({
encode: s.guard([], s.string(), function () {
return "custom:foo";
})
})],
])
});
let other = Person({
name: "foo",
age: 21,
isAlive: false,
friends: new Set(),
identities: new Map(),
// friend: me
});
console.log(me.getName(), me.age, me.isAlive, me.identities);
console.log("=====");
for (let [name, identity] of me.identities.entries()) {
console.log(`${name} => ${identity.encode()}`);
}
console.log("=====");
console.log(me.compareName(other));
// let C = s.createType("C", {});
//
// let B = s.createType("B", {
// c: s.setOf(C)
// });
//
// let A = s.createType("A", {
// b: s.mapOf(s.string(), B)
// });
//
// let a = A({
// b: new Map([
// ["1", B({
// c: new Set([
// C({}),
// C({})
// ])
// })],
// ["2", B({
// c: new Set()
// })]
// ])
// });
//
// let a2 = A({
// b: a.b.get("1").c
// // b: "foo"
// });
//
// console.log(a);
// console.log(a2);