Skip to content

Commit

Permalink
Remove lodash, add capitalizefirst, misc
Browse files Browse the repository at this point in the history
  • Loading branch information
phensley committed Nov 28, 2024
1 parent dba7fa1 commit d2a1d12
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 16 deletions.
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
},
"dependencies": {
"@phensley/timezone": "~1.2.17",
"lodash": "^4.17.21",
"utf8": "^3.0.0"
},
"peerDependencies": {
Expand All @@ -48,7 +47,6 @@
"@rollup/plugin-json": "^4.0.3",
"@rollup/plugin-node-resolve": "^7.1.3",
"@types/jest": "^29.5.10",
"@types/lodash": "^4.17.13",
"@types/node": "^20.10.0",
"@types/utf8": "^3.0.3",
"beautify-benchmark": "^0.2.4",
Expand Down
5 changes: 2 additions & 3 deletions src/plugins/formatters.commerce.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import capitalize from 'lodash/capitalize';
import { Context } from '../context';
import { ProductType } from './enums';
import { Node } from '../node';
Expand Down Expand Up @@ -245,12 +244,12 @@ export class ProductPriceFormatter extends Formatter {
// This string needs to match the correct translation template in v6 products-2.0-en-US.json.
let localizedStringKey = 'productPrice__' +
`${hasMultiplePrices ? 'multiplePrices' : 'singlePrice'}__` +
`${billingPeriodValue === 1 ? '1' : 'n'}${capitalize(billingPeriodUnit)}ly__`;
`${billingPeriodValue === 1 ? '1' : 'n'}${stringutil.capitalizeFirst(billingPeriodUnit)}ly__`;

if (durationValue == 0) {
localizedStringKey += 'indefinite';
} else {
localizedStringKey += `limited__${durationValue === 1 ? '1' : 'n'}${capitalize(durationUnit)}s`;
localizedStringKey += `limited__${durationValue === 1 ? '1' : 'n'}${stringutil.capitalizeFirst(durationUnit)}s`;
}

const localizedStringNode = ctx.resolve(['localizedStrings', localizedStringKey]);
Expand Down
16 changes: 5 additions & 11 deletions src/plugins/util.commerce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ export const getAmountFromMoneyNode = (moneyNode?: Node) => {

export const getCurrencyFromMoneyNode = (moneyNode: Node): CurrencyType => {
const currencyNode = moneyNode.path(['currency']);
const currency = !currencyNode.isMissing() ?
currencyNode.asString().trim() :
DEFAULT_MONEY_NODE.path(['currency']).asString();
const currency = !currencyNode.isMissing() ? currencyNode.asString().trim() : DEFAULT_MONEY_NODE.path(['currency']).asString();

return currency as CurrencyType;
};
Expand All @@ -60,7 +58,7 @@ export const getMoneyString = (moneyNode: Node, args: string[], ctx: Context): s
if (useCLDRMode(ctx)) {
const amount = getAmountFromMoneyNode(moneyNode);
const currencyCode = getCurrencyFromMoneyNode(moneyNode);

return ctx.cldr?.Numbers.formatCurrency(amount, currencyCode, currencyOptions(args)) ?? '';
} else {
const legacyAmount = getLegacyPriceFromMoneyNode(moneyNode);
Expand All @@ -78,15 +76,13 @@ export const getSubscriptionMoneyFromFirstPricingOptions = (pricingOptions: Node

const node = pricingOptions.get(0);

return isTruthy(node.path(['onSale']))
? node.path(['salePriceMoney'])
: node.path(['priceMoney']);
return isTruthy(node.path(['onSale'])) ? node.path(['salePriceMoney']) : node.path(['priceMoney']);
};

export const getPricingOptionsAmongLowestVariant = (item: Node): Node | null => {
const productType = getProductType(item);
const structuredContent = item.path(['structuredContent']);

switch (productType) {
case ProductType.PHYSICAL:
case ProductType.SERVICE:
Expand All @@ -96,9 +92,7 @@ export const getPricingOptionsAmongLowestVariant = (item: Node): Node | null =>
}

const first = variants.get(0);
const moneyNode = isTruthy(first.path(['onSale']))
? first.path(['salePriceMoney'])
: first.path(['priceMoney']);
const moneyNode = isTruthy(first.path(['onSale'])) ? first.path(['salePriceMoney']) : first.path(['priceMoney']);

let pricingOptions = first.path(['pricingOptions']);
let price = getAmountFromMoneyNode(moneyNode);
Expand Down
5 changes: 5 additions & 0 deletions src/plugins/util.string.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { replaceMappedChars } from '../util';

/**
* Capitalize first letter of a string and lowercase the rest.
*/
export const capitalizeFirst = (str: string) => str.charAt(0).toUpperCase() + str.slice(1).toLowerCase();

/**
* Strip text between '<' and '>' from string.
*/
Expand Down

0 comments on commit d2a1d12

Please sign in to comment.