Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

elemental-string-format

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

elemental-string-format - npm Package Compare versions

Comparing version 0.0.1 to 0.1.0

examples.js

6

lib/formatters/number.js

@@ -22,3 +22,3 @@ const number = function(i18nProvider, fixed) {

for (var i = prefixParts.length - 1; i >= 0; i--) {
if (i % 4 === 0) {
if (i % 3 === 0) {
newPrefix += this.i18nProvider.getLookup().thousandsSeparator;

@@ -29,2 +29,6 @@ }

if (newPrefix.indexOf(this.i18nProvider.getLookup().thousandsSeparator) === 0) {
newPrefix = newPrefix.slice(1);
}
return `${isNegative ? '-' : ''}${newPrefix.split('').reverse().join('')}${parts[1] ? this.i18nProvider.getLookup().decimalPlace : ''}${parts[1]}`;

@@ -31,0 +35,0 @@ };

@@ -5,3 +5,16 @@ module.exports = {

decimalPlace : ".",
thousandsSeparator : ","
thousandsSeparator : ",",
shortDate : "mm/d/yyyy",
longDate : "dddd, mmmm d, yyyy",
fullDateShortTime : "dddd, mmmm d, yyyy h:MM TT",
fullDateLongTime : "dddd, mmmm d, yyyy h:MM:ss TT",
yearMonth : "mmmm yyyy",
monthDay : "mmmm d",
shortTime : "h:MM TT",
longTime : "h:MM:ss TT",
universalFull : "dddd, mmmm d, yyyy h:MM:ss TT",
rfc1123 : "ddd, d mmm yyyy HH:MM:ss",
universalSortable : "yyyy-mm-dd HH:MM:ss'Z'",
generalDateTimeShort : "mm/d/yyyy h:MM TT",
generalDateTimeLong : "mm/d/yyyy h:MM:ss TT"
},

@@ -11,3 +24,9 @@ 'en_GB' : {

decimalPlace : ".",
thousandsSeparator : ","
thousandsSeparator : ",",
shortDate : "d/mm/yyyy",
longDate : "dddd, mmmm d, yyyy",
fullDateShortTime : "dddd, mmmm d, yyyy h:MM TT",
fullDateLongTime : "dddd, mmmm d, yyyy h:MM:ss TT",
generalDateTimeShort : "dd/mm/yyyy h:MM TT",
generalDateTimeLong : "dd/mm/yyyy h:MM TT"
},

@@ -17,3 +36,9 @@ 'nl_NL' : {

decimalPlace : ",",
thousandsSeparator : "."
thousandsSeparator : ".",
shortDate : "yyyy-mm-dd",
longDate : "dddd, mmmm d, yyyy",
fullDateShortTime : "dddd, mmmm d, yyyy h:MM TT",
fullDateLongTime : "dddd, mmmm d, yyyy h:MM:ss TT",
generalDateTimeShort : "dd/mm/yyyy h:MM TT",
generalDateTimeLong : "dd/mm/yyyy h:MM TT"
},

@@ -23,3 +48,9 @@ 'fr_FR' : {

decimalPlace : ",",
thousandsSeparator : "."
thousandsSeparator : ".",
shortDate : "d/mm/yyyy",
longDate : "dddd, mmmm d, yyyy",
fullDateShortTime : "dddd, mmmm d, yyyy h:MM TT",
fullDateLongTime : "dddd, mmmm d, yyyy h:MM:ss TT",
generalDateTimeShort : "dd/mm/yyyy h:MM TT",
generalDateTimeLong : "dd/mm/yyyy h:MM TT"
},

@@ -29,4 +60,12 @@ 'sv_SE' : {

decimalPlace : ",",
thousandsSeparator : "."
thousandsSeparator : ".",
shortDate : "d/mm/yyyy",
longDate : "dddd, mmmm d, yyyy",
fullDateShortTime : "'den' d mmmm yyyy HH:MM",
fullDateLongTime : "'den' d mmmm yyyy HH:MM:ss",
shortTime : "HH:MM",
longTime : "HH:MM:ss",
generalDateTimeShort : "dd/mm/yyyy HH:MM",
generalDateTimeLong : "dd/mm/yyyy HH:MM:ss"
}
};

4

lib/i18n/provider.js

@@ -22,3 +22,5 @@ const provider = function(lookups) {

return list;
list = JSON.parse(JSON.stringify(list));
return Object.assign(JSON.parse(JSON.stringify(this.lookups[this.defaultList])), list);
};

@@ -25,0 +27,0 @@

@@ -1,12 +0,53 @@

const injector = function(str, currency, decimal, fixed, exponential, genericNumber, number, percent, roundTrip, hexadecimal) {
const injector = function(
str,
currency,
decimal,
fixed,
exponential,
genericNumber,
number,
percent,
roundTrip,
hexadecimal,
shortDate,
longDate,
fullDateShortTime,
fullDateLongTime,
yearMonth,
universalFull,
universalSortable,
longTime,
shortTime,
sortableDate,
rfc1123,
dateRoundTrip,
monthDay,
generalDateTimeLong,
generalDateTimeShort
) {
//formatters
this.currency = currency;
this.decimal = decimal;
this.fixed = fixed;
this.exponential = exponential;
this.genericNumber = genericNumber;
this.number = number;
this.percent = percent;
this.roundTrip = roundTrip;
this.hexadecimal = hexadecimal;
this.currency = currency;
this.decimal = decimal;
this.fixed = fixed;
this.exponential = exponential;
this.genericNumber = genericNumber;
this.number = number;
this.percent = percent;
this.roundTrip = roundTrip;
this.hexadecimal = hexadecimal;
this.shortDate = shortDate;
this.longDate = longDate;
this.fullDateShortTime = fullDateShortTime;
this.fullDateLongTime = fullDateLongTime;
this.yearMonth = yearMonth;
this.universalFull = universalFull;
this.universalSortable = universalSortable;
this.longTime = longTime;
this.shortTime = shortTime;
this.sortableDate = sortableDate;
this.rfc1123 = rfc1123;
this.dateRoundTrip = dateRoundTrip;
this.monthDay = monthDay;
this.generalDateTimeLong = generalDateTimeLong;
this.generalDateTimeShort = generalDateTimeShort;

@@ -19,2 +60,3 @@ //props

this.alignment = null;
this.isNumber = false;

@@ -60,37 +102,101 @@ if (this.str.indexOf(':') !== -1) {

//do we have options?
if (lower === 'c') {
val = this.currency.format(val, this.options, this.optionArgs);
}
if (typeof(val) === "number") {
//do we have options?
if (lower === 'c') {
val = this.currency.format(val, this.options, this.optionArgs);
}
if (lower === 'd') {
val = this.decimal.format(val, this.options, this.optionArgs);
}
if (lower === 'd') {
val = this.decimal.format(val, this.options, this.optionArgs);
}
if (lower === 'f') {
val = this.fixed.format(val, this.options, this.optionArgs);
}
if (lower === 'f') {
val = this.fixed.format(val, this.options, this.optionArgs);
}
if (lower === 'e') {
val = this.exponential.format(val, this.options, this.optionArgs);
}
if (lower === 'e') {
val = this.exponential.format(val, this.options, this.optionArgs);
}
if (lower === 'g') {
val = this.genericNumber.format(val, this.options, this.optionArgs);
}
if (lower === 'g') {
val = this.genericNumber.format(val, this.options, this.optionArgs);
}
if (lower === 'n') {
val = this.number.format(val, this.options, this.optionArgs);
}
if (lower === 'n') {
val = this.number.format(val, this.options, this.optionArgs);
}
if (lower === 'p') {
val = this.percent.format(val, this.options, this.optionArgs);
}
if (lower === 'p') {
val = this.percent.format(val, this.options, this.optionArgs);
}
if (lower === 'r') {
val = this.roundTrip.format(val, this.options, this.optionArgs);
if (lower === 'r') {
val = this.roundTrip.format(val, this.options, this.optionArgs);
}
if (lower === 'x') {
val = this.hexadecimal.format(val, this.options, this.optionArgs);
}
}
if (lower === 'x') {
val = this.hexadecimal.format(val, this.options, this.optionArgs);
if (typeof(val) === "object") {
if (this.options === 'd') {
val = this.shortDate.format(val, this.options, this.optionArgs);
}
if (this.options === 'D') {
val = this.longDate.format(val, this.options, this.optionArgs);
}
if (this.options === 'f') {
val = this.fullDateShortTime.format(val, this.options, this.optionArgs);
}
if (this.options === 'F') {
val = this.fullDateLongTime.format(val, this.options, this.optionArgs);
}
if (this.options === 'g') {
val = this.generalDateTimeShort.format(val, this.options, this.optionArgs);
}
if (this.options === 'G') {
val = this.generalDateTimeLong.format(val, this.options, this.optionArgs);
}
if (lower === 'm') {
val = this.monthDay.format(val, this.options, this.optionArgs);
}
if (lower === 'o') {
val = this.dateRoundTrip.format(val, this.options, this.optionArgs);
}
if (lower === 'r') {
val = this.rfc1123.format(val, this.options, this.optionArgs);
}
if (this.options === 's') {
val = this.sortableDate.format(val, this.options, this.optionArgs);
}
if (this.options === 't') {
val = this.shortTime.format(val, this.options, this.optionArgs);
}
if (this.options === 'T') {
val = this.longTime.format(val, this.options, this.optionArgs);
}
if (this.options === 'u') {
val = this.universalSortable.format(val, this.options, this.optionArgs);
}
if (this.options === 'U') {
val = this.universalFull.format(val, this.options, this.optionArgs);
}
if (lower === 'y') {
val = this.yearMonth.format(val, this.options, this.optionArgs);
}
}

@@ -114,3 +220,28 @@

module.exports = function(currency, decimal, fixed, exponential, genericNumber, number, percent, roundTrip, hexadecimal) {
module.exports = function(
currency,
decimal,
fixed,
exponential,
genericNumber,
number,
percent,
roundTrip,
hexadecimal,
shortDate,
longDate,
fullDateShortTime,
fullDateLongTime,
yearMonth,
universalFull,
universalSortable,
longTime,
shortTime,
sortableDate,
rfc1123,
dateRoundTrip,
monthDay,
generalDateTimeLong,
generalDateTimeShort
) {
if (!currency) {

@@ -152,5 +283,91 @@ currency = require('./formatters/currency')();

if (!shortDate) {
shortDate = require('./formatters/shortDate')();
}
if (!longDate) {
longDate = require('./formatters/longDate')();
}
if (!fullDateShortTime) {
fullDateShortTime = require('./formatters/fullDateShortTime')();
}
if (!fullDateLongTime) {
fullDateLongTime = require('./formatters/fullDateLongTime')();
}
if (!yearMonth) {
yearMonth = require('./formatters/yearMonth')();
}
if (!universalFull) {
universalFull = require('./formatters/universalFull')();
}
if (!universalSortable) {
universalSortable = require('./formatters/universalSortable')();
}
if (!longTime) {
longTime = require('./formatters/longTime')();
}
if (!shortTime) {
shortTime = require('./formatters/shortTime')();
}
if (!sortableDate) {
sortableDate = require('./formatters/sortableDate')();
}
if (!rfc1123) {
rfc1123 = require('./formatters/rfc1123')();
}
if (!dateRoundTrip) {
dateRoundTrip = require('./formatters/dateRoundTrip')();
}
if (!monthDay) {
monthDay = require('./formatters/monthDay')();
}
if (!generalDateTimeLong) {
generalDateTimeLong = require('./formatters/generalDateTimeLong')();
}
if (!generalDateTimeShort) {
generalDateTimeShort = require('./formatters/generalDateTimeShort')();
}
return function(str) {
return new injector(str, currency, decimal, fixed, exponential, genericNumber, number, percent, roundTrip, hexadecimal);
return new injector(
str,
currency,
decimal,
fixed,
exponential,
genericNumber,
number,
percent,
roundTrip,
hexadecimal,
shortDate,
longDate,
fullDateShortTime,
fullDateLongTime,
yearMonth,
universalFull,
universalSortable,
longTime,
shortTime,
sortableDate,
rfc1123,
dateRoundTrip,
monthDay,
generalDateTimeLong,
generalDateTimeShort
);
};
};
{
"name": "elemental-string-format",
"version": "0.0.1",
"version": "0.1.0",
"description": "String formatting",

@@ -19,3 +19,6 @@ "main": "index.js",

"test": "./node_modules/.bin/nyc ./node_modules/.bin/jasmine"
},
"dependencies": {
"dateformat": "^3.0.3"
}
}

@@ -104,2 +104,3 @@ const

expect(stringFormat("{0:N3}", -1234.56)).toEqual("-1,234.560");
expect(stringFormat("{0:N}", 1234567890)).toEqual("1,234,567,890.00");

@@ -154,4 +155,180 @@ process.env.LANG = 'sv_SE';

const shortDateFormat = (done) => {
process.env.LANG = 'en_US';
var d = new Date(2020, 0, 1);
expect(stringFormat("{0:d}", d)).toEqual("01/1/2020");
done();
};
const longDateFormat = (done) => {
process.env.LANG = 'en_US';
var d = new Date(2020, 0, 1);
expect(stringFormat("{0:D}", d)).toEqual("Wednesday, January 1, 2020");
done();
};
const fullDateShortTimeFormat = (done) => {
process.env.LANG = 'en_US';
let d = new Date(2020, 0, 1, 13, 34, 45);
expect(stringFormat("{0:f}", d)).toEqual("Wednesday, January 1, 2020 1:34 PM");
process.env.LANG = 'sv_SE';
expect(stringFormat("{0:f}", d)).toEqual("den 1 January 2020 13:34");
done();
};
const fullDateLongTimeFormat = (done) => {
process.env.LANG = 'en_US';
let d = new Date(2020, 0, 1, 13, 34, 45);
expect(stringFormat("{0:F}", d)).toEqual("Wednesday, January 1, 2020 1:34:45 PM");
process.env.LANG = 'sv_SE';
expect(stringFormat("{0:F}", d)).toEqual("den 1 January 2020 13:34:45");
done();
};
const yearMonthTest = (done) => {
process.env.LANG = 'en_US';
let d = new Date(2020, 0, 1, 13, 34, 45);
expect(stringFormat("{0:Y}", d)).toEqual("January 2020");
process.env.LANG = 'sv_SE';
expect(stringFormat("{0:y}", d)).toEqual("January 2020");
done();
};
const universalFullTest = (done) => {
process.env.LANG = 'en_US';
let d = new Date(2020, 0, 1, 13, 34, 45);
expect(stringFormat("{0:U}", d)).toEqual("Wednesday, January 1, 2020 1:34:45 PM");
process.env.LANG = 'sv_SE';
expect(stringFormat("{0:U}", d)).toEqual("Wednesday, January 1, 2020 1:34:45 PM");
done();
};
const universalSortableTest = (done) => {
process.env.LANG = 'en_US';
let d = new Date(2020, 0, 1, 13, 34, 45);
expect(stringFormat("{0:u}", d)).toEqual("2020-01-01 13:34:45Z");
process.env.LANG = 'sv_SE';
expect(stringFormat("{0:u}", d)).toEqual("2020-01-01 13:34:45Z");
done();
};
const longTimeTest = (done) => {
process.env.LANG = 'en_US';
let d = new Date(2020, 0, 1, 13, 34, 45);
expect(stringFormat("{0:T}", d)).toEqual("1:34:45 PM");
process.env.LANG = 'sv_SE';
expect(stringFormat("{0:T}", d)).toEqual("13:34:45");
done();
};
const shortTimeTest = (done) => {
process.env.LANG = 'en_US';
let d = new Date(2020, 0, 1, 13, 34, 45);
expect(stringFormat("{0:t}", d)).toEqual("1:34 PM");
process.env.LANG = 'sv_SE';
expect(stringFormat("{0:t}", d)).toEqual("13:34");
done();
};
const rfc1123Test = (done) => {
process.env.LANG = 'en_US';
let d = new Date(2020, 0, 1, 13, 34, 45);
expect(stringFormat("{0:R}", d)).toContain("Wed, 1 Jan 2020 13:34:45");
process.env.LANG = 'sv_SE';
expect(stringFormat("{0:R}", d)).toContain("Wed, 1 Jan 2020 13:34:45");
done();
};
const roundTripDateTest = (done) => {
process.env.LANG = 'en_US';
let d = new Date(2020, 0, 1, 13, 34, 45);
expect(stringFormat("{0:O}", d)).toEqual(stringFormat("{0:o}", new Date(stringFormat("{0:O}", d))));
process.env.LANG = 'sv_SE';
expect(stringFormat("{0:O}", d)).toEqual(stringFormat("{0:o}", new Date(stringFormat("{0:O}", d))));
done();
};
const monthDayTest = (done) => {
process.env.LANG = 'en_US';
let d = new Date(2020, 0, 1, 13, 34, 45);
expect(stringFormat("{0:M}", d)).toEqual("January 1");
process.env.LANG = 'sv_SE';
expect(stringFormat("{0:m}", d)).toEqual("January 1");
done();
};
const generalLongTimeTest = (done) => {
process.env.LANG = 'en_US';
let d = new Date(2020, 0, 1, 13, 34, 45);
expect(stringFormat("{0:G}", d)).toEqual("01/1/2020 1:34:45 PM");
process.env.LANG = 'sv_SE';
expect(stringFormat("{0:G}", d)).toEqual("01/01/2020 13:34:45");
done();
};
const generalShortTimeTest = (done) => {
process.env.LANG = 'en_US';
let d = new Date(2020, 0, 1, 13, 34, 45);
expect(stringFormat("{0:g}", d)).toEqual("01/1/2020 1:34 PM");
process.env.LANG = 'sv_SE';
expect(stringFormat("{0:g}", d)).toEqual("01/01/2020 13:34");
done();
};
describe("A string formatter", () => {
it("ignores none strings", notStringIgnoreTest);
/* number tests */
it("can inject variables into a string", variableTest);

@@ -168,2 +345,18 @@ it("can align values", alignTest);

it("can format hexadecimals", hexTest);
/* date tests */
it("can format short dates", shortDateFormat);
it("can format long dates", longDateFormat);
it("can format full dates with short time", fullDateShortTimeFormat);
it("can format full dates with long time", fullDateLongTimeFormat);
it("can format year month", yearMonthTest);
it("can format universal full", universalFullTest);
it("can format universal sortable", universalSortableTest);
it("can format long time", longTimeTest);
it("can format short time", shortTimeTest);
it("can format rfc1123", rfc1123Test);
it("can round trip dates", roundTripDateTest);
it("can format month day", monthDayTest);
it("can format general long time", generalLongTimeTest);
it("can format general short time", generalShortTimeTest);
});
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