mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-05-18 20:37:14 +08:00
Merge pull request #4129 from jasonswearingen/push
new typings: sprintf-js, json5, dsv. commonjs support for paralleljs
This commit is contained in:
8
dsv/dsv-tests.ts
Normal file
8
dsv/dsv-tests.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
/// <reference path="dsv.d.ts" />
|
||||
|
||||
import dsv = require("dsv");
|
||||
|
||||
var csv = dsv(",");
|
||||
|
||||
var rows = csv.parse("a,b,c\n1,2,3\n4,5,6");
|
||||
|
||||
68
dsv/dsv.d.ts
vendored
Normal file
68
dsv/dsv.d.ts
vendored
Normal file
@@ -0,0 +1,68 @@
|
||||
// Type definitions for dsv
|
||||
// Project: https://www.npmjs.com/package/dsv
|
||||
// Definitions by: Jason Swearingen <https://jasonswearingen.github.io>
|
||||
// Definitions: https://github.com/borisyankov/DefinitelyTyped
|
||||
|
||||
|
||||
//commonjs loader
|
||||
declare module "dsv" {
|
||||
|
||||
/** A parser and formatter for DSV (CSV and TSV) files.
|
||||
Extracted from D3. */
|
||||
var loader: (
|
||||
/** the symbol used to seperate cells in the row.*/
|
||||
delimiter: string,
|
||||
/** example: "text/plain" */
|
||||
encoding?: string) => _dsv.Dsv;
|
||||
export = loader;
|
||||
}
|
||||
|
||||
declare module _dsv {
|
||||
/** A parser and formatter for DSV (CSV and TSV) files.
|
||||
Extracted from D3. */
|
||||
export class Dsv {
|
||||
/** Parses the specified string, which is the contents of a CSV file, returning an array of objects representing the parsed rows.
|
||||
The string is assumed to be RFC4180-compliant.
|
||||
Unlike the parseRows method, this method requires that the first line of the CSV file contains a comma-separated list of column names;
|
||||
these column names become the attributes on the returned objects.
|
||||
For example, consider the following CSV file:
|
||||
|
||||
Year,Make,Model,Length
|
||||
1997,Ford,E350,2.34
|
||||
2000,Mercury,Cougar,2.38
|
||||
|
||||
The resulting JavaScript array is:
|
||||
|
||||
[ {"Year": "1997", "Make": "Ford", "Model": "E350", "Length": "2.34"},
|
||||
{"Year": "2000", "Make": "Mercury", "Model": "Cougar", "Length": "2.38"} ]
|
||||
*/
|
||||
public parse<TRow>(
|
||||
table: string,
|
||||
/** coerce cells (strings) into different types or modify them. return null to strip this row from the output results. */
|
||||
accessor?: (row: any) => TRow
|
||||
): TRow[];
|
||||
/** Parses the specified string, which is the contents of a CSV file, returning an array of arrays representing the parsed rows. The string is assumed to be RFC4180-compliant. Unlike the parse method, this method treats the header line as a standard row, and should be used whenever the CSV file does not contain a header. Each row is represented as an array rather than an object. Rows may have variable length. For example, consider the following CSV file:
|
||||
|
||||
1997,Ford,E350,2.34
|
||||
2000,Mercury,Cougar,2.38
|
||||
The resulting JavaScript array is:
|
||||
|
||||
[ ["1997", "Ford", "E350", "2.34"],
|
||||
["2000", "Mercury", "Cougar", "2.38"] ]
|
||||
Note that the values themselves are always strings; they will not be automatically converted to numbers. See parse for details.*/
|
||||
public parseRows<TRow>(
|
||||
table: string,
|
||||
/** coerce cells (strings) into different types or modify them. return null to strip this row from the output results.*/
|
||||
accessor?: (row: string[]) => TRow
|
||||
): TRow[];
|
||||
/** Converts the specified array of rows into comma-separated values format, returning a string. This operation is the reverse of parse. Each row will be separated by a newline (\n), and each column within each row will be separated by a comma (,). Values that contain either commas, double-quotes (") or newlines will be escaped using double-quotes.
|
||||
|
||||
Each row should be an object, and all object properties will be converted into fields. For greater control over which properties are converted, convert the rows into arrays containing only the properties that should be converted and use formatRows. */
|
||||
public format(rows: any[]): string;
|
||||
/** Converts the specified array of rows into comma-separated values format, returning a string. This operation is the reverse of parseRows. Each row will be separated by a newline (\n), and each column within each row will be separated by a comma (,). Values that contain either commas, double-quotes (") or newlines will be escaped using double-quotes. */
|
||||
public formatRows(rows: any[]): string;
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
7
json5/json5-tests.ts
Normal file
7
json5/json5-tests.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
/// <reference path="json5.d.ts" />
|
||||
import JSON5 = require('json5');
|
||||
|
||||
var obj = JSON5.parse("{ key:'val', 'key2':[0,1,2,] } //comment ");
|
||||
var str = JSON5.stringify(obj, null, "\t");
|
||||
console.log(str);
|
||||
|
||||
45
json5/json5.d.ts
vendored
Normal file
45
json5/json5.d.ts
vendored
Normal file
@@ -0,0 +1,45 @@
|
||||
// Type definitions for JSON5
|
||||
// Project: http://json5.org/
|
||||
// Definitions by: Jason Swearingen <https://jasonswearingen.github.io>
|
||||
// Definitions: https://github.com/borisyankov/DefinitelyTyped
|
||||
|
||||
|
||||
//commonjs loader
|
||||
declare module "json5" {
|
||||
/**
|
||||
* The following is the exact list of additions to JSON's syntax introduced by JSON5. All of these are optional, and all of these come from ES5.
|
||||
|
||||
Objects
|
||||
|
||||
Object keys can be unquoted if they're valid identifiers. Yes, even reserved keywords (like default) are valid unquoted keys in ES5 [§11.1.5, §7.6]. (More info)
|
||||
|
||||
(TODO: Unicode characters and escape sequences aren’t yet supported in this implementation.)
|
||||
|
||||
Objects can have trailing commas.
|
||||
|
||||
Arrays
|
||||
|
||||
Arrays can have trailing commas.
|
||||
Strings
|
||||
|
||||
Strings can be single-quoted.
|
||||
|
||||
Strings can be split across multiple lines; just prefix each newline with a backslash. [ES5 §7.8.4]
|
||||
|
||||
Numbers
|
||||
|
||||
Numbers can be hexadecimal (base 16).
|
||||
|
||||
Numbers can begin or end with a (leading or trailing) decimal point.
|
||||
|
||||
Numbers can include Infinity, -Infinity, NaN, and -NaN.
|
||||
|
||||
Numbers can begin with an explicit plus sign.
|
||||
|
||||
Comments
|
||||
|
||||
Both inline (single-line) and block (multi-line) comments are allowed.
|
||||
*/
|
||||
var json5: JSON;
|
||||
export = json5;
|
||||
}
|
||||
4
parallel/parallel.d.ts
vendored
4
parallel/parallel.d.ts
vendored
@@ -102,3 +102,7 @@ declare class Parallel<T> {
|
||||
public require(fn: Function): Parallel<T>;
|
||||
}
|
||||
|
||||
/* commonjs binding for npm use */
|
||||
declare module "paralleljs" {
|
||||
export = Parallel;
|
||||
}
|
||||
71
sprintf-js/sprintf-js.d.ts
vendored
Normal file
71
sprintf-js/sprintf-js.d.ts
vendored
Normal file
@@ -0,0 +1,71 @@
|
||||
// Type definitions for sprintf-js
|
||||
// Project: https://www.npmjs.com/package/sprintf-js
|
||||
// Definitions by: Jason Swearingen <https://jasonswearingen.github.io>
|
||||
// Definitions: https://github.com/borisyankov/DefinitelyTyped
|
||||
|
||||
|
||||
/** sprintf.js is a complete open source JavaScript sprintf implementation for the browser and node.js.
|
||||
|
||||
Its prototype is simple:
|
||||
|
||||
string sprintf(string format , [mixed arg1 [, mixed arg2 [ ,...]]])
|
||||
*/
|
||||
declare module "sprintf-js" {
|
||||
/** sprintf.js is a complete open source JavaScript sprintf implementation for the browser and node.js.
|
||||
Its prototype is simple:
|
||||
string sprintf(string format , [mixed arg1 [, mixed arg2 [ ,...]]])
|
||||
|
||||
==Placeholders==
|
||||
The placeholders in the format string are marked by % and are followed by one or more of these elements. see "fmt" arg for more docs on placeholders.
|
||||
|
||||
==Argument swapping==
|
||||
You can also swap the arguments. That is, the order of the placeholders doesn't have to match the order of the arguments. You can do that by simply indicating in the format string which arguments the placeholders refer to:
|
||||
sprintf("%2$s %3$s a %1$s", "cracker", "Polly", "wants")
|
||||
And, of course, you can repeat the placeholders without having to increase the number of arguments.
|
||||
|
||||
==Named arguments==
|
||||
Format strings may contain replacement fields rather than positional placeholders. Instead of referring to a certain argument, you can now refer to a certain key within an object. Replacement fields are surrounded by rounded parentheses - ( and ) - and begin with a keyword that refers to a key:
|
||||
var user = {name: "Dolly"}
|
||||
sprintf("Hello %(name)s", user) // Hello Dolly
|
||||
Keywords in replacement fields can be optionally followed by any number of keywords or indexes:
|
||||
var users = [{name: "Dolly"},{name: "Molly"},{name: "Polly"}]
|
||||
sprintf("Hello %(users[0].name)s, %(users[1].name)s and %(users[2].name)s", {users: users}) // Hello Dolly, Molly and Polly
|
||||
Note: mixing positional and named placeholders is not (yet) supported
|
||||
|
||||
==Computed values==
|
||||
You can pass in a function as a dynamic value and it will be invoked (with no arguments) in order to compute the value on-the-fly.
|
||||
sprintf("Current timestamp: %d", Date.now) // Current timestamp: 1398005382890
|
||||
sprintf("Current date and time: %s", function() { return new Date().toString() })
|
||||
*/
|
||||
export function sprintf(
|
||||
/** The placeholders in the format string are marked by % and are followed by one or more of these elements, in this order:
|
||||
|
||||
An optional number followed by a $ sign that selects which argument index to use for the value. If not specified, arguments will be placed in the same order as the placeholders in the input string.
|
||||
An optional + sign that forces to preceed the result with a plus or minus sign on numeric values. By default, only the - sign is used on negative numbers.
|
||||
An optional padding specifier that says what character to use for padding (if specified). Possible values are 0 or any other character precedeed by a ' (single quote). The default is to pad with spaces.
|
||||
An optional - sign, that causes sprintf to left-align the result of this placeholder. The default is to right-align the result.
|
||||
An optional number, that says how many characters the result should have. If the value to be returned is shorter than this number, the result will be padded.
|
||||
An optional precision modifier, consisting of a . (dot) followed by a number, that says how many digits should be displayed for floating point numbers. When used on a string, it causes the result to be truncated.
|
||||
A type specifier that can be any of:
|
||||
% - yields a literal % character
|
||||
b - yields an integer as a binary number
|
||||
c - yields an integer as the character with that ASCII value
|
||||
d or i - yields an integer as a signed decimal number
|
||||
e - yields a float using scientific notation
|
||||
u - yields an integer as an unsigned decimal number
|
||||
f - yields a float as is
|
||||
o - yields an integer as an octal number
|
||||
s - yields a string as is
|
||||
x - yields an integer as a hexadecimal number (lower-case)
|
||||
X - yields an integer as a hexadecimal number (upper-case)
|
||||
*/
|
||||
fmt: string,
|
||||
/** */
|
||||
...args: any[]
|
||||
): string;
|
||||
/** vsprintf is the same as sprintf except that it accepts an array of arguments, rather than a variable number of arguments:
|
||||
|
||||
vsprintf("The first 4 letters of the english alphabet are: %s, %s, %s and %s", ["a", "b", "c", "d"])
|
||||
*/
|
||||
export function vsprintf(fmt: string, args: any[]): string;
|
||||
}
|
||||
14
sprintf-js/sprintf-tests.ts
Normal file
14
sprintf-js/sprintf-tests.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
/// <reference path="sprintf-js.d.ts" />
|
||||
|
||||
import sprintf = require('sprintf-js');
|
||||
|
||||
var str: string;
|
||||
var num: number;
|
||||
|
||||
sprintf.sprintf(str, str);
|
||||
sprintf.sprintf(str, str, num);
|
||||
sprintf.sprintf(str, num, str);
|
||||
|
||||
sprintf.vsprintf(str, [str]);
|
||||
sprintf.vsprintf(str, [str, num]);
|
||||
sprintf.vsprintf(str, [num, str]);
|
||||
Reference in New Issue
Block a user