mirror of
https://github.com/strapi/strapi.git
synced 2025-07-21 16:10:18 +00:00
40 lines
658 B
JavaScript
40 lines
658 B
JavaScript
![]() |
'use strict';
|
||
|
|
||
|
const { toString } = require('lodash/fp');
|
||
|
|
||
|
const Field = require('./field');
|
||
|
|
||
|
class BooleanField extends Field {
|
||
|
toDB(value) {
|
||
|
if (typeof value === 'boolean') return value;
|
||
|
|
||
|
if (['true', 't', '1', 1].includes(value)) {
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
if (['false', 'f', '0', 0].includes(value)) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
return Boolean(value);
|
||
|
}
|
||
|
|
||
|
fromDB(value) {
|
||
|
if (typeof value === 'boolean') {
|
||
|
return value;
|
||
|
}
|
||
|
|
||
|
const strVal = toString(value);
|
||
|
|
||
|
if (strVal === '1') {
|
||
|
return true;
|
||
|
}
|
||
|
if (strVal === '0') {
|
||
|
return false;
|
||
|
}
|
||
|
return null;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
module.exports = BooleanField;
|