Skip to content

Commit

Permalink
useProps: Add doc comment
Browse files Browse the repository at this point in the history
  • Loading branch information
earthboundkid committed Feb 19, 2024
1 parent 8b38518 commit 111ca06
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/utils/use-props.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import { ref } from "vue";

export default function useProps(src, props) {
// useProps takes an object and turns select props into refs.
// The mapping is an object whose keys are turned into names on returned data object.
// The values of the mapping are the names of the props on source object
// and then optional transformations to do on deserialize and serialize.
export default function useProps(src, mapping) {
const data = {};
for (let [prop, [name, wrap = (v) => v]] of Object.entries(props)) {
data[prop] = ref(wrap(src[name]));
for (let [prop, [name, deserialize = (v) => v]] of Object.entries(mapping)) {
data[prop] = ref(deserialize(src[name]));
}
function saveData() {
let dst = {};
for (let [prop, [name, , unwrap = (v) => v]] of Object.entries(props)) {
dst[name] = unwrap(data[prop].value);
for (let [prop, [name, , serialize = (v) => v]] of Object.entries(
mapping
)) {
dst[name] = serialize(data[prop].value);
}
return dst;
}
Expand Down

0 comments on commit 111ca06

Please sign in to comment.