Socket
Socket
Sign inDemoInstall

@lion/localize

Package Overview
Dependencies
Maintainers
1
Versions
102
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@lion/localize - npm Package Compare versions

Comparing version 0.14.9 to 0.15.0

6

CHANGELOG.md
# Change Log
## 0.15.0
### Minor Changes
- 3ada1aef: Localize set date and number postProcessors for locale
## 0.14.9

@@ -4,0 +10,0 @@

2

package.json
{
"name": "@lion/localize",
"version": "0.14.9",
"version": "0.15.0",
"description": "The localization system helps to manage localization data split into locales and automate its loading",

@@ -5,0 +5,0 @@ "license": "MIT",

@@ -0,1 +1,2 @@

/** @typedef {import('../../types/LocalizeMixinTypes').DatePostProcessor} DatePostProcessor */
/**

@@ -9,1 +10,2 @@ * Formats date based on locale and options

export function formatDate(date: Date, options?: import("../../types/LocalizeMixinTypes.js").FormatDateOptions | undefined): string;
export type DatePostProcessor = typeof import("../../types/LocalizeMixinTypes.js").DatePostProcessorImplementation;

@@ -0,4 +1,7 @@

import { localize } from '../localize.js';
import { getLocale } from './getLocale.js';
import { normalizeIntlDate } from './normalizeIntlDate.js';
/** @typedef {import('../../types/LocalizeMixinTypes').DatePostProcessor} DatePostProcessor */
/**

@@ -36,3 +39,20 @@ * Formats date based on locale and options

}
if (localize.formatDateOptions.postProcessors.size > 0) {
Array.from(localize.formatDateOptions.postProcessors).forEach(([locale, fn]) => {
if (locale === computedLocale) {
formattedDate = fn(formattedDate);
}
});
}
if (formatOptions.postProcessors && formatOptions.postProcessors.size > 0) {
Array.from(formatOptions.postProcessors).forEach(([locale, fn]) => {
if (locale === computedLocale) {
formattedDate = fn(formattedDate);
}
});
}
return normalizeIntlDate(formattedDate, computedLocale, formatOptions);
}
/**
* @typedef {import('../types/LocalizeMixinTypes').NamespaceObject} NamespaceObject
*/
/** @typedef {import('../types/LocalizeMixinTypes').DatePostProcessor} DatePostProcessor */
/** @typedef {import('../types/LocalizeMixinTypes').NumberPostProcessor} NumberPostProcessor */
/**

@@ -35,3 +37,9 @@ * `LocalizeManager` manages your translations (includes loading)

returnIfNaN: string;
/** @type {Map<string,DatePostProcessor>} */
postProcessors: Map<string, DatePostProcessor>;
};
formatDateOptions: {
/** @type {Map<string,DatePostProcessor>} */
postProcessors: Map<string, DatePostProcessor>;
};
_supportExternalTranslationTools: boolean;

@@ -225,3 +233,19 @@ /**

_getMessageForKey(key: string | undefined, locale: string): string;
/**
* @param {{locale:string, postProcessor:DatePostProcessor}} options
*/
setDatePostProcessorForLocale({ locale, postProcessor }: {
locale: string;
postProcessor: DatePostProcessor;
}): void;
/**
* @param {{locale:string, postProcessor:NumberPostProcessor}} options
*/
setNumberPostProcessorForLocale({ locale, postProcessor }: {
locale: string;
postProcessor: NumberPostProcessor;
}): void;
}
export type NamespaceObject = string | import("../types/LocalizeMixinTypes.js").StringToFunctionMap;
export type DatePostProcessor = typeof import("../types/LocalizeMixinTypes.js").DatePostProcessorImplementation;
export type NumberPostProcessor = typeof import("../types/LocalizeMixinTypes.js").NumberPostProcessorImplementation;

@@ -9,2 +9,5 @@ // @ts-expect-error no types for this package

