Socket
Socket
Sign inDemoInstall

js-joda

Package Overview
Dependencies
Maintainers
1
Versions
63
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

js-joda - npm Package Compare versions

Comparing version 0.2.5 to 0.3.6

date.html

2

package.json
{
"name": "js-joda",
"version": "0.2.5",
"version": "0.3.6",
"description": "a date and time library for javascript",

@@ -5,0 +5,0 @@ "repository": {

@@ -45,2 +45,8 @@ /**

export class IllegalStateException extends ExtendableError {
constructor(message = 'IllegalStateException') {
super(message);
}
}
export class NullPointerException extends ExtendableError {

@@ -47,0 +53,0 @@ constructor(message = 'NullPointerException') {

@@ -7,5 +7,9 @@ /*

import {requireNonNull} from '../assert';
import {DateTimeException} from '../errors';
import {MathUtil} from '../MathUtil';
import {EnumMap} from './EnumMap';
import {ResolverStyle} from './ResolverStyle';
import {IsoChronology} from '../chrono/IsoChronology';

@@ -19,2 +23,3 @@ import {ChronoLocalDate} from '../chrono/ChronoLocalDate';

import {LocalDate} from '../LocalDate';
import {Period} from '../Period';

@@ -74,2 +79,44 @@ //import {ZoneOffset} from '../ZoneOffset';

/**
*
* @param {TemporalField} field
* @return {Number} field value
*/
getFieldValue0(field) {
return this.fieldValues.get(field);
}
/**
* Adds a field-value pair to the builder.
* <p>
* This adds a field to the builder.
* If the field is not already present, then the field-value pair is added to the map.
* If the field is already present and it has the same value as that specified, no action occurs.
* If the field is already present and it has a different value to that specified, then
* an exception is thrown.
*
* @param {TemporalField} field the field to add, not null
* @param {Number} value the value to add, not null
* @return {DateTimeBuilder}, this for method chaining
* @throws DateTimeException if the field is already present with a different value
*/
_addFieldValue(field, value) {
requireNonNull(field, 'field');
var old = this.getFieldValue0(field); // check first for better error message
if (old != null && old.longValue() !== value) {
throw new DateTimeException('Conflict found: ' + field + ' ' + old + ' differs from ' + field + ' ' + value + ': ' + this);
}
return this._putFieldValue0(field, value);
}
/**
* @param {TemporalField} field
* @param {Number} value
* @param {DateTimeBuilder} this
*/
_putFieldValue0(field, value) {
this.fieldValues.put(field, value);
return this;
}
/**
* Resolves the builder, evaluating the date and time.

@@ -83,3 +130,3 @@ * <p>

* @param resolverFields
* @return {@code this}, for method chaining
* @return {DateTimeBuilder} this, for method chaining
*/

@@ -93,3 +140,3 @@ resolve(resolverStyle, resolverFields) {

this._mergeDate(resolverStyle);
//mergeTime(resolverStyle);
this._mergeTime(resolverStyle);
//if (resolveFields(resolverStyle)) {

@@ -100,8 +147,8 @@ // mergeInstantFields();

//}
//resolveTimeInferZeroes(resolverStyle);
//crossCheck();
//if (excessDays != null && excessDays.isZero() == false && date != null && time != null) {
// date = date.plus(excessDays);
// excessDays = Period.ZERO;
//}
this._resolveTimeInferZeroes(resolverStyle);
//this._crossCheck();
if (this.excessDays != null && this.excessDays.isZero() === false && this.date != null && this.time != null) {
this.date = this.date.plus(this.excessDays);
this.excessDays = Period.ZERO;
}
//resolveFractional();

@@ -149,2 +196,216 @@ //resolveInstant();

_mergeTime(resolverStyle) {
if (this.fieldValues.containsKey(ChronoField.CLOCK_HOUR_OF_DAY)) {
let ch = this.fieldValues.remove(ChronoField.CLOCK_HOUR_OF_DAY);
if (resolverStyle !== ResolverStyle.LENIENT) {
if (resolverStyle === ResolverStyle.SMART && ch === 0) {
// ok
} else {
ChronoField.CLOCK_HOUR_OF_DAY.checkValidValue(ch);
}
}
this._addFieldValue(ChronoField.HOUR_OF_DAY, ch === 24 ? 0 : ch);
}
if (this.fieldValues.containsKey(ChronoField.CLOCK_HOUR_OF_AMPM)) {
let ch = this.fieldValues.remove(ChronoField.CLOCK_HOUR_OF_AMPM);
if (resolverStyle !== ResolverStyle.LENIENT) {
if (resolverStyle === ResolverStyle.SMART && ch === 0) {
// ok
} else {
ChronoField.CLOCK_HOUR_OF_AMPM.checkValidValue(ch);
}
}
this._addFieldValue(ChronoField.HOUR_OF_AMPM, ch === 12 ? 0 : ch);
}
if (resolverStyle !== ResolverStyle.LENIENT) {
if (this.fieldValues.containsKey(ChronoField.AMPM_OF_DAY)) {
ChronoField.AMPM_OF_DAY.checkValidValue(this.fieldValues.get(ChronoField.AMPM_OF_DAY));
}
if (this.fieldValues.containsKey(ChronoField.HOUR_OF_AMPM)) {
ChronoField.HOUR_OF_AMPM.checkValidValue(this.fieldValues.get(ChronoField.HOUR_OF_AMPM));
}
}
if (this.fieldValues.containsKey(ChronoField.AMPM_OF_DAY) && this.fieldValues.containsKey(ChronoField.HOUR_OF_AMPM)) {
let ap = this.fieldValues.remove(ChronoField.AMPM_OF_DAY);
let hap = this.fieldValues.remove(ChronoField.HOUR_OF_AMPM);
this._addFieldValue(ChronoField.HOUR_OF_DAY, ap * 12 + hap);
}
// if (timeFields.containsKey(HOUR_OF_DAY) && timeFields.containsKey(MINUTE_OF_HOUR)) {
// let hod = timeFields.remove(HOUR_OF_DAY);
// let moh = timeFields.remove(MINUTE_OF_HOUR);
// this._addFieldValue(MINUTE_OF_DAY, hod * 60 + moh);
// }
// if (timeFields.containsKey(MINUTE_OF_DAY) && timeFields.containsKey(SECOND_OF_MINUTE)) {
// let mod = timeFields.remove(MINUTE_OF_DAY);
// let som = timeFields.remove(SECOND_OF_MINUTE);
// this._addFieldValue(SECOND_OF_DAY, mod * 60 + som);
// }
if (this.fieldValues.containsKey(ChronoField.NANO_OF_DAY)) {
let nod = this.fieldValues.remove(ChronoField.NANO_OF_DAY);
if (resolverStyle !== ResolverStyle.LENIENT) {
ChronoField.NANO_OF_DAY.checkValidValue(nod);
}
this._addFieldValue(ChronoField.SECOND_OF_DAY, MathUtil.intDiv(nod, 1000000000));
this._addFieldValue(ChronoField.NANO_OF_SECOND, MathUtil.intMod(nod, 1000000000));
}
if (this.fieldValues.containsKey(ChronoField.MICRO_OF_DAY)) {
let cod = this.fieldValues.remove(ChronoField.MICRO_OF_DAY);
if (resolverStyle !== ResolverStyle.LENIENT) {
ChronoField.MICRO_OF_DAY.checkValidValue(cod);
}
this._addFieldValue(ChronoField.SECOND_OF_DAY, MathUtil.intDiv(cod, 1000000));
this._addFieldValue(ChronoField.MICRO_OF_SECOND, MathUtil.intMod(cod, 1000000));
}
if (this.fieldValues.containsKey(ChronoField.MILLI_OF_DAY)) {
let lod = this.fieldValues.remove(ChronoField.MILLI_OF_DAY);
if (resolverStyle !== ResolverStyle.LENIENT) {
ChronoField.MILLI_OF_DAY.checkValidValue(lod);
}
this._addFieldValue(ChronoField.SECOND_OF_DAY, MathUtil.intDiv(lod, 1000));
this._addFieldValue(ChronoField.MILLI_OF_SECOND, MathUtil.intMod(lod, 1000));
}
if (this.fieldValues.containsKey(ChronoField.SECOND_OF_DAY)) {
let sod = this.fieldValues.remove(ChronoField.SECOND_OF_DAY);
if (resolverStyle !== ResolverStyle.LENIENT) {
ChronoField.SECOND_OF_DAY.checkValidValue(sod);
}
this._addFieldValue(ChronoField.HOUR_OF_DAY, MathUtil.intDiv(sod, 3600));
this._addFieldValue(ChronoField.MINUTE_OF_HOUR, MathUtil.intMod(MathUtil.intDiv(sod, 60), 60));
this._addFieldValue(ChronoField.SECOND_OF_MINUTE, MathUtil.intMod(sod, 60));
}
if (this.fieldValues.containsKey(ChronoField.MINUTE_OF_DAY)) {
let mod = this.fieldValues.remove(ChronoField.MINUTE_OF_DAY);
if (resolverStyle !== ResolverStyle.LENIENT) {
ChronoField.MINUTE_OF_DAY.checkValidValue(mod);
}
this._addFieldValue(ChronoField.HOUR_OF_DAY, MathUtil.intDiv(mod, 60));
this._addFieldValue(ChronoField.MINUTE_OF_HOUR, MathUtil.intMod(mod, 60));
}
// let sod = MathUtil.intDiv(nod, 1000000000L);
// this._addFieldValue(HOUR_OF_DAY, MathUtil.intDiv(sod, 3600));
// this._addFieldValue(MINUTE_OF_HOUR, MathUtil.intMod(MathUtil.intDiv(sod, 60), 60));
// this._addFieldValue(SECOND_OF_MINUTE, MathUtil.intMod(sod, 60));
// this._addFieldValue(NANO_OF_SECOND, MathUtil.intMod(nod, 1000000000L));
if (resolverStyle !== ResolverStyle.LENIENT) {
if (this.fieldValues.containsKey(ChronoField.MILLI_OF_SECOND)) {
ChronoField.MILLI_OF_SECOND.checkValidValue(this.fieldValues.get(ChronoField.MILLI_OF_SECOND));
}
if (this.fieldValues.containsKey(ChronoField.MICRO_OF_SECOND)) {
ChronoField.MICRO_OF_SECOND.checkValidValue(this.fieldValues.get(ChronoField.MICRO_OF_SECOND));
}
}
if (this.fieldValues.containsKey(ChronoField.MILLI_OF_SECOND) && this.fieldValues.containsKey(ChronoField.MICRO_OF_SECOND)) {
let los = this.fieldValues.remove(ChronoField.MILLI_OF_SECOND);
let cos = this.fieldValues.get(ChronoField.MICRO_OF_SECOND);
this._addFieldValue(ChronoField.MICRO_OF_SECOND, los * 1000 + (MathUtil.intMod(cos, 1000)));
}
if (this.fieldValues.containsKey(ChronoField.MICRO_OF_SECOND) && this.fieldValues.containsKey(ChronoField.NANO_OF_SECOND)) {
let nos = this.fieldValues.get(ChronoField.NANO_OF_SECOND);
this._addFieldValue(ChronoField.MICRO_OF_SECOND, MathUtil.intDiv(nos, 1000));
this.fieldValues.remove(ChronoField.MICRO_OF_SECOND);
}
if (this.fieldValues.containsKey(ChronoField.MILLI_OF_SECOND) && this.fieldValues.containsKey(ChronoField.NANO_OF_SECOND)) {
let nos = this.fieldValues.get(ChronoField.NANO_OF_SECOND);
this._addFieldValue(ChronoField.MILLI_OF_SECOND, MathUtil.intDiv(nos, 1000000));
this.fieldValues.remove(ChronoField.MILLI_OF_SECOND);
}
if (this.fieldValues.containsKey(ChronoField.MICRO_OF_SECOND)) {
let cos = this.fieldValues.remove(ChronoField.MICRO_OF_SECOND);
this._addFieldValue(ChronoField.NANO_OF_SECOND, cos * 1000);
} else if (this.fieldValues.containsKey(ChronoField.MILLI_OF_SECOND)) {
let los = this.fieldValues.remove(ChronoField.MILLI_OF_SECOND);
this._addFieldValue(ChronoField.NANO_OF_SECOND, los * 1000000);
}
}
/**
*
* @param {ResolverStyle} resolverStyle
* @private
*/
_resolveTimeInferZeroes(resolverStyle) {
let hod = this.fieldValues.get(ChronoField.HOUR_OF_DAY);
let moh = this.fieldValues.get(ChronoField.MINUTE_OF_HOUR);
let som = this.fieldValues.get(ChronoField.SECOND_OF_MINUTE);
let nos = this.fieldValues.get(ChronoField.NANO_OF_SECOND);
if (hod == null) {
return;
}
if (moh == null && (som != null || nos != null)) {
return;
}
if (moh != null && som == null && nos != null) {
return;
}
if (resolverStyle !== ResolverStyle.LENIENT) {
if (hod != null) {
if (resolverStyle === ResolverStyle.SMART &&
hod.longValue() === 24 &&
(moh == null || moh.longValue() === 0) &&
(som == null || som.longValue() === 0) &&
(nos == null || nos.longValue() === 0)) {
hod = 0;
this.excessDays = Period.ofDays(1);
}
let hodVal = ChronoField.HOUR_OF_DAY.checkValidIntValue(hod);
if (moh != null) {
let mohVal = ChronoField.MINUTE_OF_HOUR.checkValidIntValue(moh);
if (som != null) {
let somVal = ChronoField.SECOND_OF_MINUTE.checkValidIntValue(som);
if (nos != null) {
let nosVal = ChronoField.NANO_OF_SECOND.checkValidIntValue(nos);
this._addObject(LocalTime.of(hodVal, mohVal, somVal, nosVal));
} else {
this._addObject(LocalTime.of(hodVal, mohVal, somVal));
}
} else {
if (nos == null) {
this._addObject(LocalTime.of(hodVal, mohVal));
}
}
} else {
if (som == null && nos == null) {
this._addObject(LocalTime.of(hodVal, 0));
}
}
}
} else {
if (hod != null) {
let hodVal = hod;
if (moh != null) {
if (som != null) {
if (nos == null) {
nos = 0;
}
let totalNanos = MathUtil.safeMultiply(hodVal, 3600000000000);
totalNanos = MathUtil.safeAdd(totalNanos, MathUtil.safeMultiply(moh, 60000000000));
totalNanos = MathUtil.safeAdd(totalNanos, MathUtil.safeMultiply(som, 1000000000));
totalNanos = MathUtil.safeAdd(totalNanos, nos);
let excessDays = MathUtil.floorDiv(totalNanos, 86400000000000); // safe int cast
let nod = MathUtil.floorMod(totalNanos, 86400000000000);
this._addObject(LocalTime.ofNanoOfDay(nod));
this.excessDays = Period.ofDays(excessDays);
} else {
let totalSecs = MathUtil.safeMultiply(hodVal, 3600);
totalSecs = MathUtil.safeAdd(totalSecs, MathUtil.safeMultiply(moh, 60));
let excessDays = MathUtil.floorDiv(totalSecs, 86400); // safe int cast
let sod = MathUtil.floorMod(totalSecs, 86400);
this._addObject(LocalTime.ofSecondOfDay(sod));
this.excessDays = Period.ofDays(excessDays);
}
} else {
let excessDays = MathUtil.safeToInt(MathUtil.floorDiv(hodVal, 24));
hodVal = MathUtil.floorMod(hodVal, 24);
this._addObject(LocalTime.of(hodVal, 0));
this.excessDays = Period.ofDays(excessDays);
}
}
}
this.fieldValues.remove(ChronoField.HOUR_OF_DAY);
this.fieldValues.remove(ChronoField.MINUTE_OF_HOUR);
this.fieldValues.remove(ChronoField.SECOND_OF_MINUTE);
this.fieldValues.remove(ChronoField.NANO_OF_SECOND);
}
_addObject(dateOrTime) {

@@ -151,0 +412,0 @@ if (dateOrTime instanceof ChronoLocalDate){

@@ -253,2 +253,11 @@ /**

/**
* Returns the formatter as a composite printer parser.
*
* @param {boolean} optional whether the printer/parser should be optional
* @return {CompositePrinterParser} the printer/parser, not null
*/
toPrinterParser(optional) {
return this._printerParser.withOptional(optional);
}

@@ -276,7 +285,7 @@ toString() {

.appendValue(ChronoField.MINUTE_OF_HOUR, 2)
//.optionalStart()
.optionalStart()
.appendLiteral(':')
.appendValue(ChronoField.SECOND_OF_MINUTE, 2)
//.optionalStart()
//.appendFraction(ChronoField.NANO_OF_SECOND, 0, 9, true)
.optionalStart()
.appendFraction(ChronoField.NANO_OF_SECOND, 0, 9, true)
.toFormatter(ResolverStyle.STRICT);

@@ -286,7 +295,7 @@

.parseCaseInsensitive()
//.append(DateTimeFormatter.ISO_LOCAL_DATE)
.append(DateTimeFormatter.ISO_LOCAL_DATE)
.appendLiteral('T')
//.append(DateTimeFormatter.ISO_LOCAL_TIME)
.append(DateTimeFormatter.ISO_LOCAL_TIME)
.toFormatter(ResolverStyle.STRICT).withChronology(IsoChronology.INSTANCE);
}

@@ -7,4 +7,4 @@ /**

import {assert} from '../assert';
import {ArithmeticException, DateTimeException, IllegalArgumentException} from '../errors';
import {assert, requireNonNull} from '../assert';
import {ArithmeticException, DateTimeException, IllegalArgumentException, IllegalStateException} from '../errors';

@@ -22,3 +22,9 @@ import {Enum} from '../Enum';

constructor(){
/**
* Constructs a new instance of the builder.
*
* @param {DateTimeFormatterBuilder} parent the parent builder, not null
* @param {boolean} optional whether the formatter is optional, not null
*/
constructor(parent=null, optional=false){
/**

@@ -31,3 +37,3 @@ * The currently active builder, used by the outermost builder.

*/
this._parent = null;
this._parent = parent;

@@ -42,3 +48,3 @@ /**

*/
this._optional = false;
this._optional = optional;
/**

@@ -321,3 +327,107 @@ * The width to pad the next field to.

//-----------------------------------------------------------------------
/**
* Appends the fractional value of a date-time field to the formatter.
* <p>
* The fractional value of the field will be output including the
* preceding decimal point. The preceding value is not output.
* For example, the second-of-minute value of 15 would be output as {@code .25}.
* <p>
* The width of the printed fraction can be controlled. Setting the
* minimum width to zero will cause no output to be generated.
* The printed fraction will have the minimum width necessary between
* the minimum and maximum widths - trailing zeroes are omitted.
* No rounding occurs due to the maximum width - digits are simply dropped.
* <p>
* When parsing in strict mode, the number of parsed digits must be between
* the minimum and maximum width. When parsing in lenient mode, the minimum
* width is considered to be zero and the maximum is nine.
* <p>
* If the value cannot be obtained then an exception will be thrown.
* If the value is negative an exception will be thrown.
* If the field does not have a fixed set of valid values then an
* exception will be thrown.
* If the field value in the date-time to be printed is invalid it
* cannot be printed and an exception will be thrown.
*
* @param {TemporalField} field the field to append, not null
* @param {Number} minWidth the minimum width of the field excluding the decimal point, from 0 to 9
* @param {Number} maxWidth the maximum width of the field excluding the decimal point, from 1 to 9
* @param {boolean} decimalPoint whether to output the localized decimal point symbol
* @return this, for chaining, not null
* @throws IllegalArgumentException if the field has a variable set of valid values or
* either width is invalid
*/
appendFraction(field, minWidth, maxWidth, decimalPoint) {
this._appendInternal(new FractionPrinterParser(field, minWidth, maxWidth, decimalPoint));
return this;
}
//-----------------------------------------------------------------------
/**
* Mark the start of an optional section.
* <p>
* The output of printing can include optional sections, which may be nested.
* An optional section is started by calling this method and ended by calling
* {@link #optionalEnd()} or by ending the build process.
* <p>
* All elements in the optional section are treated as optional.
* During printing, the section is only output if data is available in the
* {@code TemporalAccessor} for all the elements in the section.
* During parsing, the whole section may be missing from the parsed string.
* <p>
* For example, consider a builder setup as
* {@code builder.appendValue(HOUR_OF_DAY,2).optionalStart().appendValue(MINUTE_OF_HOUR,2)}.
* The optional section ends automatically at the end of the builder.
* During printing, the minute will only be output if its value can be obtained from the date-time.
* During parsing, the input will be successfully parsed whether the minute is present or not.
*
* @return {DateTimeFormatterBuilder} this, for chaining, not null
*/
optionalStart() {
this._active.valueParserIndex = -1;
this._active = new DateTimeFormatterBuilder(this._active, true);
return this;
}
/**
* Ends an optional section.
* <p>
* The output of printing can include optional sections, which may be nested.
* An optional section is started by calling {@link #optionalStart()} and ended
* using this method (or at the end of the builder).
* <p>
* Calling this method without having previously called {@code optionalStart}
* will throw an exception.
* Calling this method immediately after calling {@code optionalStart} has no effect
* on the formatter other than ending the (empty) optional section.
* <p>
* All elements in the optional section are treated as optional.
* During printing, the section is only output if data is available in the
* {@code TemporalAccessor} for all the elements in the section.
* During parsing, the whole section may be missing from the parsed string.
* <p>
* For example, consider a builder setup as
* {@code builder.appendValue(HOUR_OF_DAY,2).optionalStart().appendValue(MINUTE_OF_HOUR,2).optionalEnd()}.
* During printing, the minute will only be output if its value can be obtained from the date-time.
* During parsing, the input will be successfully parsed whether the minute is present or not.
*
* @return {DateTimeFormatterBuilder} this, for chaining, not null
* @throws IllegalStateException if there was no previous call to {@code optionalStart}
*/
optionalEnd() {
if (this._active._parent == null) {
throw new IllegalStateException('Cannot call optionalEnd() as there was no previous call to optionalStart()');
}
if (this._active._printerParsers.length > 0) {
var cpp = new CompositePrinterParser(this._active._printerParsers, this._active._optional);
this._active = this._active._parent;
this._appendInternal(cpp);
} else {
this._active = this._active._parent;
}
return this;
}
/**
* Appends a printer and/or parser to the internal list handling padding.

@@ -378,3 +488,19 @@ *

//-----------------------------------------------------------------------
/**
* Appends all the elements of a formatter to the builder.
* <p>
* This method has the same effect as appending each of the constituent
* parts of the formatter directly to this builder.
*
* @param {DateTimeFormatter} formatter the formatter to add, not null
* @return this, for chaining, not null
*/
append(formatter) {
requireNonNull(formatter, 'formatter');
this._appendInternal(formatter.toPrinterParser(false));
return this;
}
/**
* Completes this builder by creating the DateTimeFormatter.

@@ -427,4 +553,4 @@ *

*
* @param optional the optional flag to set in the copy
* @return the new printer-parser, not null
* @param {boolean} optional the optional flag to set in the copy
* @return {CompositePrinterParser} the new printer-parser, not null
*/

@@ -499,5 +625,74 @@ withOptional(optional) {

/**
* Pads the output to a fixed width.
*/
class PadPrinterParserDecorator {
/**
* Constructor.
*
* @param printerParser the printer, not null
* @param padWidth the width to pad to, 1 or greater
* @param padChar the pad character
*/
constructor(printerParser, padWidth, padChar) {
// input checked by DateTimeFormatterBuilder
this._printerParser = printerParser;
this._padWidth = padWidth;
this._padChar = padChar;
}
print(context, buf) {
var preLen = buf.length();
if (this._printerParser.print(context, buf) === false) {
return false;
}
var len = buf.length() - preLen;
if (len > this._padWidth) {
throw new DateTimeException(
`Cannot print as output of ${len} characters exceeds pad width of ${this._padWidth}`);
}
for (let i = 0; i < this._padWidth - len; i++) {
buf.insert(preLen, this._padChar);
}
return true;
}
parse(context, text, position) {
// cache context before changed by decorated parser
var strict = context.isStrict();
var caseSensitive = context.isCaseSensitive();
// parse
assert(!(position > text.length));
if (position === text.length) {
return ~position; // no more characters in the string
}
var endPos = position + this._padWidth;
if (endPos > text.length) {
if (strict) {
return ~position; // not enough characters in the string to meet the parse width
}
endPos = text.length;
}
var pos = position;
while (pos < endPos &&
(caseSensitive ? text[pos] === this._padChar : context.charEquals(text[pos], this._padChar))) {
pos++;
}
text = text.substring(0, endPos);
var resultPos = this._printerParser.parse(context, text, pos);
if (resultPos !== endPos && strict) {
return ~(position + pos); // parse of decorated field didn't parse to the end
}
return resultPos;
}
toString() {
return `Pad(${this._printerParser},${this._padWidth}${(this._padChar === ' ' ? ')' : ',\'' + this._padChar + '\')')}`;
}
}
class SettingsParser extends Enum {
print(context, buf) {
print(/*context, buf*/) {
return true; // nothing to do here

@@ -527,2 +722,3 @@ }

}
SettingsParser.SENSITIVE = new SettingsParser('SENSITIVE');

@@ -533,3 +729,31 @@ SettingsParser.INSENSITIVE = new SettingsParser('INSENSITIVE');

/**
* Prints or parses a string literal.
*/
class StringLiteralPrinterParser {
constructor(literal) {
this._literal = literal;
}
print(context, buf) {
buf.append(this._literal);
return true;
}
parse(context, text, position) {
var length = text.length;
assert(!(position > length || position < 0));
if (context.subSequenceEquals(text, position, this._literal, 0, this._literal.length) === false) {
return ~position;
}
return position + this._literal.length;
}
toString() {
return '\'' + this._literal + '\'';
}
}
class NumberPrinterParser {

@@ -697,3 +921,3 @@

}
/**

@@ -711,3 +935,3 @@ * Stores the value.

}
toString() {

@@ -722,9 +946,15 @@ if (this._minWidth === 1 && this._maxWidth === MAX_WIDTH && this._signStyle === SignStyle.NORMAL) {

}
}
//-----------------------------------------------------------------------
import {MathUtil} from '../MathUtil';
/**
* Pads the output to a fixed width.
* TODO optimize FractionPrinterParser, fix documentation
*
* Prints and parses a numeric date-time field with optional padding.
*/
class PadPrinterParserDecorator {
class FractionPrinterParser {

@@ -734,26 +964,59 @@ /**

*
* @param printerParser the printer, not null
* @param padWidth the width to pad to, 1 or greater
* @param padChar the pad character
* @param {TemporalField} field the field to output, not null
* @param {Number} minWidth the minimum width to output, from 0 to 9
* @param {Number} maxWidth the maximum width to output, from 0 to 9
* @param {boolean} decimalPoint whether to output the localized decimal point symbol
*/
constructor(printerParser, padWidth, padChar) {
// input checked by DateTimeFormatterBuilder
this._printerParser = printerParser;
this._padWidth = padWidth;
this._padChar = padChar;
constructor(field, minWidth, maxWidth, decimalPoint) {
requireNonNull(field, 'field');
if (field.range().isFixed() === false) {
throw new IllegalArgumentException('Field must have a fixed set of values: ' + field);
}
if (minWidth < 0 || minWidth > 9) {
throw new IllegalArgumentException('Minimum width must be from 0 to 9 inclusive but was ' + minWidth);
}
if (maxWidth < 1 || maxWidth > 9) {
throw new IllegalArgumentException('Maximum width must be from 1 to 9 inclusive but was ' + maxWidth);
}
if (maxWidth < minWidth) {
throw new IllegalArgumentException('Maximum width must exceed or equal the minimum width but ' +
maxWidth + ' < ' + minWidth);
}
this.field = field;
this.minWidth = minWidth;
this.maxWidth = maxWidth;
this.decimalPoint = decimalPoint;
}
print(context, buf) {
var preLen = buf.length();
if (this._printerParser.print(context, buf) === false) {
var value = context.getValue(this.field);
if (value === null) {
return false;
}
var len = buf.length() - preLen;
if (len > this._padWidth) {
throw new DateTimeException(
`Cannot print as output of ${len} characters exceeds pad width of ${this._padWidth}`);
var symbols = context.symbols();
if (value === 0) { // scale is zero if value is zero
if (this.minWidth > 0) {
if (this.decimalPoint) {
buf.append(symbols.decimalSeparator());
}
for (let i = 0; i < this.minWidth; i++) {
buf.append(symbols.zeroDigit());
}
}
} else {
var fraction = this.convertToFraction(value, symbols.zeroDigit());
var outputScale = Math.min(Math.max(fraction.length, this.minWidth), this.maxWidth);
fraction = fraction.substr(0, outputScale);
if(fraction * 1 > 0 ) {
while (fraction.length > this.minWidth && fraction[fraction.length - 1] === '0') {
fraction = fraction.substr(0, fraction.length - 1);
}
}
var str = fraction;
str = symbols.convertNumberToI18N(str);
if (this.decimalPoint) {
buf.append(symbols.decimalSeparator());
}
buf.append(str);
}
for (let i = 0; i < this._padWidth - len; i++) {
buf.insert(preLen, this._padChar);
}
return true;

@@ -763,64 +1026,81 @@ }

parse(context, text, position) {
// cache context before changed by decorated parser
var strict = context.isStrict();
var caseSensitive = context.isCaseSensitive();
// parse
assert(!(position > text.length));
if (position === text.length) {
return ~position; // no more characters in the string
var effectiveMin = (context.isStrict() ? this.minWidth : 0);
var effectiveMax = (context.isStrict() ? this.maxWidth : 9);
var length = text.length;
if (position === length) {
// valid if whole field is optional, invalid if minimum width
return (effectiveMin > 0 ? ~position : position);
}
var endPos = position + this._padWidth;
if (endPos > text.length) {
if (strict) {
return ~position; // not enough characters in the string to meet the parse width
if (this.decimalPoint) {
if (text[position] !== context.symbols().decimalSeparator()) {
// valid if whole field is optional, invalid if minimum width
return (effectiveMin > 0 ? ~position : position);
}
endPos = text.length;
position++;
}
var minEndPos = position + effectiveMin;
if (minEndPos > length) {
return ~position; // need at least min width digits
}
var maxEndPos = Math.min(position + effectiveMax, length);
var total = 0; // can use int because we are only parsing up to 9 digits
var pos = position;
while (pos < endPos &&
(caseSensitive ? text[pos] === this._padChar : context.charEquals(text[pos], this._padChar))) {
pos++;
while (pos < maxEndPos) {
var ch = text.charAt(pos++);
var digit = context.symbols().convertToDigit(ch);
if (digit < 0) {
if (pos < minEndPos) {
return ~position; // need at least min width digits
}
pos--;
break;
}
total = total * 10 + digit;
}
text = text.substring(0, endPos);
var resultPos = this._printerParser.parse(context, text, pos);
if (resultPos !== endPos && strict) {
return ~(position + pos); // parse of decorated field didn't parse to the end
var moveLeft = pos - position;
var scale = Math.pow(10, moveLeft);
var value = this.convertFromFraction(total, scale);
return context.setParsedField(this.field, value, position, pos);
}
/**
*
* @param {Number} value the value to convert, must be valid for this rule
* @return {String} the value as a fraction within the range, from 0 to 1, not null
*/
convertToFraction(value, zeroDigit) {
var range = this.field.range();
range.checkValidValue(value, this.field);
var _min = range.minimum();
var _range = range.maximum() - _min + 1;
var _value = value - _min;
var _scaled = MathUtil.intDiv((_value * 1000000000), _range);
var fraction = '' + _scaled;
while(fraction.length < 9){
fraction = zeroDigit + fraction;
}
return resultPos;
return fraction;
}
/**
*
* @param {Number} fraction the fraction to convert, not null
* @return {Number} the value of the field, valid for this rule
* @throws DateTimeException if the value cannot be converted
*/
convertFromFraction(total, scale) {
var range = this.field.range();
var _min = range.minimum();
var _range = range.maximum() - _min + 1;
var _value = MathUtil.intDiv((total * _range), scale);
return _value;
}
toString() {
return `Pad(${this._printerParser},${this._padWidth}${(this._padChar === ' ' ? ')' : ',\'' + this._padChar + '\')')}`;
var decimal = (this.decimalPoint ? ',DecimalPoint' : '');
return 'Fraction(' + this.field + ',' + this.minWidth + ',' + this.maxWidth + decimal + ')';
}
}
/**
* Prints or parses a string literal.
*/
class StringLiteralPrinterParser {
constructor(literal) {
this._literal = literal;
}
print(context, buf) {
buf.append(this._literal);
return true;
}
parse(context, text, position) {
var length = text.length;
assert(!(position > length || position < 0));
if (context.subSequenceEquals(text, position, this._literal, 0, this._literal.length) === false) {
return ~position;
}
return position + this._literal.length;
}
toString() {
return '\'' + this._literal + '\'';
}
}
class StringBuilder {

@@ -854,7 +1134,8 @@ constructor(){

DateTimeFormatterBuilder.CompositePrinterParser = CompositePrinterParser;
DateTimeFormatterBuilder.PadPrinterParserDecorator = PadPrinterParserDecorator;
DateTimeFormatterBuilder.SettingsParser = SettingsParser;
DateTimeFormatterBuilder.CharLiteralPrinterParser = StringLiteralPrinterParser;
DateTimeFormatterBuilder.StringLiteralPrinterParser = StringLiteralPrinterParser;
DateTimeFormatterBuilder.NumberPrinterParser = NumberPrinterParser;
DateTimeFormatterBuilder.StringLiteralPrinterParser = StringLiteralPrinterParser;
DateTimeFormatterBuilder.CharLiteralPrinterParser = StringLiteralPrinterParser;
DateTimeFormatterBuilder.PadPrinterParserDecorator = PadPrinterParserDecorator;
DateTimeFormatterBuilder.FractionPrinterParser = FractionPrinterParser;
DateTimeFormatterBuilder.StringBuilder = StringBuilder;

@@ -55,3 +55,24 @@ /*

//-----------------------------------------------------------------------
/**
* Starts the parsing of an optional segment of the input.
*/
startOptional() {
this._parsed.push(this.currentParsed().copy());
}
/**
* Ends the parsing of an optional segment of the input.
*
* @param {boolean} successful whether the optional segment was successfully parsed
*/
endOptional(successful) {
if (successful) {
this._parsed.splice(this._parsed.length - 2, 1);
} else {
this._parsed.splice(this._parsed.length - 1, 1);
}
}
/**
* Checks if parsing is case sensitive.

@@ -184,2 +205,3 @@ *

cloned.leapSecond = this.leapSecond;
cloned.dateTimeParseContext = this.dateTimeParseContext;
return cloned;

@@ -186,0 +208,0 @@ }

@@ -13,2 +13,3 @@ /**

import {Clock} from './Clock';
import {LocalDateTime} from './LocalDateTime';

@@ -73,7 +74,7 @@ import {DateTimeFormatter} from './format/DateTimeFormatter';

*
* @param hour the hour-of-day to represent, from 0 to 23
* @param minute the minute-of-hour to represent, from 0 to 59
* @param second the second-of-minute to represent, from 0 to 59
* @param nanoOfSecond the nano-of-second to represent, from 0 to 999,999,999
* @return the local time, not null
* @param {Number} hour the hour-of-day to represent, from 0 to 23
* @param {Number} minute the minute-of-hour to represent, from 0 to 59
* @param {Number} second the second-of-minute to represent, from 0 to 59
* @param {Number} nanoOfSecond the nano-of-second to represent, from 0 to 999,999,999
* @return {LocalTime} the local time, not null
* @throws DateTimeException if the value of any field is out of range

@@ -170,6 +171,6 @@ */

*
* @param hour the hour-of-day to represent, validated from 0 to 23
* @param minute the minute-of-hour to represent, validated from 0 to 59
* @param second the second-of-minute to represent, validated from 0 to 59
* @param nanoOfSecond the nano-of-second to represent, validated from 0 to 999,999,999
* @param {Number} hour the hour-of-day to represent, validated from 0 to 23
* @param {Number} minute the minute-of-hour to represent, validated from 0 to 59
* @param {Number} second the second-of-minute to represent, validated from 0 to 59
* @param {Number} nanoOfSecond the nano-of-second to represent, validated from 0 to 999,999,999
*/

@@ -1271,8 +1272,5 @@ constructor(hour=0, minute=0, second=0, nanoOfSecond=0) {

var FROM;
LocalTime.FROM = () => {
return FROM || (FROM = createTemporalQuery('LocalTime.FROM', (temporal) => {
return LocalTime.from(temporal);
}));
};
LocalTime.FROM = createTemporalQuery('LocalTime.FROM', (temporal) => {
return LocalTime.from(temporal);
});

@@ -1279,0 +1277,0 @@ /**

@@ -8,4 +8,4 @@ /**

export const MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER ? Number.MAX_SAFE_INTEGER : Math.pow(2, 53) - 1; // Number.MAX_SAFE_INTEGER not defined in #@#$%! PhantomJS
export const MIN_SAFE_INTEGER = Number.MIN_SAFE_INTEGER ? Number.MIN_SAFE_INTEGER : -(Math.pow(2, 53) - 1); // Number.MIN_SAFE_INTEGER not defined in #@#$%! PhantomJS
export const MAX_SAFE_INTEGER = 9007199254740991;
export const MIN_SAFE_INTEGER = -9007199254740991;

@@ -21,6 +21,7 @@ /**

} else if(r < 0){
return Math.ceil(r);
r = Math.ceil(r);
} else {
return Math.floor(r);
r = Math.floor(r);
}
return MathUtil.safeZero(r);
}

@@ -33,6 +34,7 @@

} else if(r < 0){
return Math.ceil(r);
r = Math.ceil(r);
} else {
return Math.floor(r);
r = Math.floor(r);
}
return MathUtil.safeZero(r);
}

@@ -42,3 +44,3 @@

var r = Math.floor(x / y);
return r;
return MathUtil.safeZero(r);
}

@@ -48,30 +50,16 @@

var r = x - MathUtil.floorDiv(x, y) * y;
return r;
return MathUtil.safeZero(r);
}
static safeAdd(x, y) {
MathUtil.verifyInt(x);
MathUtil.verifyInt(y);
if (x === 0) {
let r = y;
if (r > MAX_SAFE_INTEGER || r < MIN_SAFE_INTEGER) {
throw new ArithmeticException('Invalid addition beyond MAX_SAFE_INTEGER!');
}
return r;
return MathUtil.safeZero(y);
}
if (y === 0) {
let r = x;
if (r > MAX_SAFE_INTEGER || r < MIN_SAFE_INTEGER) {
throw new ArithmeticException('Invalid addition beyond MAX_SAFE_INTEGER!');
}
return r;
return MathUtil.safeZero(x);
}
if (x === undefined || y === undefined) {
throw new ArithmeticException('Invalid addition using undefined as argument');
}
if (isNaN(x) || isNaN(y)) {
throw new ArithmeticException('Invalid addition using NaN as argument');
}
let r = x + y;
// detect overflow, since neither x nor y are 0 (checked above) r cannot be === x or === y
// TODO: is this correct and complete?
if (r > MAX_SAFE_INTEGER || r < MIN_SAFE_INTEGER || r === x || r === y) {
var r = MathUtil.safeToInt(x + y);
if (r === x || r === y) {
throw new ArithmeticException('Invalid addition beyond MAX_SAFE_INTEGER!');

@@ -83,37 +71,22 @@ }

static safeSubtract(x, y) {
if (x === 0) {
let r = y;
if (r > MAX_SAFE_INTEGER || r < MIN_SAFE_INTEGER) {
throw new ArithmeticException('Invalid addition beyond MAX_SAFE_INTEGER!');
}
return -1 * r;
MathUtil.verifyInt(x);
MathUtil.verifyInt(y);
if (x === 0 && y === 0) {
return 0;
} else if (x === 0) {
return MathUtil.safeZero(-1 * y);
} else if (y === 0) {
return MathUtil.safeZero(x);
}
if (y === 0) {
let r = x;
if (r > MAX_SAFE_INTEGER || r < MIN_SAFE_INTEGER) {
throw new ArithmeticException('Invalid addition beyond MAX_SAFE_INTEGER!');
}
return r;
}
if (x === undefined || y === undefined) {
throw new ArithmeticException('Invalid subtraction using undefined as argument');
}
if (isNaN(x) || isNaN(y)) {
throw new ArithmeticException('Invalid subtraction using NaN as argument');
}
let r = x - y;
// detect overflow, since neither x nor y are 0 (checked above) r cannot be === x or === y
// TODO: is this correct and complete?
if (r < MIN_SAFE_INTEGER || r > MAX_SAFE_INTEGER) {
throw new ArithmeticException('Invalid subtraction beyond MIN_SAFE_INTEGER! ' + x + '-' + y);
}
return r;
return MathUtil.safeToInt(x - y);
}
static safeMultiply(x, y) {
MathUtil.verifyInt(x);
MathUtil.verifyInt(y);
if (x === 1) {
return y;
return MathUtil.safeZero(y);
}
if (y === 1) {
return x;
return MathUtil.safeZero(x);
}

@@ -123,4 +96,4 @@ if (x === 0 || y === 0) {

}
let r = x * y;
if (r < MIN_SAFE_INTEGER || r > MAX_SAFE_INTEGER || r / y !== x || (x === MIN_SAFE_INTEGER && y === -1) || (y === MIN_SAFE_INTEGER && x === -1)) {
let r = MathUtil.safeToInt(x * y);
if (r / y !== x || (x === MIN_SAFE_INTEGER && y === -1) || (y === MIN_SAFE_INTEGER && x === -1)) {
throw new ArithmeticException('Multiplication overflows: ' + x + ' * ' + y);

@@ -131,5 +104,15 @@ }

static parseInt(value) {
var r = parseInt(value);
return MathUtil.safeToInt(r);
}
static safeToInt(value) {
if(value === 0){
return 0;
MathUtil.verifyInt(value);
return MathUtil.safeZero(value);
}
static verifyInt(value){
if (value == null) {
throw new ArithmeticException(`Invalid value: '${value}', using null or undefined as argument`);
}

@@ -142,14 +125,6 @@ if (isNaN(value)) {

}
return value;
}
static parseInt(value) {
var int = parseInt(value);
if (isNaN(int)) {
throw new ArithmeticException('Invalid int value parse to NaN: ' + value);
}
if (int > MAX_SAFE_INTEGER || int < MIN_SAFE_INTEGER) {
throw new ArithmeticException('Calculation overflows an int: ' + value);
}
return int;
static safeZero(value){
return value === 0 ? 0 : value;
}

@@ -156,0 +131,0 @@

@@ -6,2 +6,5 @@ /**

*/
import {MathUtil} from '../MathUtil';
import {Duration} from '../Duration';

@@ -347,3 +350,3 @@ import {Year} from '../Year';

*/
ChronoUnit.FOREVER = new ChronoUnit('Forever', Duration.ofSeconds(Number.MAX_SAFE_INTEGER, 999999999));
ChronoUnit.FOREVER = new ChronoUnit('Forever', Duration.ofSeconds(MathUtil.MAX_SAFE_INTEGER, 999999999));
}

@@ -6,4 +6,5 @@ /**

*/
import {assert} from '../assert';
import {DateTimeException} from '../errors';
import {DateTimeException, IllegalArgumentException} from '../errors';
import {MathUtil} from '../MathUtil';

@@ -32,7 +33,7 @@

assert(!(minSmallest > minLargest), 'Smallest minimum value \'' + minSmallest +
'\' must be less than largest minimum value \'' + minLargest + '\'');
'\' must be less than largest minimum value \'' + minLargest + '\'', IllegalArgumentException);
assert(!(maxSmallest > maxLargest), 'Smallest maximum value \'' + maxSmallest +
'\' must be less than largest maximum value \'' + maxLargest + '\'');
'\' must be less than largest maximum value \'' + maxLargest + '\'', IllegalArgumentException);
assert(!(minLargest > maxLargest), 'Minimum value \'' + minLargest +
'\' must be less than maximum value \'' + maxLargest + '\'');
'\' must be less than maximum value \'' + maxLargest + '\'', IllegalArgumentException);

@@ -45,2 +46,15 @@ this._minSmallest = minSmallest;

/**
* Is the value range fixed and fully known.
* <p>
* For example, the ISO day-of-month runs from 1 to between 28 and 31.
* Since there is uncertainty about the maximum value, the range is not fixed.
* However, for the month of January, the range is always 1 to 31, thus it is fixed.
*
* @return {boolean} true if the set of values is fixed
*/
isFixed() {
return this._minSmallest === this._minLargest && this._maxSmallest === this._maxLargest;
}
minimum(){

@@ -122,6 +136,38 @@ return this._minSmallest;

*/
isIntValue() {
isIntValue() { // should be isSafeIntegerValue
return this.minimum() >= MathUtil.MIN_SAFE_INTEGER && this.maximum() <= MathUtil.MAX_SAFE_INTEGER;
}
/**
* Checks if this range is equal to another range.
* <p>
* The comparison is based on the four values, minimum, largest minimum,
* smallest maximum and maximum.
* Only objects of type {@code ValueRange} are compared, other types return false.
*
* @param other the object to check, null returns false
* @return {boolean} true if this is equal to the other range
*/
equals(other) {
if (other === this) {
return true;
}
if (other instanceof ValueRange) {
return this._minSmallest === other._minSmallest && this._minLargest === other._minLargest &&
this._maxSmallest === other._maxSmallest && this._maxLargest === other._maxLargest;
}
return false;
}
/**
* A hash code for this range.
*
* @return {Number} a suitable hash code
*/
hashCode() {
var hash = this._minSmallest + this._minLargest << 16 + this._minLargest >> 48 + this._maxSmallest << 32 +
this._maxSmallest >> 32 + this._maxLargest << 48 + this._maxLargest >> 16;
return (hash ^ (hash >>> 32));
}
/*

@@ -136,3 +182,2 @@ * Outputs this range as a String.

*/
toString() {

@@ -184,5 +229,5 @@ var str = this.minimum() + (this.minimum() !== this.largestMinimum() ? '/' + (this.largestMinimum()) : '');

} else {
return assert(false, 'Invalid number of arguments ' + arguments.length);
return assert(false, 'Invalid number of arguments ' + arguments.length, IllegalArgumentException);
}
}
}

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

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