DefinitelyTyped/types/query-string/query-string-tests.ts
Sean Zhu e174f81551 [query-string] Make types more accurate (#29395)
* [query-string] Make types more accurate

* Update index.d.ts

* Fix ts errors

* Fix linter errors

* Fix types and make tests more accurate

* Fix linter errors
2018-10-10 13:08:52 -07:00

45 lines
1.2 KiB
TypeScript

import * as queryString from 'query-string';
// stringify
{
let result: string;
// test obj
result = queryString.stringify({
str: 'bar',
strArray: ['baz'],
num: 123,
numArray: [456],
bool: true,
boolArray: [false]
});
// test options
result = queryString.stringify({ foo: 'bar' }, { strict: false });
result = queryString.stringify({ foo: 'bar' }, { encode: false });
result = queryString.stringify({ foo: 'bar' }, { strict: false, encode: false });
}
// For each section below, the second line ensures the real answer is of the declared
// type. You can find the real answer by running the first line of each section.
// parse
{
let fooBar = queryString.parse('?foo=bar');
fooBar = {foo: "bar"};
let fooBarBaz1 = queryString.parse('&foo=bar&foo=baz');
fooBarBaz1 = { foo: [ 'bar', 'baz' ] };
let fooBarBaz2 = queryString.parse('&foo[]=bar&foo[]=baz', {arrayFormat: 'bracket'});
fooBarBaz2 = { foo: [ 'bar', 'baz' ] };
}
// extract
{
let result1 = queryString.extract('http://foo.bar/?abc=def&hij=klm');
result1 = 'abc=def&hij=klm';
let result2 = queryString.extract('http://foo.bar/?foo=bar');
result2 = 'foo=bar';
}