Skip to content

Commit

Permalink
Avoid type error and consolidate code to function
Browse files Browse the repository at this point in the history
  • Loading branch information
fbennett committed May 29, 2019
1 parent d08f6d8 commit ae4c774
Showing 1 changed file with 22 additions and 32 deletions.
54 changes: 22 additions & 32 deletions src/node_datepart.js
Expand Up @@ -15,6 +15,26 @@ CSL.Node["date-part"] = {

var date_variable = state.build.date_variables[0];

function formatAndStrip(myform, gender, val) {
if (!val) {
return val;
}
val = "" + CSL.Util.Dates[this.strings.name][myform](state, val, gender, this.default_locale);
if ("month" === this.strings.name) {
if (state.tmp.strip_periods) {
val = val.replace(/\./g, "");
} else {
for (var i = 0, ilen = this.decorations.length; i < ilen; i += 1) {
if ("@strip-periods" === this.decorations[i][0] && "true" === this.decorations[i][1]) {
val = val.replace(/\./g, "");
break;
}
}
}
}
return val;
}

func = function (state, Item) {

if (!state.tmp.date_object) {
Expand Down Expand Up @@ -97,38 +117,8 @@ CSL.Node["date-part"] = {
myform = "numeric";
}
}
value = CSL.Util.Dates[this.strings.name][myform](state, value, gender, this.default_locale);
if ("month" === this.strings.name) {
// XXXXX Cut-and-paste code in multiple locations. This code block should be
// collected in a function.
// Tag: strip-periods-block
if (state.tmp.strip_periods) {
value = value.replace(/\./g, "");
} else {
for (var i = 0, ilen = this.decorations.length; i < ilen; i += 1) {
if ("@strip-periods" === this.decorations[i][0] && "true" === this.decorations[i][1]) {
value = value.replace(/\./g, "");
break;
}
}
}
}
if (value_end) {
value_end = CSL.Util.Dates[this.strings.name][myform](state, value_end, gender, ("accessed" === date_variable), "_end");
// XXXXX Cut-and-paste code in multiple locations. This code block should be
// collected in a function.
// Tag: strip-periods-block
if (state.tmp.strip_periods) {
value_end = value_end.replace(/\./g, "");
} else {
for (var i = 0, ilen = this.decorations.length; i < ilen; i += 1) {
if ("@strip-periods" === this.decorations[i][0] && "true" === this.decorations[i][1]) {
value_end = value_end.replace(/\./g, "");
break;
}
}
}
}
value = formatAndStrip.call(this, myform, gender, value);
value_end = formatAndStrip.call(this, myform, gender, value_end);
}
state.output.openLevel("empty");
if (state.tmp.date_collapse_at.length) {
Expand Down

0 comments on commit ae4c774

Please sign in to comment.