Skip to content
This repository has been archived by the owner on Jun 21, 2023. It is now read-only.

Commit

Permalink
Merge pull request #8 from AckeeCZ/refactor/serializer-paths
Browse files Browse the repository at this point in the history
Refactor serializers - remove regexp
  • Loading branch information
smolijar committed Jan 11, 2019
2 parents ae48ebc + ce0a0c2 commit 8ff2207
Showing 1 changed file with 7 additions and 24 deletions.
31 changes: 7 additions & 24 deletions src/serializers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,12 @@ const serializers: Dictionary<SerializerFn> = {
},
};

const disablePaths = (paths: string[] | undefined) => {
if (!paths || paths.length <= 0) {
return;
}
const sliceByPrefix = (prefix: string, paths?: string[]) =>
(paths || []).filter(field => field.startsWith(prefix)).map(field => field.slice(prefix.length));

const disablePaths = (paths?: string[]) => {
forEach(serializers, (value, key) => {
const matcher = new RegExp(`^${key}.(.*)`);
const affectedFields: string[] = [];
paths.forEach(field => {
const res = field.match(matcher);
if (res !== null) {
affectedFields.push(res[1]);
}
});
const affectedFields = sliceByPrefix(`${key}.`, paths);

if (affectedFields.length > 0) {
const newSerializer: SerializerFn = (obj: Dictionary<any>) => {
Expand All @@ -77,19 +70,9 @@ const disablePaths = (paths: string[] | undefined) => {
});
};

const enablePaths = (paths: string[] | undefined) => {
if (!paths || paths.length <= 0) {
return;
}
const enablePaths = (paths?: string[]) => {
forEach(serializers, (value, key) => {
const matcher = new RegExp(`^${key}.(.*)`);
const affectedFields: string[] = [];
paths.forEach(field => {
const res = field.match(matcher);
if (res !== null) {
affectedFields.push(res[1]);
}
});
const affectedFields = sliceByPrefix(`${key}.`, paths);

if (affectedFields.length > 0) {
const newSerializer: SerializerFn = (obj: Dictionary<any>) => {
Expand Down

0 comments on commit 8ff2207

Please sign in to comment.