ember-legacy-class-transform
Version:
The default blueprint for ember-cli addons.
63 lines • 1.59 kB
JavaScript
import { ensureGuid } from './guid';
let proto = Object.create(null, {
// without this, we will always still end up with (new
// EmptyObject()).constructor === Object
constructor: {
value: undefined,
enumerable: false,
writable: true
}
});
function EmptyObject() {}
EmptyObject.prototype = proto;
export function dict() {
// let d = Object.create(null);
// d.x = 1;
// delete d.x;
// return d;
return new EmptyObject();
}
export class DictSet {
constructor() {
this.dict = dict();
}
add(obj) {
if (typeof obj === 'string') this.dict[obj] = obj;else this.dict[ensureGuid(obj)] = obj;
return this;
}
delete(obj) {
if (typeof obj === 'string') delete this.dict[obj];else if (obj._guid) delete this.dict[obj._guid];
}
forEach(callback) {
let { dict } = this;
let dictKeys = Object.keys(dict);
for (let i = 0; dictKeys.length; i++) {
callback(dict[dictKeys[i]]);
}
}
toArray() {
return Object.keys(this.dict);
}
}
export class Stack {
constructor() {
this.stack = [];
this.current = null;
}
toArray() {
return this.stack;
}
push(item) {
this.current = item;
this.stack.push(item);
}
pop() {
let item = this.stack.pop();
let len = this.stack.length;
this.current = len === 0 ? null : this.stack[len - 1];
return item === undefined ? null : item;
}
isEmpty() {
return this.stack.length === 0;
}
}