Skip to content

Instantly share code, notes, and snippets.

@WillsonSmith
Last active January 24, 2016 01:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WillsonSmith/d46a81496f9536868fe6 to your computer and use it in GitHub Desktop.
Save WillsonSmith/d46a81496f9536868fe6 to your computer and use it in GitHub Desktop.
implementations of some string stuff
(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