Simple Guide of Jest
Install1yarn add --dev jest
Simple Case123456// sum.jsfunction sum (a, b) { return a + b}module.exports = sum
12345678// sum.test.jsconst test = require('jest')const expect = require('expect')const sum = require('./sum')test('adds 1 + 2 to equals 3', () => { expect(sum(1, 2).toBe(3))})
With Babel1yarn add --dev babel-jest
12345678910// .babelrc{ "presets": [ [ "env", { "targets": ...
Webpack Code Splitting - Async
Currently a ‘function-like’ import() module loading syntax proposal is on the way into ECMAScript.
The ES6 Defines import() as method to load ES6 modules dynamically onruntime.
Webpack treats import() as a split-point and puts the requested module in a seperate chunk. import() takes the module name as argument and returns a Promise: import(name) => Promise
1234567function determineDate () { import('moment').then((moment) => { console.log(moment().format()) }). ...
Mongodb Simple Guide
yarn add mongoose
import mongoose from 'mongoose'
const db = mongoose.conenct(MONGODB_URI)
1234567891011import mongoose from 'mongoose'const db = mongoose.connect(MONGODB_URI)db.connection.on('open', () => { console.log('connected')})db.connection.on('error', (err) => { console.log(`db error: ${err}`) process.exit})
SchemaSchema defines the skeleton of minimal unit
1234567import mongoose from 'mongo ...
TypeScript With Node
Configuring TypeScript CompilationTypeScript uses the file tsconfig.json to adjust project compile options
123456789101112131415"compilerOptions": { "module": "commonjs", "target": "es6", "noImplicityAny": true, "moduleResolution": "node", "sourceMap": true, "outDir": "dist", "baseUrl": ".", "paths": { "*": [ "node_mo ...
Drag, Upload and Progress
Dragging and dropping files from your desktop to a browser is one of the ultimate goals for web application integration, which consists of:
enable file dragging and dropping onto a web page element
analyze dropped files in JavaScript
load and parse files on the client
asynchronously upload files to the server using XMLHttpRequest2
show a graphical progress bar while the upload occurs
use progressive enhancement to ensure your file upload from works in any browser
The File API
FileList: r ...
Simple Usage of flatMap
Original
Both map() and flatMap() take a function f as a parameter that controls how an input Array is translated to an output Array:
With map(), each input Array element is translated to exactly one output element, aka, f returns a single value
With flatMap(), each input Array element is translated to zero or more output elements, aka, f returns an Array of values.
An smiple implementation of flatMap:
123456789101112function flatMap (arr, mapFunc) { const result = [] for (const [inde ...
New Babel Preset - Env
babel-preset-env is a new preset which let you specify an environment and automatically enables the necessary plugins.
At the moment, several presets let you determine what features Babel should support:
babel-preset-es2015, babel-preset-es2016, etc: incrementally support various versions of ECMAScript. babel-preset-es2015 transpiles what’s new in ES6 to ES5, babel-preset-es2016 transpiles what’s new in ES7 to ES6.
babel-preset-latest: supports all features that are either part of an ECMAScrip ...
Babel-Polyfill or Babel-Runtime
The babel-polyfill and babel-runtime modules are sued to serve the same function in two different ways. Both modules ultimately serve to emulate an ES6 environment.
Both babel-polyfill and babel-runtime emulate an ES6 environment with two things:
a slew of polyfills as provided by core-js
complete generator runtime
babel-polyfill accomplishes this task by assigning methods on the global or on native type prototypes which means that once required, as far as the javascript runtime you’re using ...
Deep in Runtime-Transform
This plugin is recommended in a library/tool
Note: Instance methods such as 'foobar'.includes('foo') will not work since that would require modification of existing built-ins(Use babel-polyfill for that)
Babel uses very small helpers for common functions such as _extend. By default this will be added to every file that requires it. This duplication is sometimes unnecessary, especially when your application is spread out over multiple files.
This is where the transform-runtime p ...
More Faster React Functional Component
Original
An basic Avatar Component:
12345class Avatar extends React.Component { render () { return <img src={this.props.url} /> }}
And its functional component style is:
1const Avatar = ({ url }) => <img src={url} />
As you can see, it’s just a simple js function returning an element.
React still does a lot of stuff on functional components that, by nature, will never be used.
But we can skip React internals for these functi ...
Css Shorthand Collection
12345678910body { background: url(...) /* image */ top center / 200px 200px /* position / size */ no-repeat /* repeat */ fixed /* attachment */ padding-box /* origin */ content-box /* clip */ red; /* color */}
Usage of Grid in Css
Original
IntroductionCSS Grid Layout (aka ‘Grid’), is a two-dimensional grid-based layout system that aims to do nothing less than completely change the way we design grid-based user interfaces.
TerminologyGrid ContainerThe element on which display: grid is applied. It’s the direct parent of all the grid items. In this example container is the grid container.
12345<div class="container"> <div class="item item-1"></div> <div class="item item-2" ...
Simple Usage of Scp
Original
Basic Syntax of SCP1scp source_file_name username@destination_host:destination_folder
Provider the detail information of SCP process using -v parameterBaisc SCP command without parameter will copy the files in background. User will see nothing unless the process is done or something error appears.
You can use -v parameter to print debug information into screen.
12# verbosescp -v source_file_name username@destination_host:destination_folder
Provider modification times, access times, an ...
RxJS API
CreateYou can create an Observable from scratch by using the Create operator. You pass this operator a function that accepts the observer as its parameter. Write this function so that it behaves as an Observable – by calling the observer’s onNext, onError, and onCompleted methods appropraitely.
A well-formed finite Observable must attempt to call either the observer’s onCompleted method exactly once or its onError method exactly once, and must not thereafter attempt to call any of the observer’s ...
Detail of Node Cluster
ClusterA single instance of Node.js runs in a single tread. To take advantage of multi-core systems the user will sometimes want to launch a cluster of Node.js processes to handle the load.
The cluster module allows you to easily create child processes that all share server port.
1234567891011121314151617181920212223242526const cluster = require('cluster')const http = require('http')const numCPUs = require('os').cpus().lengthif (cluster.isMaster) { console.log ...
Api of Axios
alias
axios.request(config) axios.get(url[, config]) axios.delete(url[, config]) axios.head(url[, config]) axios.options(url[, config]) axios.post(url[, data[, config]]) axios.put(url[, data[, config]]) axios.patch(url[, data[, config]])
Config Optionsurl is required, and get is the default method
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758{ url: 'string', method: 'get', baseUrl: 'https://some-domai ...
webpackDevServer Proxy
devServer.proxy: object
Proxying some URLs can be useful when you have a separate API backend development server and you want to send API requests on the same domain.
The dev-server makes use of the powerful http-proxy-middleware package.
With a backend on localhost:3000, you can use this to enable proxying:
123proxy: { '/api': 'http://localhost:3000'}
A request to /api/users will now proxy to the request to http://localhost:3000/api/users
If you don’t ...
Notes in Migrating From React-Router-3 to React-Router-4
HashRouterVersion 4 of React-Router seperate top level router element for the different history type. If you’re using version 4 you should be able to replace <Router hisotry={hashHistory}> with <HashRouter>
Multiple Routes at same URL123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172import React from 'react'import { BrowserRouter as Router, Route, Link,} from 're ...
Learning D3
d3-arrayarray methods built-in to JavaScript
array.pop => popped element
array.push => array.length
array.reverse => change the array
array.shift => shifted element
array.unshift => array.length
array.sort => change the array
array.splice => change the array
array.concat => new Array
array.join => new String
array.slice => new Array
array.indexOf => number
array.lastIndexOf => number
array.fitler => new Array
array.forEach => undefined
arra ...
Node-Inspector's Crash
I had encountered a problem using node-inspector with info
cb(error, NM[0].ref); Cannot read property ‘ref’ of undefined
And one method to resolve it is to modify the packagge
Same issue with node v6.5.0 can be solved like this :
Edit …\node_modules\node-inspector\lib\InjectorClient.js file at line 111 if(NM.length > 0) cb(error, NM[0].ref);
It works
which is hack, and another method is without side-effect
use the local node-inspector
hope it helps