updates types and refactors object key transform functions. fixes properties passed into retention policy

This commit is contained in:
Seyi Adebajo 2018-07-31 13:31:57 -07:00
parent 9738df2984
commit acef146bfd
7 changed files with 49 additions and 35 deletions

View File

@ -306,7 +306,7 @@ export default class DatasetComplianceContainer extends Component {
await this.notifyOnSave<void>(
saveDatasetComplianceByUrn(get(this, 'urn'), {
...complianceInfo,
// filter out readonly entities, then fleece readonly attribute from remaining entities before save
// filter out readonly entities, then omit readonly attribute from remaining entities before save
complianceEntities: removeReadonlyAttr(editableTags(complianceEntities))
})
);

View File

@ -3,7 +3,7 @@ import { IdLogicalType, PurgePolicy } from 'wherehows-web/constants/index';
import { IComplianceEntity, IComplianceInfo } from 'wherehows-web/typings/api/datasets/compliance';
import { IComplianceDataType } from 'wherehows-web/typings/api/list/compliance-datatypes';
import { arrayEvery, arrayFilter, arrayMap, arrayReduce, arraySome, reduceArrayAsync } from 'wherehows-web/utils/array';
import { fleece, hasEnumerableKeys } from 'wherehows-web/utils/object';
import { omit, hasEnumerableKeys } from 'wherehows-web/utils/object';
import { lastSeenSuggestionInterval } from 'wherehows-web/constants/metadata-acquisition';
import { decodeUrn, isValidCustomValuePattern } from 'wherehows-web/utils/validators/urn';
import {
@ -120,7 +120,7 @@ const editableTags = (entities: Array<IComplianceEntity>): Array<IComplianceEnti
* @type {(entities: Array<IComplianceEntity>) => Array<IComplianceEntity>}
*/
const removeReadonlyAttr = <(entities: Array<IComplianceEntity>) => Array<IComplianceEntity>>arrayMap(
fleece<IComplianceEntity, 'readonly'>(['readonly'])
(entity: IComplianceEntity) => omit(entity, ['readonly'])
);
/**

View File

@ -2,7 +2,7 @@ import Service from '@ember/service';
import { setProperties, get, set } from '@ember/object';
import { delay } from 'wherehows-web/utils/promise-delay';
import { action } from '@ember-decorators/object';
import { fleece } from 'wherehows-web/utils/object';
import { omit } from 'wherehows-web/utils/object';
import { notificationDialogActionFactory } from 'wherehows-web/utils/notifications/notifications';
import { noop } from 'wherehows-web/utils/helpers/functions';
@ -155,14 +155,9 @@ const notificationHandlers: INotificationHandler = {
props = { dismissButtonText: 'No', confirmButtonText: 'Yes', ...props };
const { dismissButtonText, confirmButtonText, onDialogToggle } = props;
// Removes dismiss or confirm buttons if set to false
let resolvedProps: IConfirmOptions =
dismissButtonText === false
? <IConfirmOptions>fleece<IConfirmOptions, 'dismissButtonText'>(['dismissButtonText'])(props)
: props;
resolvedProps =
confirmButtonText === false
? <IConfirmOptions>fleece<IConfirmOptions, 'confirmButtonText'>(['confirmButtonText'])(props)
: props;
let resolvedProps: IConfirmOptions = dismissButtonText === false ? omit(props, ['dismissButtonText']) : props;
resolvedProps = confirmButtonText === false ? omit(props, ['confirmButtonText']) : props;
resolvedProps = typeof onDialogToggle === 'function' ? props : { ...props, onDialogToggle: noop };
return {

View File

@ -10,7 +10,7 @@ import { datasetUrlByUrn } from 'wherehows-web/utils/api/datasets/shared';
import { getJSON, postJSON } from 'wherehows-web/utils/api/fetcher';
import { getApiRoot, ApiStatus } from 'wherehows-web/utils/api/shared';
import { arrayFilter, arrayMap } from 'wherehows-web/utils/array';
import { fleece } from 'wherehows-web/utils/object';
import { omit } from 'wherehows-web/utils/object';
/**
* Defines a string enum for valid owner types
@ -149,7 +149,7 @@ const readDatasetSuggestedOwnersByUrn = async (urn: string): Promise<IOwnerRespo
* @return {Promise<void>}
*/
const updateDatasetOwnersByUrn = (urn: string, csrfToken: string = '', updatedOwners: Array<IOwner>): Promise<{}> => {
const ownersWithoutModifiedTime = arrayMap(fleece<IOwner, 'modifiedTime'>(['modifiedTime']));
const ownersWithoutModifiedTime = arrayMap((owner: IOwner) => omit(owner, ['modifiedTime']));
return postJSON<{}>({
url: datasetOwnersUrlByUrn(urn),

View File

@ -2,7 +2,7 @@ import { getJSON, postJSON } from 'wherehows-web/utils/api/fetcher';
import { datasetUrlByUrn } from 'wherehows-web/utils/api/datasets/shared';
import { IDatasetRetention, IGetDatasetRetentionResponse } from 'wherehows-web/typings/api/datasets/retention';
import { isNotFoundApiError } from 'wherehows-web/utils/api';
import { fleece } from 'wherehows-web/utils/object';
import { omit } from 'wherehows-web/utils/object';
import { encodeUrn } from 'wherehows-web/utils/validators/urn';
/**
@ -38,7 +38,7 @@ const readDatasetRetentionByUrn = async (urn: string): Promise<IGetDatasetRetent
const saveDatasetRetentionByUrn = (urn: string, retention: IDatasetRetention): Promise<IDatasetRetention> =>
postJSON<IDatasetRetention>({
url: datasetRetentionUrlByUrn(urn),
data: fleece<IDatasetRetention, 'modifiedTime' | 'modifiedBy'>(['modifiedBy', 'modifiedTime'])(retention)
data: omit(retention, ['modifiedBy', 'modifiedTime'])
});
export { readDatasetRetentionByUrn, saveDatasetRetentionByUrn };

View File

@ -1,6 +1,6 @@
import { IComplianceInfo } from 'wherehows-web/typings/api/datasets/compliance';
import { IDatasetRetention } from 'wherehows-web/typings/api/datasets/retention';
import { fleece } from 'wherehows-web/utils/object';
import { pick } from 'wherehows-web/utils/object';
/**
* Extracts values from an IComplianceInfo instance to create an instance of IDatasetRetention
@ -8,11 +8,11 @@ import { fleece } from 'wherehows-web/utils/object';
* @returns {IDatasetRetention}
*/
const extractRetentionFromComplianceInfo = (complianceInfo: IComplianceInfo): IDatasetRetention => {
const { datasetUrn, compliancePurgeNote, complianceType } = <IComplianceInfo>fleece<IComplianceInfo>([
const { datasetUrn, compliancePurgeNote, complianceType } = pick(complianceInfo, [
'complianceType',
'compliancePurgeNote',
'datasetUrn'
])(complianceInfo);
]);
return {
purgeNote: compliancePurgeNote,

View File

@ -1,3 +1,18 @@
import { arrayReduce } from 'wherehows-web/utils/array';
/**
* Aliases the exclusion / diff conditional type that specifies that an object
* contains properties from T, that are not in K
* @alias
*/
type Omit<T, K> = Pick<T, Exclude<keyof T, K>>;
/**
* Aliases the selection / extract conditional type that specifies that an object
* contains properties from T, that are in K
*/
type Select<T, K> = Pick<T, Extract<keyof T, K>>;
/**
* Checks if a type is an object
* @param {any} candidate the entity to check
@ -12,30 +27,34 @@ const isObject = (candidate: any): candidate is object =>
*/
const hasEnumerableKeys = (object: any): boolean => isObject(object) && !!Object.keys(object).length;
/**
* Function interface for a identity or partial return function
* @interface IPartialOrIdentityTypeFn
* @template T
*/
interface IPartialOrIdentityTypeFn<T> {
(o: T): Partial<T> | T;
}
/**
* Non mutative object attribute deletion. Removes the specified keys from a copy of the object and returns the copy.
* @template T the object type to drop keys from
* @template K the keys to be dropped from the object
* @param {Array<K>} [droppedKeys=[]] the list of attributes on T to be dropped
* @returns {IPartialOrIdentityTypeFn<T>}
* @param {T} o
* @param {Array<K extends keyof T>} droppedKeys
* @return {Pick<T, Exclude<keyof T, K extends keyof T>>}
*/
const fleece = <T, K extends keyof T = keyof T>(droppedKeys: Array<K> = []): IPartialOrIdentityTypeFn<T> => (
o: T
): Partial<T> | T => {
const omit = <T, K extends keyof T>(o: T, droppedKeys: Array<K>): Omit<T, K> => {
const partialResult = Object.assign({}, o);
return droppedKeys.reduce((partial, key) => {
return arrayReduce((partial: T, key: K) => {
delete partial[key];
return partial;
}, partialResult);
}, partialResult)(droppedKeys);
};
export { isObject, hasEnumerableKeys, fleece };
/**
* Extracts keys from a source to a new object
* @template T the object to select keys from
* @param {T} o the source object
* @param {Array<K extends keyof T>} pickedKeys
* @return {Select<T extends object, K extends keyof T>}
*/
const pick = <T extends object, K extends keyof T>(o: T, pickedKeys: Array<K>): Select<T, K> =>
arrayReduce(
(partial: T, key: K) => (pickedKeys.includes(key) ? Object.assign(partial, { [key]: o[key] }) : partial),
<T>{}
)(pickedKeys);
export { isObject, hasEnumerableKeys, omit, pick };