Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
candidates.sort((l1, l2) => {
let current = { y: currentLocation.value.lat, x: currentLocation.value.lon };
l1 = { y: l1.lat, x: l1.lon };
l2 = { y: l2.lat, x: l2.lon };
return ThingTalk.Builtin.distance(l1, current) - ThingTalk.Builtin.distance(l2, current);
});
}
_findInCache(fnid, params) {
const fn = this._functions[fnid];
for (let cached of this._execCache) {
let [kind, channel, cachedparams, result] = cached;
if (kind === fn.selector.kind && channel === fn.channel &&
ThingTalk.Builtin.equality(cachedparams, params))
return result;
}
return null;
}
clearGetCache() {
marshal(value) {
if (value === null || value === undefined)
return null;
if (value instanceof Array)
return value.map(ValueProto.marshal);
else if (value instanceof ThingTalk.Builtin.Entity)
return { tag: 'entity', v: value.value, d: value.display };
else if (value instanceof ThingTalk.Builtin.Currency)
return { tag: 'currency', v: value.value, c: value.code };
else if (value instanceof ThingTalk.Builtin.Location)
return { tag: 'loc', x: value.x, y: value.y, d: value.display };
else if (value instanceof ThingTalk.Builtin.Time)
return { tag: 'time', h: value.hour, m: value.minute, s: value.second };
else if (value instanceof Date)
return { tag: 'date', v: value.getTime() };
else
return value;
},
unmarshal(value) {
if (value === null || value === undefined)
return null;
if (value instanceof Array) {
return value.map(ValueProto.unmarshal);
} else {
switch (value.tag) {
case 'date':
return new Date(value.v);
case 'currency':
return new ThingTalk.Builtin.Currency(value.v, value.c);
case 'entity':
return new ThingTalk.Builtin.Entity(value.v, value.d);
case 'loc':
return new ThingTalk.Builtin.Location(value.y, value.x, value.d);
case 'time':
return new ThingTalk.Builtin.Time(value.h, value.m, value.s);
default:
return value;
}
}
}
};
function extractOne(result) {
let extracted = {};
for (let arg of block.args) {
if (arg.is_input)
continue;
if (arg.json_key)
extracted[arg.name] = get(result, arg.json_key);
else
extracted[arg.name] = result[arg.name];
if (arg.type === 'Date')
extracted[arg.name] = new Date(extracted[arg.name]);
else if (arg.type === 'Currency' && typeof extracted[arg.name] === 'number')
extracted[arg.name] = new ThingTalk.Builtin.Currency(extracted[arg.name], 'usd');
else if (arg.type === 'Currency' && typeof extracted[arg.name] === 'string')
extracted[arg.name] = new ThingTalk.Builtin.Currency(parseFloat(extracted[arg.name]), 'usd');
}
return extracted;
}