Skip to content

Commit 2491527

Browse files
committed
Use template strings.
1 parent d9c4086 commit 2491527

File tree

15 files changed

+124
-122
lines changed

15 files changed

+124
-122
lines changed

.eslintrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@
166166
padded-blocks: [2, "never"],
167167
prefer-arrow-callback: 2,
168168
prefer-const: 2,
169+
prefer-template: 2,
169170
quote-props: [2, "consistent-as-needed"],
170171
quotes: [2, "single", "avoid-escape"],
171172
semi-spacing: 2,
@@ -178,6 +179,7 @@
178179
space-infix-ops: 2,
179180
space-unary-ops: 2,
180181
spaced-comment: [2, "always", { block: { markers: ["!"] } }],
182+
template-curly-spacing: 2,
181183
wrap-regex: 0,
182184
},
183185
}

perf/N3Lexer-perf.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ if (process.argv.length !== 3) {
1010

1111
const filename = process.argv[2];
1212

13-
const TEST = '- Lexing file ' + filename;
13+
const TEST = `- Lexing file ${filename}`;
1414
console.time(TEST);
1515

1616
let count = 0;
@@ -19,7 +19,7 @@ new N3.Lexer().tokenize(fs.createReadStream(filename), (error, token) => {
1919
count++;
2020
if (token.type === 'eof') {
2121
console.timeEnd(TEST);
22-
console.log('* Tokens lexed: ' + count);
23-
console.log('* Memory usage: ' + Math.round(process.memoryUsage().rss / 1024 / 1024) + 'MB');
22+
console.log(`* Tokens lexed: ${count}`);
23+
console.log(`* Memory usage: ${Math.round(process.memoryUsage().rss / 1024 / 1024)}MB`);
2424
}
2525
});

perf/N3Parser-perf.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ if (process.argv.length !== 3) {
1010
}
1111

1212
const filename = path.resolve(process.cwd(), process.argv[2]),
13-
base = 'file://' + filename;
13+
base = `file://${filename}`;
1414

15-
const TEST = '- Parsing file ' + filename;
15+
const TEST = `- Parsing file ${filename}`;
1616
console.time(TEST);
1717

1818
let count = 0;
@@ -22,7 +22,7 @@ new N3.Parser({ baseIRI: base }).parse(fs.createReadStream(filename), (error, qu
2222
count++;
2323
else {
2424
console.timeEnd(TEST);
25-
console.log('* Quads parsed: ' + count);
26-
console.log('* Memory usage: ' + Math.round(process.memoryUsage().rss / 1024 / 1024) + 'MB');
25+
console.log(`* Quads parsed: ${count}`);
26+
console.log(`* Memory usage: ${Math.round(process.memoryUsage().rss / 1024 / 1024)}MB`);
2727
}
2828
});

perf/N3Store-perf.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ let dimSquared = dim * dim;
1212
let dimCubed = dimSquared * dim;
1313

1414
let store = new N3.Store();
15-
let TEST = '- Adding ' + dimCubed + ' triples to the default graph';
15+
let TEST = `- Adding ${dimCubed} triples to the default graph`;
1616
console.time(TEST);
1717
let i, j, k, l;
1818
for (i = 0; i < dim; i++)
@@ -21,17 +21,17 @@ for (i = 0; i < dim; i++)
2121
store.addQuad(prefix + i, prefix + j, prefix + k);
2222
console.timeEnd(TEST);
2323

24-
console.log('* Memory usage for triples: ' + Math.round(process.memoryUsage().rss / 1024 / 1024) + 'MB');
24+
console.log(`* Memory usage for triples: ${Math.round(process.memoryUsage().rss / 1024 / 1024)}MB`);
2525

26-
TEST = '- Finding all ' + dimCubed + ' triples in the default graph ' + dimSquared * 1 + ' times (0 variables)';
26+
TEST = `- Finding all ${dimCubed} triples in the default graph ${dimSquared * 1} times (0 variables)`;
2727
console.time(TEST);
2828
for (i = 0; i < dim; i++)
2929
for (j = 0; j < dim; j++)
3030
for (k = 0; k < dim; k++)
3131
assert.equal(store.getQuads(prefix + i, prefix + j, prefix + k, '').length, 1);
3232
console.timeEnd(TEST);
3333

34-
TEST = '- Finding all ' + dimCubed + ' triples in the default graph ' + dimSquared * 2 + ' times (1 variable)';
34+
TEST = `- Finding all ${dimCubed} triples in the default graph ${dimSquared * 2} times (1 variable)`;
3535
console.time(TEST);
3636
for (i = 0; i < dim; i++)
3737
for (j = 0; j < dim; j++)
@@ -44,7 +44,7 @@ for (i = 0; i < dim; i++)
4444
assert.equal(store.getQuads(null, prefix + i, prefix + j, '').length, dim);
4545
console.timeEnd(TEST);
4646

47-
TEST = '- Finding all ' + dimCubed + ' triples in the default graph ' + dimSquared * 3 + ' times (2 variables)';
47+
TEST = `- Finding all ${dimCubed} triples in the default graph ${dimSquared * 3} times (2 variables)`;
4848
console.time(TEST);
4949
for (i = 0; i < dim; i++)
5050
assert.equal(store.getQuads(prefix + i, null, null, '').length, dimSquared);
@@ -63,7 +63,7 @@ dimCubed = dimSquared * dim;
6363
const dimQuads = dimCubed * dim;
6464

6565
store = new N3.Store();
66-
TEST = '- Adding ' + dimQuads + ' quads';
66+
TEST = `- Adding ${dimQuads} quads`;
6767
console.time(TEST);
6868
for (i = 0; i < dim; i++)
6969
for (j = 0; j < dim; j++)
@@ -72,9 +72,9 @@ for (i = 0; i < dim; i++)
7272
store.addQuad(prefix + i, prefix + j, prefix + k, prefix + l);
7373
console.timeEnd(TEST);
7474

75-
console.log('* Memory usage for quads: ' + Math.round(process.memoryUsage().rss / 1024 / 1024) + 'MB');
75+
console.log(`* Memory usage for quads: ${Math.round(process.memoryUsage().rss / 1024 / 1024)}MB`);
7676

77-
TEST = '- Finding all ' + dimQuads + ' quads ' + dimCubed * 4 + ' times';
77+
TEST = `- Finding all ${dimQuads} quads ${dimCubed * 4} times`;
7878
console.time(TEST);
7979
for (i = 0; i < dim; i++)
8080
assert.equal(store.getQuads(prefix + i, null, null, null).length, dimCubed);

src/IRIs.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,27 @@ const RDF = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
44

55
export default {
66
xsd: {
7-
decimal: XSD + 'decimal',
8-
boolean: XSD + 'boolean',
9-
double: XSD + 'double',
10-
integer: XSD + 'integer',
11-
string: XSD + 'string',
7+
decimal: `${XSD}decimal`,
8+
boolean: `${XSD}boolean`,
9+
double: `${XSD}double`,
10+
integer: `${XSD}integer`,
11+
string: `${XSD}string`,
1212
},
1313
rdf: {
14-
type: RDF + 'type',
15-
nil: RDF + 'nil',
16-
first: RDF + 'first',
17-
rest: RDF + 'rest',
18-
langString: RDF + 'langString',
14+
type: `${RDF}type`,
15+
nil: `${RDF}nil`,
16+
first: `${RDF}first`,
17+
rest: `${RDF}rest`,
18+
langString: `${RDF}langString`,
1919
},
2020
owl: {
2121
sameAs: 'http://www.w3.org/2002/07/owl#sameAs',
2222
},
2323
r: {
24-
forSome: SWAP + 'reify#forSome',
25-
forAll: SWAP + 'reify#forAll',
24+
forSome: `${SWAP}reify#forSome`,
25+
forAll: `${SWAP}reify#forAll`,
2626
},
2727
log: {
28-
implies: SWAP + 'log#implies',
28+
implies: `${SWAP}log#implies`,
2929
},
3030
};

src/N3DataFactory.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ export class Literal extends Term {
128128
// ## BlankNode constructor
129129
export class BlankNode extends Term {
130130
constructor(name) {
131-
super('_:' + name);
131+
super(`_:${name}`);
132132
}
133133

134134
// ### The term type of this term
@@ -144,7 +144,7 @@ export class BlankNode extends Term {
144144

145145
export class Variable extends Term {
146146
constructor(name) {
147-
super('?' + name);
147+
super(`?${name}`);
148148
}
149149

150150
// ### The term type of this term
@@ -234,12 +234,12 @@ export function termToId(term) {
234234
// Term instantiated with another library
235235
switch (term.termType) {
236236
case 'NamedNode': return term.value;
237-
case 'BlankNode': return '_:' + term.value;
238-
case 'Variable': return '?' + term.value;
237+
case 'BlankNode': return `_:${term.value}`;
238+
case 'Variable': return `?${term.value}`;
239239
case 'DefaultGraph': return '';
240-
case 'Literal': return '"' + term.value + '"' +
241-
(term.language ? '@' + term.language :
242-
(term.datatype && term.datatype.value !== xsd.string ? '^^' + term.datatype.value : ''));
240+
case 'Literal': return `"${term.value}"${
241+
term.language ? `@${term.language}` :
242+
(term.datatype && term.datatype.value !== xsd.string ? `^^${term.datatype.value}` : '')}`;
243243
case 'Quad':
244244
// To identify RDF* quad components, we escape quotes by doubling them.
245245
// This avoids the overhead of backslash parsing of Turtle-like syntaxes.
@@ -252,7 +252,7 @@ export function termToId(term) {
252252
}${
253253
(isDefaultGraph(term.graph)) ? '' : ` ${termToId(term.graph)}`
254254
}>>`;
255-
default: throw new Error('Unexpected termType: ' + term.termType);
255+
default: throw new Error(`Unexpected termType: ${term.termType}`);
256256
}
257257
}
258258

@@ -317,7 +317,7 @@ function blankNode(name) {
317317
function literal(value, languageOrDataType) {
318318
// Create a language-tagged string
319319
if (typeof languageOrDataType === 'string')
320-
return new Literal('"' + value + '"@' + languageOrDataType.toLowerCase());
320+
return new Literal(`"${value}"@${languageOrDataType.toLowerCase()}`);
321321

322322
// Automatically determine datatype for booleans and numbers
323323
let datatype = languageOrDataType ? languageOrDataType.value : '';
@@ -339,8 +339,8 @@ function literal(value, languageOrDataType) {
339339

340340
// Create a datatyped literal
341341
return (datatype === '' || datatype === xsd.string) ?
342-
new Literal('"' + value + '"') :
343-
new Literal('"' + value + '"^^' + datatype);
342+
new Literal(`"${value}"`) :
343+
new Literal(`"${value}"^^${datatype}`);
344344
}
345345

346346
// ### Creates a variable

src/N3Lexer.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ export default class N3Lexer {
163163
// we always need a non-dot character before deciding it is a blank node.
164164
// Therefore, try inserting a space if we're at the end of the input.
165165
if ((match = this._blank.exec(input)) ||
166-
inputFinished && (match = this._blank.exec(input + ' ')))
166+
inputFinished && (match = this._blank.exec(`${input} `)))
167167
type = 'blank', prefix = '_', value = match[1];
168168
break;
169169

@@ -241,7 +241,7 @@ export default class N3Lexer {
241241
// we always need a non-dot character before deciding it is a number.
242242
// Therefore, try inserting a space if we're at the end of the input.
243243
if (match = this._number.exec(input) ||
244-
inputFinished && (match = this._number.exec(input + ' '))) {
244+
inputFinished && (match = this._number.exec(`${input} `))) {
245245
type = 'literal', value = match[0];
246246
prefix = (typeof match[1] === 'string' ? xsd.double :
247247
(typeof match[2] === 'string' ? xsd.decimal : xsd.integer));
@@ -320,7 +320,7 @@ export default class N3Lexer {
320320
// we always need a non-dot character before deciding it is a prefixed name.
321321
// Therefore, try inserting a space if we're at the end of the input.
322322
else if ((match = this._prefixed.exec(input)) ||
323-
inputFinished && (match = this._prefixed.exec(input + ' ')))
323+
inputFinished && (match = this._prefixed.exec(`${input} `)))
324324
type = 'prefixed', prefix = match[1] || '', value = this._unescape(match[2]);
325325
}
326326

@@ -420,7 +420,7 @@ export default class N3Lexer {
420420
// ### `_syntaxError` creates a syntax error for the given issue
421421
_syntaxError(issue) {
422422
this._input = null;
423-
const err = new Error('Unexpected "' + issue + '" on line ' + this._line + '.');
423+
const err = new Error(`Unexpected "${issue}" on line ${this._line}.`);
424424
err.context = {
425425
token: undefined,
426426
line: this._line,

src/N3Parser.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export default class N3Parser {
8686
this._inversePredicate = false;
8787
// In N3, blank nodes are scoped to a formula
8888
// (using a dot as separator, as a blank node label cannot start with it)
89-
this._prefixes._ = (this._graph ? this._graph.id.substr(2) + '.' : '.');
89+
this._prefixes._ = (this._graph ? `${this._graph.id.substr(2)}.` : '.');
9090
// Quantifiers are scoped to a formula
9191
this._quantified = Object.create(this._quantified);
9292
}
@@ -160,7 +160,7 @@ export default class N3Parser {
160160
case 'prefixed':
161161
const prefix = this._prefixes[token.prefix];
162162
if (prefix === undefined)
163-
return this._error('Undefined prefix "' + token.prefix + ':"', token);
163+
return this._error(`Undefined prefix "${token.prefix}:"`, token);
164164
value = this._namedNode(prefix + token.value);
165165
break;
166166
// Read a blank node
@@ -173,7 +173,7 @@ export default class N3Parser {
173173
break;
174174
// Everything else is not an entity
175175
default:
176-
return this._error('Expected entity but got ' + token.type, token);
176+
return this._error(`Expected entity but got ${token.type}`, token);
177177
}
178178
// In N3 mode, replace the entity if it is quantified
179179
if (!quantifier && this._n3Mode && (value.id in this._quantified))
@@ -265,7 +265,7 @@ export default class N3Parser {
265265
case '}':
266266
// Expected predicate didn't come, must have been trailing semicolon
267267
if (this._predicate === null)
268-
return this._error('Unexpected ' + type, token);
268+
return this._error(`Unexpected ${type}`, token);
269269
this._subject = null;
270270
return type === ']' ? this._readBlankNodeTail(token) : this._readPunctuation(token);
271271
case ';':
@@ -338,7 +338,7 @@ export default class N3Parser {
338338
// ### `_readGraph` reads a graph
339339
_readGraph(token) {
340340
if (token.type !== '{')
341-
return this._error('Expected graph but got ' + token.type, token);
341+
return this._error(`Expected graph but got ${token.type}`, token);
342342
// The "subject" we read is actually the GRAPH's label
343343
this._graph = this._subject, this._subject = null;
344344
return this._readSubject;
@@ -604,7 +604,7 @@ export default class N3Parser {
604604
next = this._readQuadPunctuation;
605605
break;
606606
}
607-
return this._error('Expected punctuation to follow "' + this._object.id + '"', token);
607+
return this._error(`Expected punctuation to follow "${this._object.id}"`, token);
608608
}
609609
// A quad has been completed now, so return it
610610
if (subject !== null) {
@@ -630,7 +630,7 @@ export default class N3Parser {
630630
next = this._readObject;
631631
break;
632632
default:
633-
return this._error('Expected punctuation to follow "' + this._object.id + '"', token);
633+
return this._error(`Expected punctuation to follow "${this._object.id}"`, token);
634634
}
635635
// A quad has been completed now, so return it
636636
this._emit(this._subject, this._predicate, this._object, this._graph);
@@ -655,7 +655,7 @@ export default class N3Parser {
655655
// ### `_readPrefixIRI` reads the IRI of a prefix declaration
656656
_readPrefixIRI(token) {
657657
if (token.type !== 'IRI')
658-
return this._error('Expected IRI to follow prefix "' + this._prefix + ':"', token);
658+
return this._error(`Expected IRI to follow prefix "${this._prefix}:"`, token);
659659
const prefixNode = this._readEntity(token);
660660
this._prefixes[this._prefix] = prefixNode.value;
661661
this._prefixCallback(this._prefix, prefixNode);
@@ -715,7 +715,7 @@ export default class N3Parser {
715715
if ((entity = this._readEntity(token, true)) !== undefined)
716716
break;
717717
default:
718-
return this._error('Unexpected ' + token.type, token);
718+
return this._error(`Unexpected ${token.type}`, token);
719719
}
720720
// Without explicit quantifiers, map entities to a quantified entity
721721
if (!this._explicitQuantifiers)
@@ -825,7 +825,7 @@ export default class N3Parser {
825825
// An entity means this is a quad (only allowed if not already inside a graph)
826826
if (this._supportsQuads && this._graph === null && (this._graph = this._readEntity(token)) !== undefined)
827827
return this._readRDFStarTail;
828-
return this._error('Expected >> to follow "' + this._object.id + '"', token);
828+
return this._error(`Expected >> to follow "${this._object.id}"`, token);
829829
}
830830
return this._readRDFStarTail(token);
831831
}
@@ -875,7 +875,7 @@ export default class N3Parser {
875875

876876
// ### `_error` emits an error message through the callback
877877
_error(message, token) {
878-
const err = new Error(message + ' on line ' + token.line + '.');
878+
const err = new Error(`${message} on line ${token.line}.`);
879879
err.context = {
880880
token: token,
881881
line: token.line,
@@ -965,7 +965,7 @@ export default class N3Parser {
965965
result = result.substr(0, segmentStart);
966966
// Remove a trailing '/..' segment
967967
if (next !== '/')
968-
return result + '/' + iri.substr(i + 1);
968+
return `${result}/${iri.substr(i + 1)}`;
969969
segmentStart = i + 1;
970970
}
971971
}
@@ -986,7 +986,7 @@ export default class N3Parser {
986986
this._sparqlStyle = false;
987987
this._prefixes = Object.create(null);
988988
this._prefixes._ = this._blankNodePrefix ? this._blankNodePrefix.substr(2)
989-
: 'b' + blankNodePrefix++ + '_';
989+
: `b${blankNodePrefix++}_`;
990990
this._prefixCallback = prefixCallback || noop;
991991
this._inversePredicate = false;
992992
this._quantified = Object.create(null);

src/N3Store.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -678,13 +678,13 @@ export default class N3Store {
678678
let name, index;
679679
// Generate a name based on the suggested name
680680
if (suggestedName) {
681-
name = suggestedName = '_:' + suggestedName, index = 1;
681+
name = suggestedName = `_:${suggestedName}`, index = 1;
682682
while (this._ids[name])
683683
name = suggestedName + index++;
684684
}
685685
// Generate a generic blank node name
686686
else {
687-
do { name = '_:b' + this._blankNodeIndex++; }
687+
do { name = `_:b${this._blankNodeIndex++}`; }
688688
while (this._ids[name]);
689689
}
690690
// Add the blank node to the entities, avoiding the generation of duplicates

0 commit comments

Comments
 (0)