Last active
July 17, 2018 11:54
-
-
Save sezemiadmin/2eab1cd9ccf76a5a725a26054ad7a5ee to your computer and use it in GitHub Desktop.
Modern JavaScript 環境構築編のサンプルコード
This file contains hidden or 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
module.exports = class Calc { | |
static sum(a, b) { | |
return a + b; | |
} | |
static multiply(a, b) { | |
return a * b; | |
} | |
} |
This file contains hidden or 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 multiply(n,u){return n*u}function sum(n,u){return n+u} |
This file contains hidden or 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
var assert = require('assert'); | |
var calc = require('../scripts/calc.js'); | |
// Behaviorで駆動。decribe関数の中にテスト内容を記述する | |
describe('calc', function() { | |
// function() は無名関数の書き方 | |
describe('#sum()', function() { | |
/* | |
- 実際に実行するものを書く | |
- クラスを書いてメソッド毎に書く | |
- => はラムダ式の描き方 | |
- it関数のあとにテストケースを書く | |
- テストケースにはどんな値を渡したらどんな値が返ってくるのかを書く*/ | |
it('should return 3 when 1 + 2', () => { | |
const actual = calc.sum(1, 2); | |
// assertion(表明という意味) メソッドで判定する | |
assert.equal(actual, 3); | |
}); | |
}); | |
describe('#multiply()', function() { | |
it('should return 6 when 2 * 3', () => { | |
const actual = calc.multiply(2, 3); | |
assert.equal(actual, 6); | |
}); | |
}); | |
}); |
This file contains hidden or 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
C:\mocha-lesson>npm test | |
> [email protected] test C:\mocha-lesson | |
> mocha js/test | |
calc | |
#sum() | |
√ should return 3 when 1 + 2 | |
#multiply() | |
√ should return 6 when 2 * 3 | |
2 passing (32ms) |
This file contains hidden or 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
// module はrequireされる側が書く、モジュール分割の構文 | |
module.exports = class Calc { | |
// 仮の引数や戻り値を書く | |
static sum(a, b) { | |
return 0; | |
} | |
// 仮の引数や戻り値を書く | |
static multiply(a, b) { | |
return 0; | |
} | |
} |
This file contains hidden or 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
C:\gulp-lesson>gulp | |
[12:25:20] Using gulpfile C:\gulp-lesson\gulpfile.js | |
[12:25:20] Starting 'default'... | |
[12:25:20] Starting 'scripts'... | |
[12:25:20] Finished 'default' after 38 ms | |
[12:25:21] Finished 'scripts' after 420 ms |
This file contains hidden or 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
// require から書く | |
var gulp = require('gulp'), | |
concat = require('gulp-concat'), | |
uglify = require('gulp-uglify'), | |
rename = require('gulp-rename'); | |
// scripts で分けてタスクを書くこともできる | |
gulp.task('default', () => { | |
gulp.start('scripts'); | |
}); | |
gulp.task('scripts', () => { | |
return gulp.src('src/scripts/**/*.js') | |
// pipeメソッドでタスクを繋いでいく | |
.pipe(concat('calc.js')) | |
.pipe(gulp.dest('dist/assets/js')) | |
.pipe(rename({suffix: '.min'})) | |
.pipe(uglify()) | |
.pipe(gulp.dest('dist/assets/js')); | |
}); |
This file contains hidden or 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
// require 関数(標準のライブラリ)でモジュールの読み込み | |
const http = require('http'); | |
// ホスト名とポート名を指定 | |
const hostname = '127.0.0.1'; | |
const port = 3000; | |
// createServer関数で立ち上げて引数をラムダ式 (req, res) で記述 (無名関数から変わった) | |
const server = http.createServer((req, res) => { | |
res.statusCode = 200; | |
res.setHeader('Content-Type', 'text/plain'); | |
res.end('Hello Node.js!\n'); | |
}); | |
// クライアントからのリクエストを待ち受けを行う listen | |
server.listen(port, hostname, () => { | |
console.log(`Server running at http://${hostname}:${port}/`); | |
}); |
This file contains hidden or 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
// require 関数で使う | |
var _ = require('lodash'); | |
// console でコンソール出力 | |
console.log(_.without([1, 2, 3], 1)); |
This file contains hidden or 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
var gulp = require('gulp'); | |
// 引数にタスク名 default を書いて実際にタスクを書きます | |
gulp.task('default', () => { | |
// place code for your default task here | |
}); |
This file contains hidden or 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
{ | |
"name": "npm-lesson", | |
"version": "1.0.0", | |
"lockfileVersion": 1, | |
"requires": true, | |
"dependencies": { | |
"lodash": { | |
"version": "4.17.10", | |
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.10.tgz", | |
"integrity": "sha512-UejweD1pDoXu+AD825lWwp4ZGtSwgnpZxb3JDViD7StjQz+Nb/6l093lx4OQ0foGWNRoc19mWy7BzL+UAK2iVg==" | |
} | |
} | |
} |
This file contains hidden or 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
{ | |
"name": "npm-lesson", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "", | |
"license": "ISC" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment