@ribajs/shopify
Advanced tools
Comparing version 1.1.5 to 1.2.0
{ | ||
"name": "@ribajs/shopify", | ||
"description": "Shopify extension for Riba.js", | ||
"version": "1.1.5", | ||
"version": "1.2.0", | ||
"author": "Pascal Garber <pascal@jumplink.eu>", | ||
@@ -28,8 +28,8 @@ "contributors": [], | ||
"devDependencies": { | ||
"@babel/cli": "^7.6.0", | ||
"@babel/core": "^7.6.0", | ||
"@babel/cli": "^7.6.2", | ||
"@babel/core": "^7.6.2", | ||
"@babel/plugin-proposal-class-properties": "^7.5.5", | ||
"@babel/plugin-proposal-object-rest-spread": "^7.5.5", | ||
"@babel/plugin-transform-runtime": "^7.6.0", | ||
"@babel/preset-env": "^7.6.0", | ||
"@babel/plugin-proposal-object-rest-spread": "^7.6.2", | ||
"@babel/plugin-transform-runtime": "^7.6.2", | ||
"@babel/preset-env": "^7.6.2", | ||
"@babel/preset-typescript": "^7.6.0", | ||
@@ -41,9 +41,9 @@ "@types/jquery": "^3.3.31", | ||
"typescript": "^3.6.3", | ||
"webpack": "^4.40.2", | ||
"webpack": "^4.41.0", | ||
"webpack-cli": "^3.3.9" | ||
}, | ||
"dependencies": { | ||
"@ribajs/core": "^1.1.5", | ||
"@ribajs/i18n": "^1.1.5" | ||
"@ribajs/core": "^1.2.0", | ||
"@ribajs/i18n": "^1.2.0" | ||
} | ||
} |
@@ -1,5 +0,3 @@ | ||
/* tslint:disable:variable-name */ | ||
import { IFormatter } from '@ribajs/core'; | ||
import { ShopifyService } from '../services/shopify.service'; | ||
/** | ||
@@ -15,2 +13,23 @@ * Return a resized shopify image URL | ||
*/ | ||
export const img_url = ShopifyService.resizeImage; | ||
export const imgUrlFormatter: IFormatter = { | ||
name: 'img_url', | ||
read(url: string, size: string, scale: number, crop: string, extension: string) { | ||
try { | ||
if ('original' === size || 'master' === size) { | ||
return url; | ||
} | ||
const result = url.match(/(.*\/[\w\-\_\.]+)\.(\w{2,4})/); | ||
if (!result || !result[1] || !result[2]) { | ||
throw new Error(`Can't match url ${url}`); | ||
} | ||
const path = result[1]; | ||
extension = extension || result[2]; | ||
return path + '_' + size + '.' + extension; | ||
} catch (error) { | ||
console.error(error); | ||
return url; | ||
} | ||
}, | ||
}; |
@@ -1,7 +0,5 @@ | ||
/* tslint:disable:variable-name */ | ||
import { IFormatter } from '@ribajs/core'; | ||
import { moneyFormatter } from './money.formatter'; | ||
import { ShopifyService } from '../services/shopify.service'; | ||
const shopifyService = new ShopifyService(); | ||
/** | ||
@@ -11,2 +9,12 @@ * Formats the price based on the shop's HTML with currency setting (if the format is not overwritten by passing a format parameter). | ||
*/ | ||
export const money_with_currency = shopifyService.formatMoneyWithCurrency; | ||
export const moneyWithCurrencyFormatter: IFormatter = { | ||
name: 'money_with_currency', | ||
read(cents: string | number, format?: string) { | ||
const formatString = format || ShopifyService.moneyWithCurrencyFormat; | ||
if (!moneyFormatter.read) { | ||
console.error(new Error('Can\'t find moneyFormatter\'s read method!')); | ||
return cents; | ||
} | ||
return moneyFormatter.read(cents, formatString); | ||
}, | ||
}; |
@@ -0,9 +1,56 @@ | ||
import { IFormatter } from '@ribajs/core'; | ||
import { ShopifyService } from '../services/shopify.service'; | ||
const shopifyService = new ShopifyService(); | ||
/** | ||
* Formats the price based on the shop's HTML without currency setting (if the format is not overwritten by passing a format parameter). | ||
* Formats the price based on the shop's HTML with currency setting (if the format is not overwritten by passing a format parameter). | ||
* @param cents | ||
* @param format | ||
* | ||
* @see https://github.com/NathanPJF/deploybot-shopify/blob/master/assets/ajaxify.js.liquid | ||
* @see https://github.com/discolabs/cartjs/blob/master/src/utils.coffee | ||
* @see https://github.com/JumpLinkNetwork/shopify-productjs/blob/master/src/utilities.js | ||
* @see https://help.shopify.com/en/themes/liquid/filters/money-filters | ||
*/ | ||
export const money = shopifyService.formatMoney; | ||
export const moneyFormatter: IFormatter = { | ||
name: 'money', | ||
read(cents: string | number, format?: string) { | ||
let value = ''; | ||
const placeholderRegex = /\{\{\s*(\w+)\s*\}\}/; | ||
const formatString = format || this.moneyFormat; | ||
if (!formatString) { | ||
console.warn(`Can't parse format: ${formatString}`); | ||
return '0'; | ||
} | ||
if (typeof cents === 'string') { | ||
cents = cents.replace('.', ''); | ||
} | ||
// cents to float number | ||
cents = parseFloat(cents.toString()); | ||
const matchedFormat = formatString.match(placeholderRegex); | ||
if (matchedFormat !== null && matchedFormat.length >= 1) { | ||
switch (matchedFormat[1]) { | ||
case 'amount': | ||
value = ShopifyService.formatMoneyWithDelimiters(cents, 2); | ||
break; | ||
case 'amount_no_decimals': | ||
value = ShopifyService.formatMoneyWithDelimiters(cents, 0); | ||
break; | ||
case 'amount_with_comma_separator': | ||
value = ShopifyService.formatMoneyWithDelimiters(cents, 2, '.', ','); | ||
break; | ||
case 'amount_no_decimals_with_comma_separator': | ||
value = ShopifyService.formatMoneyWithDelimiters(cents, 0, '.', ','); | ||
break; | ||
} | ||
return formatString.replace(placeholderRegex, value); | ||
} | ||
console.warn(`Can't parse format: ${formatString}`); | ||
return '0'; | ||
}, | ||
}; |
import { IRibaModule } from '@ribajs/core'; | ||
export * from './components'; | ||
export * from './formatters'; | ||
export * from './services'; | ||
@@ -6,0 +4,0 @@ export * from './interfaces'; |
@@ -33,2 +33,20 @@ import { Debug, Utils } from '@ribajs/core'; | ||
/** | ||
* Check if the option values fits to the current variant. | ||
* @param variant | ||
* @param optionValues | ||
* @return Returns true if the option values fitting to the variant | ||
*/ | ||
public static fitsVariantOptions(variant: IShopifyProductVariant, optionValues: string[]) { | ||
let fit = true; | ||
// position0 is the option index starting on 0 | ||
for (const position0 in optionValues) { | ||
if (optionValues[position0]) { | ||
const optionValue = optionValues[position0]; | ||
fit = fit && variant.options.indexOf(optionValue.toString()) > -1; | ||
} | ||
} | ||
return fit; | ||
} | ||
/** | ||
* Get product variant of (selected) option values | ||
@@ -44,11 +62,4 @@ * @param optionValues (selected) option values | ||
const variant = product.variants[i]; | ||
let fit = false; | ||
// position0 is the option index starting on 0 | ||
for (const position0 in optionValues) { | ||
if (optionValues[position0]) { | ||
const optionValue = optionValues[position0]; | ||
fit = variant.options.indexOf(optionValue.toString()) > -1; | ||
} | ||
} | ||
if (fit) { | ||
const fits = this.fitsVariantOptions(variant, optionValues); | ||
if (fits) { | ||
result = variant; | ||
@@ -55,0 +66,0 @@ break; |
@@ -14,40 +14,34 @@ import { Debug, Utils } from '@ribajs/core'; | ||
/** | ||
* Custom version of Shopify.resizeImage | ||
* @param url | ||
* @param size | ||
* @param scale TODO | ||
* @param crop TODO | ||
* @param extension | ||
* | ||
* @see https://help.shopify.com/en/themes/liquid/filters/url-filters#img_url | ||
*/ | ||
public static resizeImage(url: string, size: string, scale: number, crop: string, extension: string) { | ||
try { | ||
if ('original' === size) { | ||
return url; | ||
} | ||
const result = url.match(/(.*\/[\w\-\_\.]+)\.(\w{2,4})/); | ||
public static formatMoneyWithDelimiters(num: number, precision = 2, thousands = ',', decimal = '.'): string { | ||
if (!result || !result[1] || !result[2]) { | ||
throw new Error(`Can't match url ${url}`); | ||
} | ||
if (!Utils.isNumber(num) || num === null) { | ||
return '0'; | ||
} | ||
const path = result[1]; | ||
extension = extension || result[2]; | ||
return path + '_' + size + '.' + extension; | ||
} catch (error) { | ||
console.error(error); | ||
return url; | ||
} | ||
const numStr: string = (num / 100.0).toFixed(precision); | ||
const parts = numStr.split('.'); | ||
const dollars = parts[0].replace(/(\d)(?=(\d\d\d)+(?!\d))/g, '$1' + thousands); | ||
const cents = parts[1] ? (decimal + parts[1]) : ''; | ||
return dollars + cents; | ||
} | ||
/** singleton instance */ | ||
private static instance: ShopifyService; | ||
// public cart = new ShopifyCartService(); | ||
protected static instance: ShopifyService; | ||
protected moneyFormat?: string; | ||
protected moneyWithCurrencyFormat?: string; | ||
public static get moneyWithCurrencyFormat() { | ||
if ((window as any).model && (window as any).model.system && (window as any).model.system.shopSettings) { | ||
return (window as any).model.system.shopSettings.moneyWithCurrencyFormat; | ||
} | ||
} | ||
public static get moneyFormat() { | ||
if ((window as any).model && (window as any).model.system && (window as any).model.system.shopSettings) { | ||
return (window as any).model.system.shopSettings.moneyFormat; | ||
} | ||
} | ||
private debug = Debug('service:ShopifyService'); | ||
@@ -57,7 +51,2 @@ | ||
if ((window as any).model && (window as any).model.system && (window as any).model.system.shopSettings) { | ||
this.moneyFormat = (window as any).model.system.shopSettings.moneyFormat; | ||
this.moneyWithCurrencyFormat = (window as any).model.system.shopSettings.moneyWithCurrencyFormat; | ||
} | ||
if (ShopifyService.instance) { | ||
@@ -72,72 +61,2 @@ return ShopifyService.instance; | ||
public formatMoneyWithCurrency(cents: string | number, format?: string) { | ||
const formatString = format || this.moneyWithCurrencyFormat; | ||
this.formatMoney(cents, formatString); | ||
} | ||
/** | ||
* Custom version of Shopify.formatMoney | ||
* @param cents | ||
* @param format | ||
* | ||
* @see https://github.com/NathanPJF/deploybot-shopify/blob/master/assets/ajaxify.js.liquid | ||
* @see https://github.com/discolabs/cartjs/blob/master/src/utils.coffee | ||
* @see https://github.com/JumpLinkNetwork/shopify-productjs/blob/master/src/utilities.js | ||
*/ | ||
public formatMoney(cents: string | number, format?: string) { | ||
let value = ''; | ||
const placeholderRegex = /\{\{\s*(\w+)\s*\}\}/; | ||
const formatString = format || this.moneyFormat; | ||
if (!formatString) { | ||
console.warn(`Can't parse format: ${formatString}`); | ||
return '0'; | ||
} | ||
if (typeof cents === 'string') { | ||
cents = cents.replace('.', ''); | ||
} | ||
// cents to float number | ||
cents = parseFloat(cents.toString()); | ||
function formatWithDelimiters(num: number, precision = 2, thousands = ',', decimal = '.'): string { | ||
if (!Utils.isNumber(num) || num === null) { | ||
return '0'; | ||
} | ||
const numStr: string = (num / 100.0).toFixed(precision); | ||
const parts = numStr.split('.'); | ||
const dollars = parts[0].replace(/(\d)(?=(\d\d\d)+(?!\d))/g, '$1' + thousands); | ||
cents = parts[1] ? (decimal + parts[1]) : ''; | ||
return dollars + cents; | ||
} | ||
const matchedFormat = formatString.match(placeholderRegex); | ||
if (matchedFormat !== null && matchedFormat.length >= 1) { | ||
switch (matchedFormat[1]) { | ||
case 'amount': | ||
value = formatWithDelimiters(cents, 2); | ||
break; | ||
case 'amount_no_decimals': | ||
value = formatWithDelimiters(cents, 0); | ||
break; | ||
case 'amount_with_comma_separator': | ||
value = formatWithDelimiters(cents, 2, '.', ','); | ||
break; | ||
case 'amount_no_decimals_with_comma_separator': | ||
value = formatWithDelimiters(cents, 0, '.', ','); | ||
break; | ||
} | ||
return formatString.replace(placeholderRegex, value); | ||
} | ||
console.warn(`Can't parse format: ${formatString}`); | ||
return '0'; | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
46754
1286
Updated@ribajs/core@^1.2.0
Updated@ribajs/i18n@^1.2.0