🤖 Merge PR #63801 [parse-decimal-number] Add types by @ciaran1344

* [parse-decimal-number] Add types

* [parse-decimal-number] Add types using namespaces
This commit is contained in:
ciaran1344
2023-01-04 23:35:38 +00:00
committed by GitHub
parent a79ac14faa
commit 57e6773b0b
4 changed files with 96 additions and 0 deletions

56
types/parse-decimal-number/index.d.ts vendored Normal file
View File

@@ -0,0 +1,56 @@
// Type definitions for parse-decimal-number 1.0
// Project: https://github.com/AndreasPizsa/parse-decimal-number
// Definitions by: Ciarán Ingle <https://github.com/ciaran1344>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
declare namespace parseDecimalNumber {
type ArrayOptions = [thousands: string, decimal: string];
interface HashOptions {
/**
* Decimal point character.
* Default: ".".
*/
decimal: string;
/**
* Thousands separator character.
* Default: ",".
*/
thousands: string;
}
type Options = ArrayOptions | HashOptions | string;
/**
* Has the same effect as `parseDecimalNumber.setOptions({ thousands: ",", decimal: "." })`.
*/
function factoryReset(): void;
/**
* Set the default thousands and decimal characters that are used when no options are passed to
* {@link parseDecimalNumber}.
*/
function setOptions(hash: HashOptions): void;
/**
* Returns a function that will take a string as an argument and return a float or `NaN`,
* just like {@link parseDecimalNumber}.
*/
function withOptions(options: Options, enforceGroupSize?: boolean): (string: string) => number;
}
declare function parseDecimalNumber(
/**
* A string that is supposed to contain a number.
*/
string: string,
/**
* A string, array or hash with thousands and decimal separators.
*/
options?: parseDecimalNumber.Options,
/**
* A boolean indicating whether to support that individual groups between the thousands
* character are exactly 3 digits.
*/
enforceGroupSize?: boolean,
): number;
export = parseDecimalNumber;

View File

@@ -0,0 +1,16 @@
import parseDecimalNumber = require('parse-decimal-number');
parseDecimalNumber('12,345,678.90'); // $ExpectType number
parseDecimalNumber('12.345.678,90', '.,'); // $ExpectType number
parseDecimalNumber('12.345.678,90', ['.', ',']); // $ExpectType number
parseDecimalNumber('12.345.678,90', { thousands: '.', decimal: ',' }); // $ExpectType number
parseDecimalNumber('12.345.678,90', '.,', true); // $ExpectType number
parseDecimalNumber.factoryReset(); // $ExpectType void
parseDecimalNumber.setOptions({ thousands: '.', decimal: ',' }); // $ExpectType void
parseDecimalNumber.withOptions({ thousands: '.', decimal: ',' })('12.345.678,90'); // $ExpectType number
parseDecimalNumber.withOptions({ thousands: '.', decimal: ',' }, true)('12.345.678,90'); // $ExpectType number

View File

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

View File

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