1
0
mirror of https://github.com/chylex/Firefox-SCsCC.git synced 2025-04-09 16:15:43 +02:00

price check enhacements

This commit is contained in:
sarics 2017-08-26 13:15:12 +02:00
parent 21d4e388b9
commit 4e2b3d5102
3 changed files with 25 additions and 63 deletions

View File

@ -1,3 +1,5 @@
import { nonWordPatt } from './patts';
const getCssPseudoContent = (node, place) => {
const style = window.getComputedStyle(node, `:${place}`);
if (style && style.content !== 'none') {
@ -57,12 +59,17 @@ const checkSiblingMatches = (textNode, toCurr, { numPatt, symbPatts }) => {
chckTxt.after = getCssPseudoContent(textNode.parentNode, 'after');
Object.keys(chckTxt).forEach((pos) => {
if (!chckTxt[pos].length) return;
if (!chckTxt[pos]) return;
Object.keys(symbPatts).forEach((fromCurr) => {
if (fromCurr === toCurr) return;
const symbPatt = new RegExp(`^${symbPatts[fromCurr]}$`, 'gi');
let symbPattStr = symbPatts[fromCurr];
if (pos === 'prev') symbPattStr = `${nonWordPatt}${symbPattStr}$`;
else if (pos === 'next') symbPattStr = `^${symbPattStr}${nonWordPatt}`;
else symbPattStr = `^${symbPattStr}$`;
const symbPatt = new RegExp(symbPattStr, 'i');
if (symbPatt.test(chckTxt[pos])) {
matches[fromCurr] = match;

View File

@ -16,4 +16,6 @@ export const currPatts = Object.keys(symbPatts)
const allSymbPatt = Object.keys(symbPatts).reduce((patt, curr) => `${patt}|${symbPatts[curr].replace(/^\((.*)\)$/, '$1')}`, '\\s|,--');
export const cleanSymbPatt = new RegExp(allSymbPatt, 'gi');
export const wordPatt = new RegExp(`[${UNICODE_ALPHABETIC}]`);
const alphabeticChars = UNICODE_ALPHABETIC;
export const wordPatt = `[${alphabeticChars}]`;
export const nonWordPatt = `[^${alphabeticChars}]`;

View File

@ -1,71 +1,24 @@
import { cleanSymbPatt, wordPatt } from './patts';
export const checkPriceSpecCases = (txt, match, fromCurr) => {
let chckchar;
const alphabeticPatt = new RegExp(wordPatt);
const alphaNumericPatt = new RegExp(wordPatt.replace(/^\[/, '[0-9'));
export const checkPriceSpecCases = (txt, match) => {
const charind = txt.indexOf(match);
let checkedMatch = match;
// skip other dollars
// Australian (A$)
// Barbadian (Bds$)
// Belizean (BZ$)
// Brunei (B$)
// Canadian (CA$)
// Cayman Islands (CI$)
// East Caribbean (EC$)
// Fiji (FJ$)
// Guyanese (G$)
// Hong Kong (HK$)
// Jamaican (J$)
// Liberian (L$ or LD$)
// Namibian (N$)
// New Zealand (NZ$)
// Singaporean (S$)
// Soloman Islands (SI$)
// Taiwanese (NT$)
// Trinidad and Tobago (TT$)
// Tuvaluan (TV$)
// Zimbabwean (Z$)
// Chilean (CLP$)
// Colombian (COL$)
// Dominican (RD$)
// Mexican (Mex$)
// Nicaraguan córdoba (C$)
// Brazilian real (R$)
if (fromCurr === 'USD' && match.charAt(0) === '$') {
chckchar = txt.charAt(charind - 1);
if (
/\w/.test(chckchar) &&
/(A|Bds|BZ|B|CA|CI|EC|FJ|G|HK|J|L|LD|N|NZ|S|SI|NT|TT|TV|Z|CLP|COL|RD|Mex|C|R)$/.test(txt.slice(0, charind))
) return null;
}
// in case text is like: masseur 1234
// or
// in case text is like: 1234 europe
const sind = match.search(/eur|usd|gbp/i);
if (sind !== -1) {
if (sind === 0) { // starts with eur(os)/usd/gbp
// if there is any word character before it, skip it
chckchar = txt.charAt(charind - 1);
if (wordPatt.test(chckchar)) return null;
} else { // ends with eur(os)/usd/gbp
// if there is any word character after it, skip it
chckchar = txt.charAt(charind + match.length);
if (wordPatt.test(chckchar)) return null;
}
}
const bChar = txt.charAt(charind - 1);
const aChar = txt.charAt(charind + match.length);
// in case text is like: somestring1 234 $
if (match.charAt(0).search(/\d/) !== -1) {
// if there is a word character before it
chckchar = txt.charAt(charind - 1);
if (wordPatt.test(chckchar)) {
checkedMatch = match.replace(/^\d+\s/, ''); // convert only 234 $
}
// if there is a word character before it
if (/^\d/.test(match) && alphabeticPatt.test(bChar)) {
if (/^\d+\s+\d/.test(match)) return match.replace(/^\d+\s+/, ''); // convert only 234 $
return null;
}
return checkedMatch;
if (alphaNumericPatt.test(bChar) || alphaNumericPatt.test(aChar)) return null;
return match;
};