Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import { ObjectModel } from "objectmodel";
const Person = ObjectModel({
name: String,
female: Boolean
});
const Mother = Person.extend({
female: true,
child: Person
});
let joe = new Person({ name: "Joe", female: false });
let ann = new Person({ name: "Ann", female: true });
let joanna = new Person({ name: "Joanna", female: true });
ann = new Mother({ name: "Ann", female: true, child: joanna });
console.log(ann instanceof Mother && ann instanceof Person); // true
import { ObjectModel } from "objectmodel";
const Animation = new ObjectModel({
// can be a Number or a String
delay: [Number, String],
// optional property which can be a Boolean or a String
easing: [Boolean, String, undefined]
});
const opening = new Animation({ delay: 300 }); // easing is optional
opening.delay = "fast"; // String is a valid type
opening.delay = null;
// TypeError: expecting delay to be Number or String, got null
opening.easing = true; // Boolean is a valid type
opening.easing = 1;
// TypeError: expecting easing to be Boolean or String or undefined, got Number 1
const Person = ObjectModel({
name: String,
female: Boolean
});
const Mother = Person.extend({
female: true,
child: Person
});
const Father = Person.extend({
female: false,
child: Person
});
const Family = ObjectModel({
father: Father,
mother: Mother,
children: ArrayModel(Person), // array of Persons
grandparents: [ArrayModel([Mother, Father])]
// optional array of Mothers or Fathers
});
let joe = new Person({ name: "Joe", female: false });
let ann = new Person({ name: "Ann", female: true });
let joanna = new Person({ name: "Joanna", female: true });
const joefamily = new Family({
father: joe,
mother: ann,
children: [joanna, "dog"]
});
import { ObjectModel } from "objectmodel";
const User = ObjectModel({
email: String, // mandatory
name: [String] // optional
});
const Person = ObjectModel({
name: String,
female: Boolean
});
const Order = ObjectModel({
product: {
name: String,
quantity: Number
},
orderDate: Date
});
const Client = Person.extend(User, Order, { store: String });
Client.prototype.sendConfirmationMail = function() {
return (
import { ObjectModel } from "objectmodel";
const User = ObjectModel({
email: String, // mandatory
name: [String] // optional
});
const stan = User({ email: "stan@smith.com" }); // no exceptions
const roger = User({ name: "Roger" }); // email is mandatory
// TypeError: expecting email to be String, got undefined
import { Model, ObjectModel } from "objectmodel";
const Circle = ObjectModel({
radius: Number, // public
_index: Number, // private
UNIT: ["px", "cm"], // constant
_ID: [Number] // private and constant
}).defaultTo({
_index: 0,
getIndex() {
return this._index;
},
setIndex(value) {
this._index = value;
}
});
let c = new Circle({ radius: 120, UNIT: "px", _ID: 1 });
c.radius = 100;
import { ObjectModel, FunctionModel } from "objectmodel";
const Person = ObjectModel({
name: String,
// function without arguments returning a String
sayMyName: FunctionModel().return(String)
}).defaultTo({
sayMyName: function() {
return "my name is " + this.name;
}
});
// takes one Person as argument, returns a String
Person.prototype.greet = FunctionModel(Person).return(String)(function(
otherguy
) {
return "Hello " + otherguy.name + ", " + this.sayMyName();
});
import { ObjectModel } from "objectmodel";
const User = ObjectModel({
email: String, // mandatory
name: [String] // optional
});
const Person = ObjectModel({
name: String,
female: Boolean
});
const Order = ObjectModel({
product: {
name: String,
quantity: Number
},
orderDate: Date
});
import { ArrayModel, ObjectModel } from "objectmodel";
const Person = ObjectModel({
name: String,
female: Boolean
});
const Mother = Person.extend({
female: true,
child: Person
});
const Father = Person.extend({
female: false,
child: Person
});
const Family = ObjectModel({
father: Father,
import { ObjectModel } from "objectmodel";
class Person extends ObjectModel({ name: String, female: Boolean }) {
constructor({ name, female }) {
if (!female) name = `Mr ${name}`;
super({ name, female });
}
}
class Mother extends Person.extend({ female: true, child: Person }) {
constructor({ name, female = true, child }) {
super({ name: `Mama ${name}`, female, child });
}
}
let joe = new Person({ name: "Joe", female: false });
let joanna = new Person({ name: "Joanna", female: true });
let ann = new Mother({ name: "Ann", child: joanna });