Skip to content

Commit

Permalink
Merge pull request #2127 from Gobie:patch/cleanup
Browse files Browse the repository at this point in the history
Code cleanup and speedup
  • Loading branch information
ichernev committed Jul 12, 2015
2 parents eef69fc + 1cee6e7 commit 49f8886
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 45 deletions.
43 changes: 43 additions & 0 deletions benchmarks/zeroFill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
var Benchmark = require('benchmark');

module.exports = {
name: 'zeroFill',
tests: {
zeroFillMath: {
setup: function() {
var zeroFillMath = function(number, targetLength, forceSign) {
var absNumber = '' + Math.abs(number),
zerosToFill = targetLength - absNumber.length,
sign = number >= 0;
return (sign ? (forceSign ? '+' : '') : '-') +
Math.pow(10, Math.max(0, zerosToFill)).toString().substr(1) + absNumber;
}
},
fn: function() {
zeroFillMath(Math.random() * 1e5 | 0, 5);
zeroFillMath(Math.random() * 1e5 | 0, 10);
zeroFillMath(Math.random() * 1e10 | 0, 20);
},
async: true
},
zeroFillWhile: {
setup: function() {
var zeroFillWhile = function(number, targetLength, forceSign) {
var output = '' + Math.abs(number),
sign = number >= 0;

while (output.length < targetLength) {
output = '0' + output;
}
return (sign ? (forceSign ? '+' : '') : '-') + output;
}
},
fn: function() {
zeroFillWhile(Math.random() * 1e5 | 0, 5);
zeroFillWhile(Math.random() * 1e5 | 0, 10);
zeroFillWhile(Math.random() * 1e10 | 0, 20);
},
async: true
}
}
};
5 changes: 1 addition & 4 deletions src/lib/format/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,7 @@ export function formatMoment(m, format) {
}

format = expandFormat(format, m.localeData());

if (!formatFunctions[format]) {
formatFunctions[format] = makeFormatFunction(format);
}
formatFunctions[format] = formatFunctions[format] || makeFormatFunction(format);

return formatFunctions[format](m);
}
Expand Down
18 changes: 11 additions & 7 deletions src/lib/locale/formats.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ export var defaultLongDateFormat = {
};

export function longDateFormat (key) {
var output = this._longDateFormat[key];
if (!output && this._longDateFormat[key.toUpperCase()]) {
output = this._longDateFormat[key.toUpperCase()].replace(/MMMM|MM|DD|dddd/g, function (val) {
return val.slice(1);
});
this._longDateFormat[key] = output;
var format = this._longDateFormat[key],
formatUpper = this._longDateFormat[key.toUpperCase()];

if (format || !formatUpper) {
return format;
}
return output;

this._longDateFormat[key] = formatUpper.replace(/MMMM|MM|DD|dddd/g, function (val) {
return val.slice(1);
});

return this._longDateFormat[key];
}
4 changes: 1 addition & 3 deletions src/lib/locale/locales.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ export function getSetGlobalLocale (key, values) {
export function defineLocale (name, values) {
if (values !== null) {
values.abbr = name;
if (!locales[name]) {
locales[name] = new Locale();
}
locales[name] = locales[name] || new Locale();
locales[name].set(values);

// backwards compat for now: also set the locale
Expand Down
28 changes: 14 additions & 14 deletions src/lib/units/day-of-week.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,20 @@ addWeekParseToken(['d', 'e', 'E'], function (input, week, config, token) {
// HELPERS

function parseWeekday(input, locale) {
if (typeof input === 'string') {
if (!isNaN(input)) {
input = parseInt(input, 10);
}
else {
input = locale.weekdaysParse(input);
if (typeof input !== 'number') {
return null;
}
}
if (typeof input !== 'string') {
return input;
}

if (!isNaN(input)) {
return parseInt(input, 10);
}
return input;

input = locale.weekdaysParse(input);
if (typeof input === 'number') {
return input;
}

return null;
}

// LOCALES
Expand All @@ -91,9 +93,7 @@ export function localeWeekdaysMin (m) {
export function localeWeekdaysParse (weekdayName) {
var i, mom, regex;

if (!this._weekdaysParse) {
this._weekdaysParse = [];
}
this._weekdaysParse = this._weekdaysParse || [];

for (i = 0; i < 7; i++) {
// make the regex if we don't have it already
Expand Down
7 changes: 1 addition & 6 deletions src/lib/units/offset.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,7 @@ export function setOffsetToParsedOffset () {
}

export function hasAlignedHourOffset (input) {
if (!input) {
input = 0;
}
else {
input = createLocal(input).utcOffset();
}
input = input ? createLocal(input).utcOffset() : 0;

return (this.utcOffset() - input) % 60 === 0;
}
Expand Down
8 changes: 3 additions & 5 deletions src/lib/utils/to-int.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import absFloor from './abs-floor';

export default function toInt(argumentForCoercion) {
var coercedNumber = +argumentForCoercion,
value = 0;

if (coercedNumber !== 0 && isFinite(coercedNumber)) {
if (coercedNumber >= 0) {
value = Math.floor(coercedNumber);
} else {
value = Math.ceil(coercedNumber);
}
value = absFloor(coercedNumber);
}

return value;
Expand Down
10 changes: 4 additions & 6 deletions src/lib/utils/zero-fill.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
export default function zeroFill(number, targetLength, forceSign) {
var output = '' + Math.abs(number),
var absNumber = '' + Math.abs(number),
zerosToFill = targetLength - absNumber.length,
sign = number >= 0;

while (output.length < targetLength) {
output = '0' + output;
}
return (sign ? (forceSign ? '+' : '') : '-') + output;
return (sign ? (forceSign ? '+' : '') : '-') +
Math.pow(10, Math.max(0, zerosToFill)).toString().substr(1) + absNumber;
}

0 comments on commit 49f8886

Please sign in to comment.