/** @typedef {import('../types/LocalizeMixinTypes').DatePostProcessor} DatePostProcessor */
/** @typedef {import('../types/LocalizeMixinTypes').NumberPostProcessor} NumberPostProcessor */
/**

@@ -34,4 +37,11 @@ * `LocalizeManager` manages your translations (includes loading)

returnIfNaN: '',
/** @type {Map<string,DatePostProcessor>} */
postProcessors: new Map(),
};
this.formatDateOptions = {
/** @type {Map<string,DatePostProcessor>} */
postProcessors: new Map(),
};
/**

@@ -528,2 +538,16 @@ * Via html[data-localize-lang], developers are allowed to set the initial locale, without

}
/**
* @param {{locale:string, postProcessor:DatePostProcessor}} options
*/
setDatePostProcessorForLocale({ locale, postProcessor }) {
this.formatDateOptions.postProcessors.set(locale, postProcessor);
}
/**
* @param {{locale:string, postProcessor:NumberPostProcessor}} options
*/
setNumberPostProcessorForLocale({ locale, postProcessor }) {
this.formatNumberOptions.postProcessors.set(locale, postProcessor);
}
}

@@ -0,1 +1,2 @@

/** @typedef {import('../../types/LocalizeMixinTypes').NumberPostProcessor} NumberPostProcessor */
/**

@@ -11,2 +12,3 @@ * Formats a number based on locale and options. It uses Intl for the formatting.

export function formatNumber(number: number, options?: import("../../types/LocalizeMixinTypes.js").FormatNumberOptions | undefined): string;
export type NumberPostProcessor = typeof import("../../types/LocalizeMixinTypes.js").NumberPostProcessorImplementation;
/**

@@ -13,0 +15,0 @@ * Formats a number based on locale and options. It uses Intl for the formatting.

import { localize } from '../localize.js';
import { getLocale } from './getLocale.js';
import { formatNumberToParts } from './formatNumberToParts.js';
/** @typedef {import('../../types/LocalizeMixinTypes').NumberPostProcessor} NumberPostProcessor */
/**

@@ -30,3 +33,22 @@ * Formats a number based on locale and options. It uses Intl for the formatting.

}
const computedLocale = getLocale(options && options.locale);
if (localize.formatNumberOptions.postProcessors.size > 0) {
Array.from(localize.formatNumberOptions.postProcessors).forEach(([locale, fn]) => {
if (locale === computedLocale) {
printNumberOfParts = fn(printNumberOfParts);
}
});
}
if (options.postProcessors && options.postProcessors.size > 0) {
Array.from(options.postProcessors).forEach(([locale, fn]) => {
if (locale === computedLocale) {
printNumberOfParts = fn(printNumberOfParts);
}
});
}
return printNumberOfParts;
}

@@ -235,2 +235,102 @@ import { expect } from '@open-wc/testing';

});
describe('Date post processors', () => {
/**
* Uppercase processor
*
* @param {string} str
* @returns {string}
*/
const upperCaseProcessor = str => {
return str.toUpperCase();
};
/**
* Lowercase processor
*
* @param {string} str
* @returns {string}
*/
const lowerCaseProcessor = str => {
return str.toLocaleLowerCase();
};
it('displays the appropriate date after post processor set in options', async () => {
const testDate = new Date('2012/05/21');
const postProcessors = new Map();
postProcessors.set('nl-NL', upperCaseProcessor);
postProcessors.set('de-DE', lowerCaseProcessor);
const options = {
weekday: 'long',
year: 'numeric',
month: 'long',
day: '2-digit',
postProcessors,
};
// locale is en-GB
expect(formatDate(testDate, options)).to.equal('Monday, 21 May 2012');
localize.locale = 'nl-NL';
expect(formatDate(testDate, options)).to.equal('MAANDAG 21 MEI 2012');
localize.locale = 'de-DE';
expect(formatDate(testDate, options)).to.equal('montag, 21. mai 2012');
localize.locale = 'en-US';
expect(formatDate(testDate, options)).to.equal('Monday, May 21, 2012');
});
it('displays the appropriate date after post processor set in localize', async () => {
const testDate = new Date('2012/05/21');
const options = {
weekday: 'long',
year: 'numeric',
month: 'long',
day: '2-digit',
};
localize.setDatePostProcessorForLocale({
locale: 'nl-NL',
postProcessor: upperCaseProcessor,
});
localize.setDatePostProcessorForLocale({
locale: 'de-DE',
postProcessor: upperCaseProcessor,
});
expect(formatDate(testDate, options)).to.equal('Monday, 21 May 2012');
localize.locale = 'nl-NL';
expect(formatDate(testDate, options)).to.equal('MAANDAG 21 MEI 2012');
localize.locale = 'de-DE';
expect(formatDate(testDate, options)).to.equal('MONTAG, 21. MAI 2012');
localize.locale = 'en-US';
expect(formatDate(testDate, options)).to.equal('Monday, May 21, 2012');
});
it('displays the appropriate date after post processors set in options and localize', async () => {
const testDate = new Date('2012/05/21');
const postProcessors = new Map();
postProcessors.set('nl-NL', upperCaseProcessor);
postProcessors.set('de-DE', upperCaseProcessor);
const options = {
weekday: 'long',
year: 'numeric',
month: 'long',
day: '2-digit',
postProcessors,
};
localize.setDatePostProcessorForLocale({
locale: 'de-DE',
postProcessor: lowerCaseProcessor,
});
expect(formatDate(testDate, options)).to.equal('Monday, 21 May 2012');
localize.locale = 'nl-NL';
expect(formatDate(testDate, options)).to.equal('MAANDAG 21 MEI 2012');
localize.locale = 'de-DE';
expect(formatDate(testDate, options)).to.equal('MONTAG, 21. MAI 2012');
localize.locale = 'en-US';
expect(formatDate(testDate, options)).to.equal('Monday, May 21, 2012');
});
});
});

