Skip to content

Commit

Permalink
feat(util): remove mapping functions
Browse files Browse the repository at this point in the history
  • Loading branch information
manushak committed Aug 21, 2024
1 parent 24b585d commit f5d7043
Showing 1 changed file with 0 additions and 68 deletions.
68 changes: 0 additions & 68 deletions src/common/util/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {createInterface} from 'node:readline/promises';
import {exec} from 'child_process';
import * as path from 'path';
import {promisify} from 'util';
import {MappingParams, PluginParams} from '@grnsft/if-core/types';

/**
* Promise version of Node's `exec` from `child-process`.
Expand Down Expand Up @@ -64,70 +63,3 @@ export const parseManifestFromStdin = async () => {

return match![1];
};

/**
* Maps input data if the mapping has valid data.
*/
export const mapInputIfNeeded = (
input: PluginParams,
mapping: MappingParams
) => {
const newInput = Object.assign({}, input);

Object.entries(mapping || {}).forEach(([key, value]) => {
if (value in newInput) {
const mappedKey = input[value];
newInput[key] = mappedKey;
delete newInput[value];
}
});

return newInput;
};

/**
* Maps config data if the mapping hass valid data.
*/
export const mapConfigIfNeeded = (config: any, mapping: MappingParams) => {
if (!mapping) {
return config;
}

if (typeof config !== 'object' || config === null) {
return config;
}

const result: Record<string, any> = Array.isArray(config) ? [] : {};

Object.entries(config).forEach(([key, value]) => {
const mappedKey = mapping[key] || key;

if (typeof value === 'object' && value !== null) {
result[mappedKey] = mapConfigIfNeeded(value, mapping);
} else {
result[mappedKey] =
typeof value === 'string' && value in mapping ? mapping[value] : value;
}
});

return result;
};

/**
* Maps the output parameter of the plugin if the `mapping` parameter is provided.
*/
export const mapOutputIfNeeded = (
output: PluginParams,
mapping: MappingParams
) => {
if (!mapping) return output;

return Object.entries(output).reduce((acc, [key, value]) => {
if (key in mapping) {
acc[mapping[key]] = value;
} else {
acc[key] = value;
}
return acc;
}, {} as PluginParams);
};

0 comments on commit f5d7043

Please sign in to comment.