Skip to content

Instantly share code, notes, and snippets.

@assertchris
Created May 2, 2012 12:12
Show Gist options
  • Select an option

  • Save assertchris/2576151 to your computer and use it in GitHub Desktop.

Select an option

Save assertchris/2576151 to your computer and use it in GitHub Desktop.
has = (self, key) ->
Object.hasOwnProperty.call(self, key)
each = (object, method, context) ->
for key in object
if method.call(context, object[key], key, object) == false
break
object
create = Object.create or (self) ->
F = ->
F.prototype = self
new F
mutator = (key, value) ->
@prototype[key] = value
@
implement = (obj) ->
each(obj, (value, key) =>
if key != "constructor" and key != "inherits" and key != "mutator"
@mutator(key, value)
)
@
prime = (proto) ->
superprime = proto.inherits
if superprime
superproto = superprime.prototype
constructor = if has(proto, "constructor") then proto.constructor else
if superprime then ->
superproto.constructor.apply(this, arguments)
else ->
if superprime
cproto = constructor.prototype = create(superproto)
constructor.parent = superproto
cproto.constructor = constructor
constructor.mutator = proto.mutator or (superprime and superprime.mutator) or mutator
constructor.implement = implement
constructor.implement(proto)
prime.each = each
prime.has = has
prime.create = create
module.exports = prime
@ryanflorence
Copy link

has = (self, key) ->
  Object.hasOwnProperty.call self, key

each = (object, method, context) ->
  # this needs an complete overhaul
  # `for key in object` iterates like its an array using object.length and such
  # `for key of object` is what you use for objects, or more often written as
  # `for own key, value of object`

create = Object.create or (self) ->
  F = ->
  F.prototype = self
  new F

mutator = (key, value) ->
  @::[key] = value
  this


implement = (obj) ->
  each obj, (value, key) =>
    if key not in ["constructor", "inherits", "mutator"]
      @mutator key, value
  this

prime = (proto) ->
  superprime = proto.inherits
  superproto = superprime.prototype if superprime
  constructor = if has(proto, "constructor") then proto.constructor else
    if superprime then ->
      superproto.constructor.apply this, arguments
    else ->

  if superprime
    cproto = constructor.prototype = create superproto
    constructor.parent = superproto
    cproto.constructor = constructor

  constructor.mutator = proto.mutator or (superprime and superprime.mutator) or mutator
  constructor.implement = implement

  constructor.implement proto

prime.each = each
prime.has = has
prime.create = create

module.exports = prime

@assertchris
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment