Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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 / resolveAllPromise.ts
Last active September 21, 2016 08:53
An example to get an object which is resolved all (even nested) promises in Zone.
import 'babel-polyfill';
import 'zone.js/dist/zone-node';
declare const Zone: any;
Zone.current.fork({}).run(async () => {
// (async () => {
const obj = {
increment: Promise.resolve({
@ovrmrw
ovrmrw / simple-redux.ts
Last active December 29, 2016 16:31
Simple concept for easy Redux
import 'core-js';
import { Observable, Subject, BehaviorSubject } from 'rxjs';
type Action = {
key: string;
value: any;
}
type State = {
@ovrmrw
ovrmrw / main.ts
Created February 21, 2017 08:02
redux dev tools
require('setimmediate')
const asap = require('asap') as (func: Function) => void
const cloneDeep = require('lodash.clonedeep') as <T>(obj: T) => T
import { createStore, applyMiddleware, Store } from 'redux'
import { Observable } from 'rxjs/Observable'
import { Subject } from 'rxjs/Subject'
import { BehaviorSubject } from 'rxjs/BehaviorSubject'
import 'rxjs/add/observable/of'
import 'rxjs/add/observable/from'
@ovrmrw
ovrmrw / mp3-rename-util.ts
Created April 18, 2017 07:03
Re-name MP3 files from its ID3 tag's title.
import * as fs from 'fs-extra'
import * as nodeID3 from 'node-id3'
import * as path from 'path'
const TARGET = 'au_kaiwa_1705_A'
const filePaths = fs.readdirSync(TARGET)
.map((file) => path.join(process.cwd(), TARGET, file))
.filter((filePath) => fs.existsSync(filePath))
import { WindowWrapper } from './WindowWrapper';
let _namespace: Namespace | undefined;
export class Namespace {
private readonly key: string;
static fromName(key: string): Namespace {
if (!_namespace) {
_namespace = new Namespace(key);