From b1956785d638d352d4045bae2b35dca7c19b0b41 Mon Sep 17 00:00:00 2001 From: Zhiyuan Wang Date: Fri, 30 Oct 2015 21:24:22 +0800 Subject: [PATCH] Add definitions and tests for NodeJs package bytes v2.1.0 --- bytes/bytes-tests.ts | 16 +++++++++++ bytes/bytes.d.ts | 66 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 bytes/bytes-tests.ts create mode 100644 bytes/bytes.d.ts diff --git a/bytes/bytes-tests.ts b/bytes/bytes-tests.ts new file mode 100644 index 0000000000..4a981aee47 --- /dev/null +++ b/bytes/bytes-tests.ts @@ -0,0 +1,16 @@ +/// + +import bytes = require('bytes'); + +// 1024*1024 = 1048576 +console.log(bytes(104857)); +console.log(bytes(104857, { thousandsSeparator: ' ' })); + +console.log(bytes.format(104857)); +console.log(bytes.format(104857, { thousandsSeparator: ' ' })); + +console.log(bytes('1024kb')); +console.log(bytes(1024)); + +console.log(bytes.parse('1024kb')); +console.log(bytes.parse(1024)); \ No newline at end of file diff --git a/bytes/bytes.d.ts b/bytes/bytes.d.ts new file mode 100644 index 0000000000..d9946edf2f --- /dev/null +++ b/bytes/bytes.d.ts @@ -0,0 +1,66 @@ +// Type definitions for bytes v2.1.0 +// Project: https://github.com/visionmedia/bytes.js +// Definitions by: Zhiyuan Wang +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +declare module 'bytes' { + + interface IBytesFormatOptions { + thousandsSeparator: string + } + + /** + *Convert the given value in bytes into a string. + * + * @param {number} value + * @param {{ + * thousandsSeparator: [string] + * }} [options] bytes options. + * + * @returns {string} + */ + function bytes(value: number, options?: IBytesFormatOptions): string; + + /** + *Parse string to an integer in bytes. + * + * @param {string} value + * @returns {number} + */ + function bytes(value: string): number; + + module bytes { + + /** + * Format the given value in bytes into a string. + * + * If the value is negative, take Math.abs(). If it is a float, + * it is rounded. + * + * @param {number} value + * @param {IBytesFormatOptions} [options] + */ + + function format(value: number, options?: IBytesFormatOptions): string; + + /** + * Just return the input number value. + * + * @param {number} value + * @return {number} + */ + function parse(value: number): number; + + /** + * Parse the string value into an integer in bytes. + * + * If no unit is given, it is assumed the value is in bytes. + * + * @param {string} value + * @return {number} + */ + function parse(value: string): number; + } + + export = bytes; +} \ No newline at end of file