@@ -365,2 +365,67 @@ import { expect } from '@open-wc/testing';

});
describe('postProcessors', () => {
/** @type {Map<string, import('../../types/LocalizeMixinTypes.js').NumberPostProcessor>} */
let savedpostProcessors;
beforeEach(() => {
savedpostProcessors = localize.formatNumberOptions.postProcessors;
});
afterEach(() => {
localize.formatNumberOptions.postProcessors = savedpostProcessors;
});
/**
* Comma to spaces processor
*
* @param {string} str
* @returns {string}
*/
const commaToSpaceProcessor = str => {
return str.replace(/,/g, ' ');
};
/**
* First space to dot processor
*
* @param {string} str
* @returns {string}
*/
const firstSpaceToDotProcessor = str => {
return str.replace(' ', '.');
};
it('uses `options.postProcessors`', () => {
const postProcessors = new Map();
postProcessors.set('en-GB', commaToSpaceProcessor);
expect(
formatNumber(112345678, {
postProcessors,
}),
).to.equal('112 345 678');
});
it('uses `localize.formatNumberOptions.postProcessors`', () => {
localize.setNumberPostProcessorForLocale({
locale: 'en-GB',
postProcessor: commaToSpaceProcessor,
});
expect(formatNumber(112345678)).to.equal('112 345 678');
});
it('uses `options.postProcessors` and `localize.formatNumberOptions.postProcessors`', () => {
const postProcessors = new Map();
postProcessors.set('en-GB', commaToSpaceProcessor);
localize.setNumberPostProcessorForLocale({
locale: 'en-GB',
postProcessor: firstSpaceToDotProcessor,
});
expect(
formatNumber(112345678, {
postProcessors,
}),
).to.equal('112 345 678');
});
});
});

@@ -9,2 +9,6 @@ import { Constructor } from '@open-wc/dedupe-mixin';

declare function DatePostProcessorImplementation(date: string): string;
export type DatePostProcessor = typeof DatePostProcessorImplementation;
// Take the DateTimeFormat and add the missing resolved options as well as optionals

@@ -21,4 +25,10 @@ export declare interface FormatDateOptions extends Intl.DateTimeFormatOptions {

mode?: 'pasted' | 'auto';
postProcessors?: Map<string, DatePostProcessor>;
}
declare function NumberPostProcessorImplementation(number: string): string;
export type NumberPostProcessor = typeof NumberPostProcessorImplementation;
// Take the DateTimeFormat and add the missing resolved options as well as optionals, and our own

@@ -32,2 +42,4 @@ export declare interface FormatNumberOptions extends Intl.NumberFormatOptions {

mode?: 'pasted' | 'auto';
postProcessors?: Map<string, NumberPostProcessor>;
}

@@ -34,0 +46,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc