Skip to content

Commit f0b8edd

Browse files
authored
🤖 Merge PR DefinitelyTyped#74465 Add types for should-format by @gasp
1 parent d4ea76e commit f0b8edd

File tree

5 files changed

+148
-0
lines changed

5 files changed

+148
-0
lines changed

types/should-format/.npmignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*
2+
!**/*.d.ts
3+
!**/*.d.cts
4+
!**/*.d.mts
5+
!**/*.d.*.ts

types/should-format/index.d.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* Options for the Formatter.
3+
*/
4+
interface FormatterOptions {
5+
/**
6+
* Custom function to get keys from an object.
7+
*/
8+
keysFunc?: (obj: object) => string[];
9+
10+
/**
11+
* If false, uses Object.getOwnPropertyNames instead of Object.keys.
12+
*/
13+
keys?: boolean;
14+
15+
/**
16+
* Maximum line length before wrapping. Default: 60.
17+
*/
18+
maxLineLength?: number;
19+
20+
/**
21+
* Property separator. Default: ','.
22+
*/
23+
propSep?: string;
24+
25+
/**
26+
* If true, formats dates in UTC.
27+
*/
28+
isUTCdate?: boolean;
29+
}
30+
31+
/**
32+
* Formatter class for converting values to string representations.
33+
*/
34+
declare class Formatter {
35+
constructor(opts?: FormatterOptions);
36+
37+
/**
38+
* Format a value to a string representation.
39+
* @param value - The value to format
40+
* @returns Formatted string representation
41+
*/
42+
format(value: unknown): string;
43+
}
44+
45+
/**
46+
* Default format function that creates a Formatter and formats the value.
47+
*
48+
* @param value - The value to format
49+
* @param opts - Optional formatter options
50+
* @returns Formatted string representation
51+
*
52+
* @example
53+
* ```javascript
54+
* const format = require('should-format');
55+
*
56+
* format({ a: 1, b: 2 }); // => "{ a: 1, b: 2 }"
57+
* format([1, 2, 3]); // => "[ 1, 2, 3 ]"
58+
* format(null); // => "null"
59+
* ```
60+
*/
61+
declare function format(value: unknown, opts?: FormatterOptions): string;
62+
63+
declare namespace format {
64+
export { Formatter, FormatterOptions };
65+
}
66+
67+
export = format;

types/should-format/package.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"private": true,
3+
"name": "@types/should-format",
4+
"version": "3.0.9999",
5+
"projects": [
6+
"https://github.com/shouldjs/format"
7+
],
8+
"devDependencies": {
9+
"@types/should-format": "workspace:."
10+
},
11+
"owners": [
12+
{
13+
"name": "gaspard",
14+
"githubUsername": "gasp"
15+
}
16+
]
17+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import format = require("should-format");
2+
3+
// Test default format function
4+
const objStr: string = format({ a: 1, b: 2 });
5+
const arrStr: string = format([1, 2, 3]);
6+
const nullStr: string = format(null);
7+
const undefinedStr: string = format(undefined);
8+
const numStr: string = format(42);
9+
const boolStr: string = format(true);
10+
const strStr: string = format("hello");
11+
const dateStr: string = format(new Date());
12+
const regexStr: string = format(/test/g);
13+
14+
// Test with options
15+
const withOptions: string = format({ a: 1 }, {
16+
maxLineLength: 80,
17+
propSep: ";",
18+
isUTCdate: true,
19+
});
20+
21+
// Test with keysFunc option
22+
const withKeysFunc: string = format({ a: 1, b: 2 }, {
23+
keysFunc: (obj) => Object.keys(obj).reverse(),
24+
});
25+
26+
// Test with keys: false option
27+
const withAllKeys: string = format({ a: 1 }, {
28+
keys: false,
29+
});
30+
31+
// Test Formatter class
32+
const formatter = new format.Formatter();
33+
const formatted: string = formatter.format({ test: true });
34+
35+
// Test Formatter with options
36+
const formatterWithOpts = new format.Formatter({
37+
maxLineLength: 100,
38+
propSep: ",",
39+
});
40+
const formatted2: string = formatterWithOpts.format([1, 2, 3]);

types/should-format/tsconfig.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"compilerOptions": {
3+
"module": "node16",
4+
"lib": [
5+
"es6"
6+
],
7+
"noImplicitAny": true,
8+
"noImplicitThis": true,
9+
"strictNullChecks": true,
10+
"strictFunctionTypes": true,
11+
"types": [],
12+
"noEmit": true,
13+
"forceConsistentCasingInFileNames": true
14+
},
15+
"files": [
16+
"index.d.ts",
17+
"should-format-tests.ts"
18+
]
19+
}

0 commit comments

Comments
 (0)