Skip to content

Instantly share code, notes, and snippets.

@GCheung55
Forked from anonymous/_.js
Created December 27, 2012 22:44
Show Gist options
  • Save GCheung55/4392804 to your computer and use it in GitHub Desktop.
Save GCheung55/4392804 to your computer and use it in GitHub Desktop.
"use strict";
var prime = require('prime')
var ghost = require('prime/util/ghost')
var typeOf = require('prime/util/type')
var shells = {
"string" : require("prime/types/string"),
"number" : require("prime/types/number"),
"array" : require("prime/collection/list"),
"object" : require("prime/collection/hash"),
//"date" : require("prime/es5/date"),
"function" : require("prime/es5/function"),
"regexp" : require("prime/es5/regexp")
}
module.exports = function(typeCheck){
if (!typeCheck) typeCheck = typeOf
var _ = function(self){
if (self == null || self instanceof lodash) return self
var l = _[typeCheck(self)]
return l ? new l(self) : self
}
var lodash = prime({
mutator: function(key, method){
this.prototype[key] = function(){
// this is basically the only difference, ghost wraps it in ƒ while _ directly returns the result.
return method.apply(this.valueOf(), arguments)
}
}
})
_.chain = function(obj){
return ghost(obj);
}
_.register = function(type, shell){
var l = _[type] = prime({
inherits: lodash,
constructor: function(self){
if (!this || this.constructor !== l) return new l(self)
this.valueOf = function(){
return self
}
}
})
l.implement(shell.prototype)
}
for (var type in shells) _.register(type, shells[type])
return _
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment