Skip to content

Commit 82c0129

Browse files
authored
TypeScript: Convert wp-utils package (#12247)
1 parent e1efbf1 commit 82c0129

File tree

6 files changed

+58
-27
lines changed

6 files changed

+58
-27
lines changed

packages/wp-utils/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
"npm": ">= 7.3"
2626
},
2727
"type": "module",
28-
"main": "./src/index.js",
28+
"main": "./src/index.ts",
29+
"types": "dist-types/index.d.ts",
30+
"source": "src/index.ts",
2931
"dependencies": {}
3032
}

packages/wp-utils/src/bindToCallbacks.js renamed to packages/wp-utils/src/bindToCallbacks.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,17 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
const bindToCallbacks = (callbacks, config) => {
17-
return Object.entries(callbacks).reduce((_callbacks, [name, callback]) => {
18-
_callbacks[name] = callback.bind(null, config);
19-
return _callbacks;
20-
}, {});
21-
};
16+
17+
function bindToCallbacks<T>(
18+
callbacks: Record<string, (config: T) => unknown>,
19+
config: T
20+
): Record<string, unknown> {
21+
return Object.fromEntries(
22+
Object.entries(callbacks).map(([name, callback]) => [
23+
name,
24+
callback.bind(null, config),
25+
])
26+
);
27+
}
2228

2329
export default bindToCallbacks;
File renamed without changes.

packages/wp-utils/src/snakeToCamelCase.js renamed to packages/wp-utils/src/snakeToCamelCase.ts

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
* Takes a string that is in snake case and returns the same
1818
* term in camel case.
1919
*
20-
* @param {string} string The key in snake case
21-
* @return {string} The key in camel case
20+
* @param string The key in snake case
21+
* @return The key in camel case
2222
*/
23-
export function snakeToCamelCase(string = '') {
23+
export function snakeToCamelCase(string = ''): string {
2424
if (!string.includes('_') && !string.includes('-')) {
2525
return string;
2626
}
@@ -29,32 +29,46 @@ export function snakeToCamelCase(string = '') {
2929
.toLowerCase()
3030
.replace(
3131
/([a-z])([_|-][a-z])/g,
32-
(_match, group1, group2) =>
32+
(_match, group1: string, group2: string) =>
3333
group1 + group2.toUpperCase().replace('_', '').replace('-', '')
3434
);
3535
}
3636

37+
type ObjectOrPrimitive = SnakeOrCamelCaseObject | unknown;
38+
39+
interface SnakeOrCamelCaseObject {
40+
[key: string]: ObjectOrPrimitive;
41+
}
42+
43+
function isObject(obj: unknown) {
44+
return obj && 'object' === typeof obj && !Array.isArray(obj);
45+
}
46+
3747
/**
3848
* Transform a given object keys from snake case to camel case recursively.
3949
*
40-
* @param {Object} obj Object to be transformed.
41-
* @param {Object} ignore Array of keys to be ignored for camelcase.
42-
* @return {any} Transformed object.
50+
* @param obj Object to be transformed.
51+
* @param ignore Array of keys to be ignored for camelcase.
52+
* @return Transformed object.
4353
*/
44-
export function snakeToCamelCaseObjectKeys(obj, ignore = []) {
45-
const isObject = (val) =>
46-
val && 'object' === typeof val && !Array.isArray(val);
47-
54+
export function snakeToCamelCaseObjectKeys(
55+
obj: ObjectOrPrimitive,
56+
ignore: string[] = []
57+
): ObjectOrPrimitive {
4858
if (!isObject(obj)) {
4959
return obj;
5060
}
5161

52-
return Object.entries(obj).reduce((transformedObject, [key, value]) => {
53-
const camelCaseKey = snakeToCamelCase(key);
54-
transformedObject[camelCaseKey] =
55-
isObject(value) && !ignore.includes(key)
56-
? snakeToCamelCaseObjectKeys(value)
57-
: value;
58-
return transformedObject;
59-
}, {});
62+
return Object.fromEntries(
63+
Object.entries(obj as SnakeOrCamelCaseObject).map(
64+
([key, value]: [string, ObjectOrPrimitive]) => {
65+
return [
66+
snakeToCamelCase(key),
67+
isObject(value) && !ignore.includes(key)
68+
? snakeToCamelCaseObjectKeys(value)
69+
: value,
70+
] as [string, SnakeOrCamelCaseObject];
71+
}
72+
)
73+
);
6074
}

packages/wp-utils/tsconfig.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"extends": "../../tsconfig.shared.json",
3+
"compilerOptions": {
4+
"rootDir": "src",
5+
"declarationDir": "dist-types"
6+
},
7+
"include": ["src/**/*"]
8+
}

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
{ "path": "packages/tracking" },
1010
{ "path": "packages/transform" },
1111
{ "path": "packages/units" },
12-
{ "path": "packages/url" }
12+
{ "path": "packages/url" },
13+
{ "path": "packages/wp-utils" }
1314
],
1415
"include": []
1516
}

0 commit comments

Comments
 (0)