wip: shopping cart, product options
This commit is contained in:
30
spa/src/helpers.js
Normal file
30
spa/src/helpers.js
Normal file
@@ -0,0 +1,30 @@
|
||||
export function isNotEmpty(value) {
|
||||
if (value === null || value === undefined) return false;
|
||||
|
||||
if (Array.isArray(value)) return value.length > 0;
|
||||
|
||||
if (typeof value === 'object') return Object.keys(value).length > 0;
|
||||
|
||||
if (typeof value === 'string') return value.trim() !== '';
|
||||
|
||||
return true; // для чисел, булевых и т.п.
|
||||
}
|
||||
|
||||
export function formatPrice(raw) {
|
||||
if (raw === null || raw === undefined) return '';
|
||||
|
||||
const str = String(raw).trim();
|
||||
const match = str.match(/^([+-]?)(\d+(?:\.\d+)?)/);
|
||||
if (!match) return '';
|
||||
|
||||
const sign = match[1] || '';
|
||||
const num = parseFloat(match[2]);
|
||||
|
||||
if (isNaN(num) || num === 0) return '';
|
||||
|
||||
const formatted = Math.round(num)
|
||||
.toString()
|
||||
.replace(/\B(?=(\d{3})+(?!\d))/g, ' ');
|
||||
|
||||
return `${sign}${formatted}`;
|
||||
}
|
||||
Reference in New Issue
Block a user