Skip to content

Instantly share code, notes, and snippets.

@sakunyo
Last active January 18, 2017 15:37
Show Gist options
  • Save sakunyo/01d5b7573bf0ae47d384d1811a940982 to your computer and use it in GitHub Desktop.
Save sakunyo/01d5b7573bf0ae47d384d1811a940982 to your computer and use it in GitHub Desktop.
Assertion or refactoring.
// Define namespace.
var app = {};
(function (app) {
/**
* Closure pattern.
* See also Functional Programming.
*/
app.foo = function (after) {
return function (txt = "") {
return txt.replace(/(。)/g, "$1" + after);
};
};
var _ = app.foo; // Assign reference
var a = _("<br class='is-show'>"); // Make Function.
var b = _("<br class='is-hide'>"); // Make Function.
var c = _("<br class='is-close'>"); // Make Function.
var d = c(b(a("My string。")));
})(app);
(function (app) {
/**
* Singleton object pattern.
* See also OOP.
*/
app.bar = {
text2: function (txt = "", after) {
return txt.replace(/(。)/g, "$1" + after);
},
text2A: function (txt) {
return this.text2(txt, "<br class='is-show'>");
},
text2B: function (txt) {
return this.text2(txt, "<br class='is-hide'>");
}
};
app.bar.text2A(undefined);
app.bar.text2A("My string。");
app.bar.text2B("My string。");
})(app);
(function (app) {
var fixture = app.foo("<br class='is-hide'>");
/**
* Assertion
* console.assert(true) is ok
* console.assert(false) is ng
*/
console.assert(fixture("hoge。hoge") === "hoge。<br class='is-hide'>hoge");
// if (window.matchMedia('(max-width: 768px)').matches) {
// var $textBox = $("#text-box");
// $textBox.html(a($textBox.html()));
// }
})(app);
function a(n) {
return n ? " " + n : "";
}
// Assertion
console.assert(a() === "");
console.assert(a("foo") === " foo");
class Foo {
constructor() {
}
say(name) {
return this.hello(name);
}
hello(name) {
if (a(name)) {
return "Hello" + a(name);
}
return "empty";
}
}
// Assertion
const foo = new Foo();
console.assert(foo.say("world") === "Hello world");
console.assert(foo.say() === "empty");
console.assert(foo.hello("world") === "Hello world");
console.assert(foo.hello() === "empty");
<html>
<head>
<meta charset="utf-8">
<title>app.js</title>
</head>
<body>
<script src="app.js"></script>
<script src="loop.js"></script>
<script src="assertion.js"></script>
</body>
</html>
const arr = [1, 2, 3, 4, 5];
{
/**
* Loop while. reverse.
*/
let len = arr.length;
while(len--) {
console.log(arr[len]);
}
}
{
/**
* Loop for.
*/
for (let i = 0; i < arr.length; i++) {
console.log(arr[i]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment