types for 'urlencode' package (#31715)

* first commit

* fixed tsconfig, modify lint error

* declare urlencode function ad namespace

* [@types/urlencode] modify export 

export default urlencode -> exrpot = urlencode;

* esModuleInterop, allowSyntheticDefaultImports
add to tsconfig

* exprot default urlencode

* modify export =
This commit is contained in:
kimcoder 2019-01-08 01:31:28 +09:00 committed by Sheetal Nandi
parent 778c0d9771
commit 6dbe9483fc
4 changed files with 87 additions and 0 deletions

46
types/urlencode/index.d.ts vendored Normal file
View File

@ -0,0 +1,46 @@
// Type definitions for urlencode 1.1
// Project: https://github.com/node-modules/urlencode
// Definitions by: My Self <https://github.com/kimcoder>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
interface charsetParam {
charset: string;
}
/**
* Encode string
* @param str The string for encoding.
*/
declare function urlencode(str: string, charset?: string): string;
declare namespace urlencode {
/**
* Encode string
* @param str The string for encoding.
*/
function encode(str: string, charset?: string): string;
/**
* Decode string
* @param encodedString The encoded string.
*/
function decode(encodedString: string, charset?: string): string;
/**
* Parse querystring
* @param queryString Querystring
* @param charsetParam The charset for parsing
*/
function parse(queryString: string, charsetParam: charsetParam): any;
interface charsetParam {
charset: string;
}
/**
* Stringify object
* @param obj Query Object
* @param charsetParam The charset for parsing
*/
function stringify(obj: any, prefix?: charsetParam, charsetParam?: charsetParam): string;
}
export = urlencode;

View File

@ -0,0 +1,25 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"urlencode-tests.ts"
]
}

View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }

View File

@ -0,0 +1,15 @@
import urlencode from 'urlencode';
urlencode('苏千'); // default is utf8
urlencode('苏千', 'gbk'); // '%CB%D5%C7%A7'
// decode gbk
urlencode.decode('%CB%D5%C7%A7', 'gbk'); // '苏千'
// parse gbk querystring
urlencode.parse('nick=%CB%D5%C7%A7', {charset: 'gbk'}); // {nick: '苏千'}
// stringify obj with gbk encoding
const str = 'x[y][0][v][w]=' + urlencode('雾空', 'gbk'); // x[y][0][v][w]=%CE%ED%BF%D5
const obj = {x: {y : [{v : {w : '雾空'}}]}};
urlencode.stringify(obj, {charset: 'gbk'});