diff --git a/ui/js/component/common.js b/ui/js/component/common.js index a39abc802..16ac2497e 100644 --- a/ui/js/component/common.js +++ b/ui/js/component/common.js @@ -1,5 +1,5 @@ import React from "react"; -import { formatCredits, formatFullPrice } from "utils"; +import { formatCredits, formatFullPrice } from "util/formatCredits"; import lbry from "../lbry.js"; //component/icon.js diff --git a/ui/js/component/header/index.js b/ui/js/component/header/index.js index 8c8c1f96a..464a471c8 100644 --- a/ui/js/component/header/index.js +++ b/ui/js/component/header/index.js @@ -1,5 +1,5 @@ import React from "react"; -import { formatCredits } from "utils"; +import { formatCredits } from "util/formatCredits"; import { connect } from "react-redux"; import { selectIsBackDisabled, selectIsForwardDisabled } from "selectors/app"; import { selectBalance } from "selectors/wallet"; diff --git a/ui/js/lbry.js b/ui/js/lbry.js index 045fdb21f..f55685074 100644 --- a/ui/js/lbry.js +++ b/ui/js/lbry.js @@ -1,8 +1,28 @@ -import lbryio from "./lbryio.js"; import lighthouse from "./lighthouse.js"; import jsonrpc from "./jsonrpc.js"; import lbryuri from "./lbryuri.js"; -import { getLocal, getSession, setSession, setLocal } from "./utils.js"; + +/** + * The 4 get/set functions below used to be in a utils.js library when used more widely. + * They've been reduced to just this file and probably ought to be eliminated entirely. + */ +function getLocal(key, fallback = undefined) { + const itemRaw = localStorage.getItem(key); + return itemRaw === null ? fallback : JSON.parse(itemRaw); +} + +function setLocal(key, value) { + localStorage.setItem(key, JSON.stringify(value)); +} + +function getSession(key, fallback = undefined) { + const itemRaw = sessionStorage.getItem(key); + return itemRaw === null ? fallback : JSON.parse(itemRaw); +} + +function setSession(key, value) { + sessionStorage.setItem(key, JSON.stringify(value)); +} const { remote, ipcRenderer } = require("electron"); const menu = remote.require("./menu/main-menu"); diff --git a/ui/js/reducers/user.js b/ui/js/reducers/user.js index 2f03def9e..02203cdfe 100644 --- a/ui/js/reducers/user.js +++ b/ui/js/reducers/user.js @@ -1,5 +1,4 @@ import * as types from "constants/action_types"; -import { getLocal } from "utils"; const reducers = {}; diff --git a/ui/js/util/formatCredits.js b/ui/js/util/formatCredits.js new file mode 100644 index 000000000..30d36ce98 --- /dev/null +++ b/ui/js/util/formatCredits.js @@ -0,0 +1,24 @@ +export function formatCredits(amount, precision) { + return amount.toFixed(precision || 1).replace(/\.?0+$/, ""); +} + +export function formatFullPrice(amount, precision) { + let formated = ""; + + const quantity = amount.toString().split("."); + const fraction = quantity[1]; + + if (fraction) { + // Set precision + precision = precision || 1; + + const decimals = fraction.split(""); + const first = decimals.filter(number => number != "0")[0]; + const index = decimals.indexOf(first); + + // Set format fraction + formated = "." + fraction.substring(0, index + precision); + } + + return parseFloat(quantity[0] + formated); +} diff --git a/ui/js/utils.js b/ui/js/utils.js deleted file mode 100644 index 67d3ed804..000000000 --- a/ui/js/utils.js +++ /dev/null @@ -1,56 +0,0 @@ -/** - * Thin wrapper around localStorage.getItem(). Parses JSON and returns undefined if the value - * is not set yet. - */ -export function getLocal(key, fallback = undefined) { - const itemRaw = localStorage.getItem(key); - return itemRaw === null ? fallback : JSON.parse(itemRaw); -} - -/** - * Thin wrapper around localStorage.setItem(). Converts value to JSON. - */ -export function setLocal(key, value) { - localStorage.setItem(key, JSON.stringify(value)); -} - -/** - * Thin wrapper around localStorage.getItem(). Parses JSON and returns undefined if the value - * is not set yet. - */ -export function getSession(key, fallback = undefined) { - const itemRaw = sessionStorage.getItem(key); - return itemRaw === null ? fallback : JSON.parse(itemRaw); -} - -/** - * Thin wrapper around localStorage.setItem(). Converts value to JSON. - */ -export function setSession(key, value) { - sessionStorage.setItem(key, JSON.stringify(value)); -} - -export function formatCredits(amount, precision) { - return amount.toFixed(precision || 1).replace(/\.?0+$/, ""); -} - -export function formatFullPrice(amount, precision) { - let formated = ""; - - const quantity = amount.toString().split("."); - const fraction = quantity[1]; - - if (fraction) { - // Set precision - precision = precision || 1; - - const decimals = fraction.split(""); - const first = decimals.filter(number => number != "0")[0]; - const index = decimals.indexOf(first); - - // Set format fraction - formated = "." + fraction.substring(0, index + precision); - } - - return parseFloat(quantity[0] + formated); -}