Last active
January 24, 2016 01:28
-
-
Save WillsonSmith/d46a81496f9536868fe6 to your computer and use it in GitHub Desktop.
implementations of some string stuff
This file contains 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() { | |
function compose(...funcs) { | |
return function (value) { | |
return funcs.reverse().reduce(function(previous, current) { | |
return previous.call(this, current(value)); | |
}); | |
}; | |
} | |
function shout(string) { | |
return `${string}!`; | |
} | |
function upperCase(string) { | |
return string.toUpperCase(); | |
} | |
let composed = compose(upperCase, shout); | |
console.log(composed('hi')); | |
})(); | |
//////////////////////////////////////////////// | |
class stringShouter { | |
constructor(string) { | |
this.string = string; | |
} | |
shout() { | |
this.string = `${this.string}!`; | |
return this; | |
} | |
upperCase() { | |
this.string = this.string.toUpperCase(); | |
return this; | |
} | |
getvalue() { | |
return this.string; | |
} | |
} | |
let shoutIt = new stringShouter('hi'); | |
console.log(shoutIt.upperCase().shout().getvalue()); | |
//////////////////////////////////////////////// | |
let objStringShouter = function(string) { | |
return { | |
shout() { | |
string = `${string}!`; | |
return this; | |
}, | |
upperCase() { | |
string = string.toUpperCase(); | |
return this; | |
}, | |
value() { | |
return string; | |
} | |
}; | |
}; | |
let objShoutIt = objStringShouter('hi'); | |
console.log(objShoutIt.upperCase().shout().value()); | |
//////////////////////////////////////////////// | |
function Shouter(string) { | |
this.string = string; | |
this.shout = function() { | |
this.string = `${this.string}!`; | |
return this; | |
} | |
this.upperCase = function() { | |
this.string = this.string.toUpperCase(); | |
return this; | |
} | |
this.getvalue = function() { | |
return this.string; | |
} | |
} | |
var shoutHi = new Shouter('hi'); | |
console.log(shoutHi.upperCase().shout().getvalue()); | |
//////////////////////////////////////////////// | |
var ObjectShout = { | |
shout() { | |
this.string = `${this.string}!`; | |
return this; | |
}, | |
upperCase() { | |
this.string = this.string.toUpperCase(); | |
return this; | |
}, | |
value() { | |
return this.string; | |
} | |
} | |
var Shouting = Object.create(ObjectShout); | |
Shouting.string = "hi"; | |
console.log(Shouting.upperCase().shout().value()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment