From ae04f7599c822dec52af60c51aeb2ab135818c5d Mon Sep 17 00:00:00 2001 From: Ulrich-Matthias Schäfer Date: Thu, 8 Nov 2018 12:59:03 +0100 Subject: implements `round()` (#916) --- src/elements/Dom.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'src') diff --git a/src/elements/Dom.js b/src/elements/Dom.js index e31c15c..75f3e94 100644 --- a/src/elements/Dom.js +++ b/src/elements/Dom.js @@ -188,6 +188,25 @@ export default class Dom extends EventTarget { return element } + round (precision = 2, map) { + const factor = 10 ** precision + const attrs = this.attr() + + // If we have no map, build one from attrs + if (!map) { + map = Object.keys(attrs) + } + + // Holds rounded attributes + const newAttrs = {} + map.forEach((key) => { + newAttrs[key] = Math.round(attrs[key] * factor) / factor + }) + + this.attr(newAttrs) + return this + } + // Return id on string conversion toString () { return this.id() -- cgit v1.2.3