Skip to content

Commit

Permalink
✨ Add Field#castContainedValues() method
Browse files Browse the repository at this point in the history
  • Loading branch information
skerit committed Feb 13, 2024
1 parent 519808e commit b4ef97a
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion lib/class/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,41 @@ Field.setMethod(function getRecordValue(record, path_to_value_hint) {
});

/**
* Cast the given value to this field's type
* Cast the value (could be in a container)
*
* @author Jelle De Loecker <[email protected]>
* @since 1.4.0
* @version 1.4.0
*
* @param {*} value
* @param {boolean} to_datasource Is this meant for the datasource?
*
* @return {*}
*/
Field.setMethod(function castContainedValues(value, to_datasource) {

if (this.is_translatable && value && this.is_translatable) {
let prefix;

for (prefix in value) {
value[prefix] = this.cast(value[prefix], to_datasource);
}
} else if (this.is_array && value) {
let i;

for (i = 0; i < value.length; i++) {
value[i] = this.cast(value[i], to_datasource);
}
} else {
value = this.cast(value, to_datasource);
}

return value;
});

/**
* Cast the given value to this field's type.
* Containers should not be passed (like the array or translation object container)
*
* @author Jelle De Loecker <[email protected]>
* @since 0.2.0
Expand Down

0 comments on commit b4ef97a

Please sign in to comment.