39 lines
688 B
JavaScript
39 lines
688 B
JavaScript
/*
|
|
* Copyright (C) Sapphirecode - All Rights Reserved
|
|
* Created by Timo Hocker <timo@scode.ovh>, March 2020
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
/**
|
|
* truncates a floating point number
|
|
*
|
|
* @param {number} num number to truncate
|
|
* @param {number} len length to truncate to
|
|
* @returns {number} truncated number
|
|
*/
|
|
function truncate_decimal (num, len) {
|
|
return Math.round (num * (10 ** len)) / (10 ** len);
|
|
}
|
|
|
|
/**
|
|
* parse json and catch invalid strings
|
|
*
|
|
* @param {string} text input
|
|
* @returns {any} parsed
|
|
*/
|
|
function try_parse_json (text) {
|
|
try {
|
|
return JSON.parse (text);
|
|
}
|
|
catch (e) {
|
|
// noop
|
|
}
|
|
return null;
|
|
}
|
|
|
|
module.exports = {
|
|
truncate_decimal,
|
|
try_parse_json
|
|
};
|