🚚 Refactored section helpers into own file

This commit is contained in:
Alicia Sykes 2021-12-29 14:31:11 +00:00
parent c650743384
commit aad7ab9b1f
6 changed files with 88 additions and 57 deletions

View File

@ -12,7 +12,7 @@
<script>
import { shouldBeVisible } from '@/utils/MiscHelpers';
import { shouldBeVisible } from '@/utils/SectionHelpers';
export default {
name: 'Footer',

View File

@ -13,7 +13,7 @@
<script>
import PageTitle from '@/components/PageStrcture/PageTitle.vue';
import Nav from '@/components/PageStrcture/Nav.vue';
import { shouldBeVisible } from '@/utils/MiscHelpers';
import { shouldBeVisible } from '@/utils/SectionHelpers';
export default {
name: 'Header',

View File

@ -4,7 +4,7 @@ import Vuex from 'vuex';
import Keys from '@/utils/StoreMutations';
import ConfigAccumulator from '@/utils/ConfigAccumalator';
import { componentVisibility } from '@/utils/ConfigHelpers';
import { applyItemId } from '@/utils/MiscHelpers';
import { applyItemId } from '@/utils/SectionHelpers';
import filterUserSections from '@/utils/CheckSectionVisibility';
import { InfoHandler, InfoKeys } from '@/utils/ErrorHandler';

View File

@ -13,7 +13,7 @@ import {
layout as defaultLayout,
} from '@/utils/defaults';
import ErrorHandler from '@/utils/ErrorHandler';
import { applyItemId } from '@/utils/MiscHelpers';
import { applyItemId } from '@/utils/SectionHelpers';
import conf from '../../public/conf.yml';
export default class ConfigAccumulator {

View File

@ -1,8 +1,5 @@
/* A collection of generic reusable functions for various string processing tasks */
/* eslint-disable arrow-body-style */
import { hideFurnitureOn } from '@/utils/defaults';
/* Returns false if page furniture should be hidden on said route */
export const shouldBeVisible = (routeName) => !hideFurnitureOn.includes(routeName);
/* Very rudimentary hash function for generative icons */
export const asciiHash = (input) => {
@ -27,31 +24,6 @@ export const sanitize = (string) => {
return string.replace(reg, (match) => (map[match]));
};
/* Based on section title, item name and index, return a string value for ID */
const makeItemId = (sectionStr, itemStr, index) => {
const charSum = sectionStr.split('').map((a) => a.charCodeAt(0)).reduce((x, y) => x + y);
const itemTitleStr = itemStr.replace(/\s+/g, '-').replace(/[^a-zA-Z ]/g, '').toLowerCase();
return `${index}_${charSum}_${itemTitleStr}`;
};
/* Given an array of sections, apply a unique ID to each item, and return modified array */
export const applyItemId = (inputSections) => {
const sections = inputSections || [];
sections.forEach((sec, secIdx) => {
if (sec.items) {
sec.items.forEach((item, itemIdx) => {
sections[secIdx].items[itemIdx].id = makeItemId(sec.name, item.title, itemIdx);
});
}
if (sec.widgets) {
sec.widgets.forEach((widget, widgetIdx) => {
sections[secIdx].widgets[widgetIdx].id = makeItemId(sec.name, widget.type, widgetIdx);
});
}
});
return sections;
};
/* Given a timestamp, returns formatted date, in local format */
export const timestampToDate = (timestamp) => {
const localFormat = navigator.language;
@ -74,30 +46,6 @@ export const timestampToDateTime = (timestamp) => {
return `${timestampToDate(timestamp)} at ${timestampToTime(timestamp)}`;
};
/* Given a currency code, return the corresponding unicode symbol */
export const findCurrencySymbol = (currencyCode) => {
const code = currencyCode.toUpperCase().trim();
const currencies = {
USD: '$', // US Dollar
EUR: '€', // Euro
CRC: '₡', // Costa Rican Colón
GBP: '£', // British Pound Sterling
ILS: '₪', // Israeli New Sheqel
INR: '₹', // Indian Rupee
JPY: '¥', // Japanese Yen
KRW: '₩', // South Korean Won
NGN: '₦', // Nigerian Naira
PHP: '₱', // Philippine Peso
PLN: 'zł', // Polish Zloty
PYG: '₲', // Paraguayan Guarani
THB: '฿', // Thai Baht
UAH: '₴', // Ukrainian Hryvnia
VND: '₫', // Vietnamese Dong
};
if (currencies[code]) return currencies[code];
return code;
};
/* Given a 2-digit country code, return path to flag image from Flagpedia */
export const getCountryFlag = (countryCode, dimens) => {
const protocol = 'https';
@ -108,6 +56,12 @@ export const getCountryFlag = (countryCode, dimens) => {
return `${protocol}://${cdn}/${dimensions}/${country}.${ext}`;
};
/* Given a currency code, return path to corresponding countries flag icon */
export const getCurrencyFlag = (currency) => {
const cdn = 'https://raw.githubusercontent.com/transferwise/currency-flags';
return `${cdn}/master/src/flags/${currency.toLowerCase()}.png`;
};
/* Given a Latitude & Longitude object, and optional zoom level, return link to OSM */
export const getMapUrl = (location, zoom) => {
return `https://www.openstreetmap.org/#map=${zoom || 10}/${location.lat}/${location.lon}`;
@ -152,3 +106,49 @@ export const roundPrice = (price) => {
export const truncateStr = (str, len = 60, ellipse = '...') => {
return str.length > len + ellipse.length ? `${str.slice(0, len)}${ellipse}` : str;
};
/* Given a currency code, return the corresponding unicode symbol */
export const findCurrencySymbol = (currencyCode) => {
const code = currencyCode.toUpperCase().trim();
const currencies = {
USD: '$', // US Dollar
EUR: '€', // Euro
GBP: '£', // British Pound Sterling
AFN: '؋', // Afghan Afghani
ALL: 'Lek', // Albanian Lek
AUD: '$', // Australian Dollar
AWG: 'ƒ', // Aruban Guilder
BAM: 'KM', // Bosnian Mark
BWP: 'P', // Botswana Pula
CAD: '$', // Canadian Dollar
CNY: '¥', // Chinese Yuan Renminbi
CRC: '₡', // Costa Rican Colón
CRS: '₡', // Costa Rican Colon
CUP: '₱', // Cuban Peso
DKK: 'kr', // Danish Krone
HKD: '$', // Hong Kong Dollar
HUF: 'Ft', // Hungarian Forint
HRK: 'kn', // Croatian Kuna
ISK: 'kr', // Icelandic Krona
ILS: '₪', // Israeli New Sheqel
INR: '₹', // Indian Rupee
IRR: '﷼', // Iranian Rial
JPY: '¥', // Japanese Yen
KRW: '₩', // South Korean Won
LAK: '₭', // Laos Kip
NGN: '₦', // Nigerian Naira
NOK: 'kr', // Norwegian Krone
PHP: '₱', // Philippine Peso
PKR: '₨', // Pakistani Rupee
PLN: 'zł', // Polish Zloty
PYG: '₲', // Paraguayan Guarani
RUB: '₽', // Russian Ruble
THB: '฿', // Thai Baht
UAH: '₴', // Ukrainian Hryvnia
VND: '₫', // Vietnamese Dong
YER: '﷼', // Yemen Rial
ZWD: 'Z$', // Zimbabwean Dollar
};
if (currencies[code]) return currencies[code];
return `${code} `; // Symbol not found, return text code instead
};

View File

@ -0,0 +1,31 @@
/* Helper functions for Sections and Items */
import { hideFurnitureOn } from '@/utils/defaults';
/* Returns false if page furniture should be hidden on said route */
export const shouldBeVisible = (routeName) => !hideFurnitureOn.includes(routeName);
/* Based on section title, item name and index, return a string value for ID */
const makeItemId = (sectionStr, itemStr, index) => {
const charSum = sectionStr.split('').map((a) => a.charCodeAt(0)).reduce((x, y) => x + y);
const itemTitleStr = itemStr.replace(/\s+/g, '-').replace(/[^a-zA-Z ]/g, '').toLowerCase();
return `${index}_${charSum}_${itemTitleStr}`;
};
/* Given an array of sections, apply a unique ID to each item, and return modified array */
export const applyItemId = (inputSections) => {
const sections = inputSections || [];
sections.forEach((sec, secIdx) => {
if (sec.items) {
sec.items.forEach((item, itemIdx) => {
sections[secIdx].items[itemIdx].id = makeItemId(sec.name, item.title, itemIdx);
});
}
if (sec.widgets) {
sec.widgets.forEach((widget, widgetIdx) => {
sections[secIdx].widgets[widgetIdx].id = makeItemId(sec.name, widget.type, widgetIdx);
});
}
});
return sections;
};