Skip to content

Commit

Permalink
new format for changes
Browse files Browse the repository at this point in the history
  • Loading branch information
snewcomer committed Mar 21, 2022
1 parent 02dfaac commit 8105923
Showing 1 changed file with 14 additions and 58 deletions.
72 changes: 14 additions & 58 deletions src/validated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import {
INotifier,
InternalMap,
NewProperty,
PrepareChangesFn,
Snapshot,
ValidationErr,
ValidatorKlass
Expand Down Expand Up @@ -55,8 +54,18 @@ function maybeUnwrapProxy(content: Content): any {
return content;
}

// function getKeyValues(obj) {
// }
export function newFormat(obj: Record<string, any>[]): Record<string, any> {
let newFormat: Record<string, any> = {};
for (let item of obj) {
const { key, value } = item;
newFormat[key] = {
current: value,
// original: ,
}
}

return newFormat;
}

// This is intended to provide an alternative changeset structure compatible with `yup`
// This slims down the set of features, including removed APIs and `validate` returns just the `validate()` method call and requires users to manually add errors.
Expand Down Expand Up @@ -157,15 +166,6 @@ export class ValidatedChangeset {
return (obj[key] = value);
}

get _bareChanges() {
let obj = this[CHANGES];

return getKeyValues(obj).reduce((newObj: { [key: string]: any }, { key, value }) => {
newObj[key] = value;
return newObj;
}, Object.create(null));
}

/**
* @property changes
* @type {Array}
Expand All @@ -177,7 +177,7 @@ export class ValidatedChangeset {
// original: 0,
// current: 1,
// }
return getKeyValues(obj);
return newFormat(getKeyValues(obj));
}

/**
Expand All @@ -187,8 +187,7 @@ export class ValidatedChangeset {
get errors() {
let obj = this[ERRORS];

// [{ key, validation, value }, ...]
return getKeyErrorValues(obj);
return newFormat(getKeyValues(obj));
}

get change() {
Expand Down Expand Up @@ -285,49 +284,6 @@ export class ValidatedChangeset {
return `changeset:${normalisedContent.toString()}`;
}

/**
* Provides a function to run before emitting changes to the model. The
* callback function must return a hash in the same shape:
*
* ```
* changeset
* .prepare((changes) => {
* let modified = {};
*
* for (let key in changes) {
* modified[underscore(key)] = changes[key];
* }
*
* return modified; // { first_name: "Jim", last_name: "Bob" }
* })
* .execute(); // execute the changes
* ```
*
* @method prepare
*/
prepare(prepareChangesFn: PrepareChangesFn): this {
let changes: { [s: string]: any } = this._bareChanges;
let preparedChanges = prepareChangesFn(changes);

assert('Callback to `changeset.prepare` must return an object', this.isObject(preparedChanges));

let newObj: Changes = {};
if (this.isObject(preparedChanges)) {
let newChanges: Changes = keys(preparedChanges as Record<string, any>).reduce(
(newObj: Changes, key: keyof Changes) => {
newObj[key] = new Change((preparedChanges as Record<string, any>)[key]);
return newObj;
},
newObj
);

// @tracked
this[CHANGES] = newChanges;
}

return this;
}

/**
* Executes the changeset if in a valid state.
*
Expand Down

0 comments on commit 8105923

Please sign in to comment.