This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function parse_content(cursor) { | |
| let run = true; | |
| while (run && input.length > 0) { | |
| // Parse an open tag | |
| const success = pull(/^<([a-zA-Z][a-zA-Z0-9\-]*)>/, tag => { | |
| const new_tag = { tag, attributes: {}, children: [] }; | |
| cursor.children.push(new_tag); | |
| parse_content(new_tag); | |
| }) || | |
| // Parse close tag |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export default function parse_html(input) { | |
| const root = { children: [] }; | |
| function pull(regex, handler = () => {}) { | |
| const match = regex.exec(input); | |
| if (match !== null) { | |
| const [full_match, ...captures] = match; | |
| input = input.substr(full_match.length); | |
| handler(...captures); | |
| return true; | |
| } else { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Car { | |
| get [Drivable]() { | |
| return { | |
| x: 0, | |
| y: 0, | |
| steer(direction, throttle) { | |
| this.x += throttle * direction.x; | |
| this.y += throttle * direction.y; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const Undead = new Trait("Neither fully alive nor fully dead."); | |
| const AI = new Trait("Has an AI script"); | |
| function implement_ai(base) { | |
| let ai_impl; | |
| if (base instanceof Undead) { | |
| ai_impl = { | |
| ai_tick() { | |
| for (const creature of this.source.surroundings()) { | |
| // Find living things in the surounding and eat them with a preference for brains... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const Sayable = new Trait("Can be made to say something."); | |
| const Drivable = new Trait("Can be driven."); | |
| class Cow { | |
| [Sayable]() { | |
| return "Eat Mor ChiKin"; | |
| } | |
| } | |
| class Horse { | |
| [Sayable]() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export default class Trait { | |
| constructor(description) { | |
| this.symbol = Symbol(description); | |
| } | |
| [Symbol.toPrimitive]() { | |
| return this.symbol; | |
| } | |
| [Symbol.hasInstance](target) { | |
| return typeof target == 'object' && target[this.symbol] !== undefined; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Foo { | |
| a = 5; | |
| next() { | |
| return false; // Doesn't implement the iterator protocol | |
| } | |
| [Symbol.iterator]() { | |
| const self = this; | |
| return { | |
| i: 0, | |
| next() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Fib { | |
| a = 0; | |
| b = 0; | |
| next() { | |
| if (this.a == 0) { | |
| this.a = 1; | |
| return { value: 1, done: false }; | |
| } | |
| if (this.b == 0) { | |
| this.b = 1; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export function Base_extend(inherit = HTMLElement) { | |
| return class Base extends inherit { | |
| abortController = new AbortController() | |
| constructor() { | |
| super(); | |
| // Construct the shadow DOM | |
| this.attachShadow({mode: 'open'}); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function* testHelper() { | |
| console.log('Entering Helper'); | |
| yield delay(1000); // State H-1 | |
| console.log('Helper: Transition 1'); | |
| yield delay(1000); // State H-2 | |
| console.log('Helper: Transition 2'); | |
| return 5; | |
| } | |
| function* test() { |