Research
Security News
Malicious PyPI Package ‘pycord-self’ Targets Discord Developers with Token Theft and Backdoor Exploit
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
@phensley/messageformat
Advanced tools
Compact and extensible ICU message formatter with built-in support for plural
, select
, and selectordinal
.
import { pluralRules } from '@phensley/plurals';
import {
buildMessageMatcher,
parseMessagePattern,
DefaultMessageArgConverter,
MessageArg,
MessageEngine,
MessageFormatter,
MessageNamedArgs
} from '@phensley/messageformat';
const FORMATTERS = {
foo: (args: MessageArg[], options: string[]) =>
options[0] === 'upper' ? args[0].toUpperCase() : args[0].toLowerCase()
};
const FORMATTER_NAMES = Object.keys(FORMATTERS);
const MATCHER = buildMessageMatcher(FORMATTER_NAMES);
const CONVERTER = new DefaultMessageArgConverter();
const parse = (message: string) => parseMessagePattern(message, MATCHER);
const dump = (message: string) => console.log(JSON.stringify(parse(message)));
const plurals = (language: string, region?: string) =>
pluralRules.get(language, region);
const format = (
message: string,
positional: MessageArg[],
named: MessageNamedArgs = {}
) => {
const engine = new MessageEngine(
plurals('en'),
CONVERTER,
FORMATTERS,
parse(message)
);
console.log(engine.evaluate(positional, named));
};
let msg: string;
Messages can be pre-parsed and embedded into source code, JSON, or YAML files, or parsed and cached at runtime.
dump('{0 select, male {his} female {her} other {their}} {item}');
[4,[[3,[0],[["male",[0,"his"]],["female",[0,"her"]],["other",[0,"their"]]]],[0," "],[1,"item"]]]
dump('{word} uppercase = {word foo upper} lowercase = {word foo lower}');
[4,[[1,"word"],[0," uppercase = "],[6,"foo",["word"],["upper"]],[0," lowercase = "],[6,"foo",["word"],["lower"]]]]
If you don't need to embed parsed messages into source code, the MessageFormatter
can parse and cache messages at runtime. Internally it uses a least-recently-used cache whose size can be configured.
const rules = plurals('en');
const formatter = new MessageFormatter({
plurals: rules,
formatters: FORMATTERS,
cacheSize: 100
});
msg = '{0 select, male {his} female {her} other {their}} {item}';
console.log(formatter.format(msg, ['female'], { item: 'parka' }));
her parka
msg =
'{count, plural, offset:1 =0 {Be the first to like this} =1 {You liked this} ' +
'one {You and someone else liked this} other {You and # others liked this}}';
format(msg, [], { count: 0 });
format(msg, [], { count: 1 });
format(msg, [], { count: 2 });
format(msg, [], { count: 3 });
Be the first to like this
You liked this
You and someone else liked this
You and 2 others liked this
msg = 'Get {0, select, male {his} female {her} other {their}} {item}';
format(msg, ['they'], { item: 'coat' });
format(msg, ['female'], { item: 'jacket' });
format(msg, ['male'], { item: 'parka' });
Get their coat
Get her jacket
Get his parka
msg =
'{name} {tied select true {tied for} other {came in}} {place selectordinal one {#st} ' +
'two {#nd} few {#rd} other {#th}} place';
const racers = [
{ name: 'Lisa', place: 1 },
{ name: 'Bob', place: 2 },
{ name: 'Betty', place: 3 },
{ name: 'Frank', place: 4, tied: true },
{ name: 'George', place: 4, tied: true },
{ name: 'Larry', place: 5 }
];
for (const racer of racers) {
format(msg, [], racer);
}
Lisa came in 1st place
Bob came in 2nd place
Betty came in 3rd place
Frank tied for 4th place
George tied for 4th place
Larry came in 5th place
msg = '{word} uppercase = {word foo upper} lowercase = {word foo lower}';
const WORDS = ['Computer', 'Science', 'Mathematics'];
for (const word of WORDS) {
format(msg, [], { word });
}
Computer uppercase = COMPUTER lowercase = computer
Science uppercase = SCIENCE lowercase = science
Mathematics uppercase = MATHEMATICS lowercase = mathematics
FAQs
Extensible ICU message formatter
The npm package @phensley/messageformat receives a total of 868 weekly downloads. As such, @phensley/messageformat popularity was classified as not popular.
We found that @phensley/messageformat demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.
Security News
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.