Skip to content

Instantly share code, notes, and snippets.

@ovrmrw
ovrmrw / chokidar-file-watch.ts
Last active August 24, 2016 07:59
chokidarで任意のフォルダのファイル追加・変更を監視するサンプル
import chokidar from 'chokidar'; // @types/chokidar
import parse from 'csv-parse'; // @types/csv-parse
import fs from 'fs'; // @types/node
chokidar.watch('S:/chokidar', { ignored: /[\/\\]\./ }).on('all', (event: string, path: string) => {
if (event === 'add' || event === 'change') {
if (new RegExp(/\.(csv|txt)$/).test(path)) {
console.log(event, path);
fs.readFile(path, 'utf8', (err, data: string) => {
@ovrmrw
ovrmrw / systemjs.config.js
Last active July 23, 2016 14:48
Useful SystemJS config for Angular2 with "packageConfigPaths" option.
System.config({
// baseURL: '/', // あるとないとでどう変わるかよくわからない。
// defaultJSExtensions: true, // 拡張子.jsを省略する。
transpiler: false, // on the flyでトランスパイルするときに設定する。
paths: {
'*': 'node_modules/*', // import {...} from 'hoge' と書くと node_modules/hoge を参照するようになる。
'root:*': '/*',
},
packageConfigPaths: [ // package.jsonのmainプロパティがあればjsファイルを自動で参照する。
'*/package.json', // ./node_modules/*/package.json と書くのと同じ。lodash等のマッピングに使われる。
@ovrmrw
ovrmrw / jspm-typescript-types-copy.js
Last active July 16, 2016 12:00
Copy *.ts and *.d.ts files from ./jspm_packages/npm/ to ./node_modules/@types/
/*
Copy *.ts and *.d.ts files from ./jspm_packages/npm/ to ./node_modules/@types/
This tool is for TypeScript 2.0.
*/
const lodash = require('lodash');
const fs = require('fs-extra');
const path = require('path');
const root = path.resolve();
require('./jspm_packages/system.src'); // SystemJS
@ovrmrw
ovrmrw / async-power.ts
Last active June 22, 2016 08:23
Helper to use power-assert in Jasmine testing space.
export function asyncPower(asyncFunction: () => Promise<void>): Function {
return function (done) {
asyncFunction()
.then(() => done())
.catch(e => done.fail(e.message));
}
}
@ovrmrw
ovrmrw / fake_async.ts
Created June 14, 2016 04:58
Angular2 rc.1のfakeAsyncの書き換え
import {BaseException} from '@angular/core';
import {getTestInjector} from '@angular/core/testing';
declare var Zone: any;
let _FakeAsyncTestZoneSpecType = (Zone as any /** TODO #9100 */)['FakeAsyncTestZoneSpec'];
/**
* Wraps a function to be executed in the fakeAsync zone:
* - microtasks are manually executed by calling `flushMicrotasks()`,
@ovrmrw
ovrmrw / translator.ts
Last active April 30, 2016 08:27
MicrosoftのTranslator APIを使ってText-to-Textの翻訳をするサンプル
/*
MicrosoftのTranslator APIを使ってText-to-Textの翻訳をするサンプル。
https://www.microsoft.com/en-us/translator
es2015形式でJSにトランスパイルした後、babel-nodeで実行すると簡単です。
Dependencies: request, xml2js, babel-polyfill
*/
import 'babel-polyfill'; // async/awaitを書くなら必要。
@ovrmrw
ovrmrw / app+.js
Last active March 9, 2016 20:29
sample: ArangoDB's Foxx controller with using any library you want
(function() {
"use strict";
var Foxx = require("org/arangodb/foxx"),
controller = new Foxx.Controller(applicationContext),
_ = require("underscore"), // loading underscore.js in C:\Program Files\ArangoDB 2.2.4\share\arangodb\js\node\node_modules
moment = require("moment"); // loading moment.js in C:\Program Files\ArangoDB 2.2.4\share\arangodb\js\node\node_modules
controller.get("/hello/:name", function(req, res) {
res.set("Content-Type", "text/plain");
@ovrmrw
ovrmrw / sqlserver-csv.ts
Last active January 18, 2016 09:13
sample code to convert data from SQLServer to CSV on Node.js
import 'babel-polyfill';
import lodash from 'lodash';
const config = require('./config.json') as { mssql: any };
import Sequelize from 'sequelize';
const sequelize = new Sequelize(config.mssql.schema, config.mssql.user, config.mssql.password, {
host: config.mssql.host,
dialect: 'mssql'
});
@ovrmrw
ovrmrw / falcor-json-passer.js
Created January 7, 2016 08:57
(未完成) FalcorでフロントからサーバーにJSONを送るとき、送る前に色々と文字置換をして、送った後にそれを復元する必要がある。
'use strict';
const obj = {
a: 1,
b: "two'`%#",
c: true,
d: null,
e: [1, '`two', true, null],
percentReplacer: '@PERSONT@',
sharpReplacer: '@SHARP@'
@ovrmrw
ovrmrw / page1.ts
Created December 6, 2015 11:53
Angular2 Http module runs correctly on alpha.47 but it's failing on alpha.48.
import {Component} from 'angular2/angular2'
import {Http, Response, HTTP_PROVIDERS} from 'angular2/http'
import _ from 'lodash'
@Component({
selector: 'my-page1',
template: `
<ul>
<li *ng-for="#card of cards">{{card.title}}</li>
</ul>