@astro-utils/forms
Version:
Server component for Astro (call server functions from client side with validation and state management)
40 lines (39 loc) • 1.03 kB
JavaScript
/**
* Returns the difference between two objects
*/
export function diffProps(object1, object2, skipKeys = []) {
const diff = {};
for (const [key, value] of Object.entries(object2)) {
if (skipKeys.includes(key))
continue;
if (JSON.stringify(object1[key]) !== JSON.stringify(value)) {
diff[key] = value;
}
}
return diff;
}
export function getSomeProps(object, props) {
if (props === true) {
return object;
}
const result = {};
for (const prop of props) {
result[prop] = object[prop];
}
return result;
}
/**
* Omit properties from an object, any property that starts with an underscore or is in the props array will be omitted
*/
export function omitProps(object, props) {
if (props === true) {
return {};
}
const result = { ...object };
for (const prop of Object.keys(object)) {
if (props.includes(prop) || prop[0] === '_') {
delete result[prop];
}
}
return result;
}