Skip to content

Commit

Permalink
feat: updates deps, migrates to eslint, adds migrate related code fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Enngage committed Jul 31, 2023
1 parent 2adc65a commit 288cbb7
Show file tree
Hide file tree
Showing 8 changed files with 2,714 additions and 9,249 deletions.
16 changes: 16 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* eslint-env node */
module.exports = {
extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended'],
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint'],
parserOptions: {
tsconfigRootDir: __dirname,
project: ['./tsconfig.json']
},
rules: {
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-unused-vars': 'off',
'@typescript-eslint/no-namespace': 'off'
},
root: true
};
3 changes: 1 addition & 2 deletions lib/mappers/element.mapper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { enumHelper } from '@kontent-ai/core-sdk';
import { deliveryUrlHelper } from '../utilities';
import { deliveryUrlHelper, enumHelper } from '../utilities';

import { IDeliveryClientConfig } from '../config';
import { Contracts } from '../contracts';
Expand Down
62 changes: 62 additions & 0 deletions lib/utilities/enum.helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
export class EnumHelper {
getAllNames(T: any): any[] {
const enumNames: any[] = [];

for (const key in T) {
if (T.hasOwnProperty(key)) {
enumNames.push(key);
}
}

return enumNames;
}

getAllValues(T: any): any[] {
const allEnumValues: any[] = Object.keys(T).map((key) => T[key]);

return allEnumValues;
}

getEnumFromValue<T>(T: any, value: string | number): T | undefined {
try {
if (!value) {
return undefined;
}

// we can map back from index number directly
if (this.isNumeric(value)) {
return <T>T[value];
}

// for strings, we need to compare each value separately
const allEnumValues = this.getAllValues(T);

const result = allEnumValues.find((m) => m.toLowerCase() === value.toString().toLowerCase());

if (!result) {
return undefined;
}

return result as T;
} catch (err) {
return undefined;
}
}

getEnumFromName<T>(T: any, name: string): T | undefined {
const allNames = this.getAllNames(T);

for (const enumName of allNames) {
if (enumName.toLowerCase() === name.toLowerCase()) {
return T[enumName];
}
}
return undefined;
}

private isNumeric(value: any): boolean {
return !isNaN(parseFloat(value)) && isFinite(value);
}
}

export const enumHelper = new EnumHelper();
3 changes: 2 additions & 1 deletion lib/utilities/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './linked-items.helper';
export * from './guid.helper';
export * from './delivery-url.helper';
export * from './text-helper';
export * from './text.helper';
export * from './enum.helper';
File renamed without changes.
Loading

0 comments on commit 288cbb7

Please sign in to comment.