Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function convertTimestamps(originVal, targetVal) {
if (originVal === '%convertTimestamp%') {
// firestore timestamps
// @ts-ignore
if (isWhat.isAnyObject(targetVal) && !isWhat.isObject(targetVal) && isWhat.isFunction(targetVal.toDate)) {
// @ts-ignore
return targetVal.toDate();
}
// strings
if (isWhat.isString(targetVal) && isWhat.isDate(new Date(targetVal))) {
return new Date(targetVal);
}
}
return targetVal;
}
/**
function convertTimestamps(originVal, targetVal) {
if (originVal === '%convertTimestamp%') {
// firestore timestamps
// @ts-ignore
if (isWhat.isAnyObject(targetVal) && !isWhat.isPlainObject(targetVal) && isWhat.isFunction(targetVal.toDate)) {
// @ts-ignore
return targetVal.toDate();
}
// strings
if (isWhat.isString(targetVal) && isWhat.isDate(new Date(targetVal))) {
return new Date(targetVal);
}
}
return targetVal;
}
/**
function convertTimestamps(originVal, targetVal) {
if (originVal === '%convertTimestamp%') {
// firestore timestamps
// @ts-ignore
if (isWhat.isAnyObject(targetVal) && !isWhat.isPlainObject(targetVal) && isWhat.isFunction(targetVal.toDate)) {
// @ts-ignore
return targetVal.toDate();
}
// strings
if (isWhat.isString(targetVal) && isWhat.isDate(new Date(targetVal))) {
return new Date(targetVal);
}
}
return targetVal;
}
/**
let res
box._sync.userId = 'charlie'
box._conf.sync.fillables = ['body', 'del', 'pathdel']
box._conf.sync.guard = []
// prepareForPatch
res = store.getters['pokemonBox/prepareForPatch'](['1', '2'], {body: 'new', del: Firebase.firestore.FieldValue.delete()})
t.deepEqual(Object.keys(res), ['1', '2'])
t.is(res['1'].body, 'new')
t.is(res['2'].body, 'new')
t.is(res['1'].del._methodName, 'FieldValue.delete')
t.is(res['2'].del._methodName, 'FieldValue.delete')
t.is(res['1'].id, '1')
t.is(res['2'].id, '2')
t.is(res['1'].updated_by, 'charlie')
t.is(res['2'].updated_by, 'charlie')
t.is(isDate(res['1'].updated_at), true)
t.is(isDate(res['2'].updated_at), true)
// prepareForPropDeletion
res = store.getters['pokemonBox/prepareForPropDeletion']('1.pathdel.a')
t.is(res['1'].id, '1')
t.is(res['1']['pathdel.a']._methodName, 'FieldValue.delete')
t.is(res['1'].updated_by, 'charlie')
t.is(isDate(res['1'].updated_at), true)
// deeper delete
res = store.getters['pokemonBox/prepareForPropDeletion']('1.a.met.de.aba')
t.is(res['1']['a.met.de.aba']._methodName, 'FieldValue.delete')
// different fillables & guard
box._conf.sync.guard = ['updated_at', 'updated_by', 'id']
res = store.getters['pokemonBox/prepareForPropDeletion']('1.pathdel.a')
t.is(res['1'].id, '1') // id stays even if it's added to guard
t.is(res['1']['pathdel.a']._methodName, 'FieldValue.delete')
t.is(res['1'].updated_by, undefined)
function convertTimestamps(originVal, targetVal) {
if (originVal === '%convertTimestamp%') {
// firestore timestamps
if (isObject(targetVal) && isFunction(targetVal.toDate)) {
return targetVal.toDate();
} // strings
if (isString(targetVal) && isDate(new Date(targetVal))) {
return new Date(targetVal);
}
}
return targetVal;
}
/**
function convertTimestamps(originVal, targetVal) {
if (originVal === '%convertTimestamp%') {
// firestore timestamps
// @ts-ignore
if (isAnyObject(targetVal) && !isPlainObject(targetVal) && isFunction(targetVal.toDate)) {
// @ts-ignore
return targetVal.toDate();
}
// strings
if (isString(targetVal) && isDate(new Date(targetVal))) {
return new Date(targetVal);
}
}
return targetVal;
}
/**
function convertTimestamps(originVal, targetVal) {
if (originVal === '%convertTimestamp%') {
// firestore timestamps
// @ts-ignore
if (isWhat.isAnyObject(targetVal) && !isWhat.isPlainObject(targetVal) && isWhat.isFunction(targetVal.toDate)) {
// @ts-ignore
return targetVal.toDate();
}
// strings
if (isWhat.isString(targetVal) && isWhat.isDate(new Date(targetVal))) {
return new Date(targetVal);
}
}
return targetVal;
}
/**
function convertTimestamps (originVal: any, targetVal: any): Date {
if (originVal === '%convertTimestamp%') {
// firestore timestamps
// @ts-ignore
if (isAnyObject(targetVal) && !isPlainObject(targetVal) && isFunction(targetVal.toDate)) {
// @ts-ignore
return targetVal.toDate()
}
// strings
if (isString(targetVal) && isDate(new Date(targetVal))) {
return new Date(targetVal)
}
}
return targetVal
}
test('my recursive object assign', async t => {
let res, defaultValues, target
const nd = new Date()
defaultValues = {body: 'a'}
target = {dueDate: nd}
res = setDefaultValues(target, defaultValues)
t.true(isDate(res.dueDate))
t.is(res.body, 'a')
t.deepEqual(defaultValues, {body: 'a'})
t.deepEqual(target, {dueDate: nd})
defaultValues = {
body: '',
head: null,
toes: {big: true},
fingers: {'12': false}
}
target = {body: {}, head: {}, toes: {}, fingers: null}
res = setDefaultValues(target, defaultValues)
t.deepEqual(res, {body: {}, head: {}, toes: {big: true}, fingers: null})
defaultValues = {body: 'a'}
target = {body: 'b'}
time: 'now',
newDate,
very: {
deep: {prop: false}
}
}
})
t.deepEqual(target, {
info: {
date: 'tomorrow',
very: {
deep: {prop: true}
}
}
})
t.true(isDate(res.info.newDate))
defaultValues = {
info: {
time: {when: 'now'},
very: {
deep: {prop: false}
}
}
}
target = {
info: {
time: {},
very: {whole: 1}
}
}
res = setDefaultValues(target, defaultValues)