Skip to content

Instantly share code, notes, and snippets.

@jonohayon
Last active July 9, 2016 10:32
Show Gist options
  • Save jonohayon/97eaf89f355f7881a502a8dc58b41502 to your computer and use it in GitHub Desktop.
Save jonohayon/97eaf89f355f7881a502a8dc58b41502 to your computer and use it in GitHub Desktop.
BoreJS Specs

#BoreJS I was pretty bored, so I decided to build this altJS language using the Canopy parser.

  1. Annotation syntax
@export
class Lmao { /*body...*/ }
@private var a = 'ayyy' // When exported and is outside of a class (using module.exports), will throw an error. If inside a class, will act like regular private/public inside of classes
@public var a = 'lmao' // @public is the same as @export. If inside a class, will act like regular private/public inside of classes
  1. Validation helpers for classes
class StringArray extends Array {
  @validate p.is(String) || p.toString() // just boolean checking
  push (val) {
    return val
  }
}
class DB extends MongoConnection {
  @validate p => Schema.check(p)  // or using a custom function
  createObject (val) { // val is now a proper Mongo Schema (and not a regular object)
    return this.createDoc(val).then(doc => {
      console.log(`Created a document with ObjectID ${doc._id}`)
      return Promise.resolve(doc)
    })
  }
}
  1. Type checking and casting (like in Swift)
let strArr: StringArray = [1, 2, 3, 4, 5] as StringArray // Child of Array, still conforms to that type
let sumArr = (arr: Array) => { // arr can be any object that inherits the Array properties
  return arr.is(StringArray): arr.reduce((prev, curr) => prev + curr)
}
console.log(sumArr(strArr)) // throws an error: strArr won't conform to !StringArray needed type
console.log(sumArr(strArr as! Array)) // logs [1,2,3,4,5]
  1. Operator Overloading
@export
class GeoPoint extends Point {
  operator+ (toAdd) {
    return toAdd.is(Point)? new GeoPoint(this.x + toAdd.x, this.y + toAdd.y) 
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment