Skip to content

Commit a3be151

Browse files
committed
chore: rename registerAlias to registerAliases
Plural form is clearly better as it's not surprising to the reader to see it being passed an array - where as the singular form might have been. Meanwhile it's also easy to assume that it also supports arrays of any size - including an array with a singular alias. The fact that it can magically accept a string as the first argument might not be obvious, but we document it and even if one didn't know this they could still use the array form of the API without any issue by passing a one item array.
1 parent 527086f commit a3be151

File tree

5 files changed

+13
-13
lines changed

5 files changed

+13
-13
lines changed

CHANGES.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Parser Engine:
44

55
- (parser) Adds `keywords.$pattern` key to grammar definitions (#2519) [Josh Goebel][]
66
- (parser) Adds SHEBANG utility mode [Josh Goebel][]
7-
- (parser) Adds `registerAlias` method (#2540) [Taufik Nurrohman][]
7+
- (parser) Adds `registerAliases` method (#2540) [Taufik Nurrohman][]
88
- (enh) Added `on:begin` callback for modes (#2261) [Josh Goebel][]
99
- (enh) Added `on:end` callback for modes (#2261) [Josh Goebel][]
1010
- (enh) Added ability to programatically ignore begin and end matches (#2261) [Josh Goebel][]

docs/api.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ Adds new language to the library under the specified name. Used mostly internall
112112
to use common regular expressions defined within it.
113113

114114

115-
``registerAlias(alias|aliases, {languageName})``
116-
------------------------------------------------
115+
``registerAliases(alias|aliases, {languageName})``
116+
--------------------------------------------------
117117

118118
Adds new language alias or aliases to the library for the specified language name defined under ``languageName`` key.
119119

src/highlight.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ const HLJS = function(hljs) {
658658
lang.rawDefinition = language.bind(null, hljs);
659659

660660
if (lang.aliases) {
661-
lang.aliases.forEach(function(alias) { aliases[alias] = name; });
661+
registerAliases(lang.aliases, { languageName: name });
662662
}
663663
}
664664

@@ -685,12 +685,11 @@ const HLJS = function(hljs) {
685685
return languages[name] || languages[aliases[name]];
686686
}
687687

688-
function registerAlias(alias, {languageName}) {
689-
let list = alias;
690-
if (typeof list === 'string') {
691-
list = [alias]
688+
function registerAliases(aliasList, {languageName}) {
689+
if (typeof aliasList === 'string') {
690+
aliasList = [aliasList]
692691
}
693-
list.forEach(alias => aliases[alias] = languageName);
692+
aliasList.forEach(alias => aliases[alias] = languageName);
694693
}
695694

696695
function autoDetection(name) {
@@ -724,7 +723,7 @@ const HLJS = function(hljs) {
724723
registerLanguage,
725724
listLanguages,
726725
getLanguage,
727-
registerAlias,
726+
registerAliases,
728727
requireLanguage,
729728
autoDetection,
730729
inherit,

test/api/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ describe('hljs', function() {
1212
require('./highlight');
1313
require('./fixmarkup');
1414
require('./keywords');
15+
require('./registerAlias');
1516
});

test/api/registerAlias.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
const hljs = require('../../build');
44
const should = require('should');
55

6-
describe('.registerAlias()', () => {
6+
describe('.registerAliases()', () => {
77
it('should get an existing language by alias', () => {
8-
hljs.registerAlias('jquery', {
8+
hljs.registerAliases('jquery', {
99
languageName: 'javascript'
1010
});
1111
const result = hljs.getLanguage('jquery');
@@ -14,7 +14,7 @@ describe('.registerAlias()', () => {
1414
});
1515

1616
it('should get an existing language by aliases', () => {
17-
hljs.registerAlias(['jquery', 'jqueryui'], {
17+
hljs.registerAliases(['jquery', 'jqueryui'], {
1818
languageName: 'javascript'
1919
});
2020
const result = hljs.getLanguage('jquery');

0 commit comments

Comments
 (0)