数値の書式化ができる文字列ライブラリunderscore.string
をangular-cliで使用するためのメモ。
動作確認環境
以下の環境で動作確認済みです。
# | package | version |
---|---|---|
1 | angular-cli | 1.0.0-beta.24 |
2 | typescript | 2.0.3 |
3 | angular | 2.3.1 |
4 | underscore.string | 3.3.4 |
5 | @types/underscore.string | 0.0.30 |
underscore.string のインストール
まずはunderscore.string
本体のインストール。
npm install underscore.string --save
続いて、angular-cliの設定。underscore.string.min.js
をscripts
の中に追加。
angular-cli.json
"scripts": [
"../node_modules/underscore.string/dist/underscore.string.min.js"
]
@types/underscore.string のインストール
typescriptで使うためにd.ts定義ファイルをインストールする。
npm install @types/underscore.string --save-dev
使う
sample.ts
import * as s from 'underscore.string'
console.debug(s.lpad('1', 2, '0'));
console.debug(s.numberFormat(1000, 2));
console.debug(s.numberFormat(123456789.123, 5, ".", ","));
コンソール出力
01
1,000.00
123,456,789.12300
underscore.string
自体の使い方は以下が参考になる。