Skip to content

Commit 6088a98

Browse files
committed
lnc-web: allow request keys to be camel-cased
1 parent 9f81b8e commit 6088a98

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

lib/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99

1010
import isPlainObject from 'lodash/isPlainObject';
1111
import { wasmLog as log } from './util/log';
12-
import { isObject, snakeKeysToCamel } from './util/objects';
12+
import { isObject, camelKeysToSnake, snakeKeysToCamel } from './util/objects';
1313
import { LndApi, LoopApi, PoolApi, FaradayApi } from './api';
1414
import {
1515
createTestCipher,
@@ -206,7 +206,7 @@ export default class LNC {
206206
clearStorage = () =>
207207
Object.entries(localStorage)
208208
.map((x) => x[0])
209-
.filter((x) => x.substring(0, 8) == 'lnc-web:')
209+
.filter((x) => x.substring(0, 8) === 'lnc-web:')
210210
.map((x) => localStorage.removeItem(x));
211211

212212
/**
@@ -528,7 +528,7 @@ export default class LNC {
528528
} else {
529529
updated[key] = value;
530530
}
531-
return updated;
531+
return camelKeysToSnake(updated);
532532
}, o);
533533
}
534534
}

lib/util/objects.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ const toCamel = (text: string) => {
77
});
88
};
99

10+
/**
11+
* Converts a string from camel-case to snake-case
12+
*/
13+
const toSnake = (str: string) =>
14+
str.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`);
15+
1016
/**
1117
* Returns true if the value provided is an array
1218
*/
@@ -44,3 +50,26 @@ export const snakeKeysToCamel = <T>(o: any): T => {
4450

4551
return o;
4652
};
53+
54+
/**
55+
* Recursively converts the keys of a Javascript object from camel-case to snake-case
56+
* Ex: { someKey: 'foo' } becomes { some-key: 'foo' }
57+
* @param o any Javascript object
58+
*/
59+
export const camelKeysToSnake = <T>(o: any): T => {
60+
if (isObject(o)) {
61+
const n: Record<string, unknown> = {};
62+
63+
Object.keys(o).forEach((k) => {
64+
n[toSnake(k)] = camelKeysToSnake(o[k]);
65+
});
66+
67+
return n as T;
68+
} else if (isArray(o)) {
69+
return o.map((i: any) => {
70+
return camelKeysToSnake(i);
71+
});
72+
}
73+
74+
return o;
75+
};

0 commit comments

Comments
 (0)