Skip to content

Commit

Permalink
fix: drop Lodash
Browse files Browse the repository at this point in the history
  • Loading branch information
ruisaraiva19 committed Aug 18, 2022
1 parent 49926a4 commit ba8b9f5
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 68 deletions.
44 changes: 10 additions & 34 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@
"prepublishOnly": "npm run build"
},
"dependencies": {
"js-cookie": "^2.2.1",
"lodash": "^4.17.21"
"js-cookie": "^2.2.1"
},
"devDependencies": {
"@types/js-cookie": "^3.0.2",
"@types/lodash": "^4.14.183",
"@types/js-cookie": "^2.2.1",
"@types/node": "^18.7.6",
"tsup": "^6.2.2",
"typescript": "^4.7.4"
Expand Down
67 changes: 37 additions & 30 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import Cookies, { CookieAttributes } from 'js-cookie';
import cloneDeep from 'lodash/fp/cloneDeep';
import isObject from 'lodash/fp/isObject';

type CustomOptions = {
params?: Record<string, any>
Expand Down Expand Up @@ -179,6 +177,14 @@ function interpolate(str: string, ctx: Record<string, any>) {
return str.replace(/{{\s?([a-zA-Z]+)\s?}}/g, (match, key) => ctx[key] || match);
}

/**
* Based on https://github.com/lodash/lodash/blob/2da024c3b4f9947a48517639de7560457cd4ec6c/isObject.js
*/
function isObject(value: unknown) {
const type = typeof value
return value != null && (type === 'object' || type === 'function')
}

const configs: Record<string, Config> = {
default: {
apiUrl: '/api',
Expand Down Expand Up @@ -279,33 +285,34 @@ export default class DeviseTokenAuthClient {
constructor(params: Partial<Config> | Array<Record<string, Partial<Config>>>) {
// user is using multiple concurrent configs (>1 user types).
if (params instanceof Array && params.length) {
// extend each item in array from default settings
for (let i = 0; i < params.length; i++) {
// get the name of the config
const conf = params[i];
let label = '';
// eslint-disable-next-line no-restricted-syntax,guard-for-in
for (const k in conf) {
label = k;

// set the first item in array as default config
if (i === 0) {
defaultConfigName = label;
}
}

// use copy preserve the original default settings object while
// extending each config object
const defaults = cloneDeep(configs.default);
const fullConfig = {} as any;
fullConfig[label] = Object.assign(defaults, conf[label]);
Object.assign(configs, fullConfig);
}

// remove existing default config
if (defaultConfigName !== 'default') {
delete configs.default;
}
throw new Error('DeviceTokenAuthClient config as an Array is not supported.\nWe need to migrate cloneDeep from lodash/fp/cloneDeepDeep first.');
// // extend each item in array from default settings
// for (let i = 0; i < params.length; i++) {
// // get the name of the config
// const conf = params[i];
// let label = '';
// // eslint-disable-next-line no-restricted-syntax,guard-for-in
// for (const k in conf) {
// label = k;

// // set the first item in array as default config
// if (i === 0) {
// defaultConfigName = label;
// }
// }

// // use copy preserve the original default settings object while
// // extending each config object
// const defaults = cloneDeep(configs.default);
// const fullConfig = {} as any;
// fullConfig[label] = Object.assign(defaults, conf[label]);
// Object.assign(configs, fullConfig);
// }

// // remove existing default config
// if (defaultConfigName !== 'default') {
// delete configs.default;
// }
} else if (params instanceof Object) {
// user is extending the single default config
Object.assign(configs.default, params);
Expand Down Expand Up @@ -834,7 +841,7 @@ performBestTransit();`;
/**
* Generates query string based on simple or complex object graphs
*/
buildQueryString(params: Record<string, string>, prefix?: string) {
buildQueryString(params: Record<string, any>, prefix?: string) {
const str: string[] = [];
Object.entries(params).forEach(([key, val]) => {
const k = prefix ? `${prefix}[${key}]` : key;
Expand Down

0 comments on commit ba8b9f5

Please sign in to comment.