Skip to content

Commit

Permalink
chore: rename registerAlias to registerAliases
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
joshgoebel committed May 8, 2020
1 parent 527086f commit a3be151
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 13 deletions.
2 changes: 1 addition & 1 deletion CHANGES.md
Expand Up @@ -4,7 +4,7 @@ Parser Engine:

- (parser) Adds `keywords.$pattern` key to grammar definitions (#2519) [Josh Goebel][]
- (parser) Adds SHEBANG utility mode [Josh Goebel][]
- (parser) Adds `registerAlias` method (#2540) [Taufik Nurrohman][]
- (parser) Adds `registerAliases` method (#2540) [Taufik Nurrohman][]
- (enh) Added `on:begin` callback for modes (#2261) [Josh Goebel][]
- (enh) Added `on:end` callback for modes (#2261) [Josh Goebel][]
- (enh) Added ability to programatically ignore begin and end matches (#2261) [Josh Goebel][]
Expand Down
4 changes: 2 additions & 2 deletions docs/api.rst
Expand Up @@ -112,8 +112,8 @@ Adds new language to the library under the specified name. Used mostly internall
to use common regular expressions defined within it.


``registerAlias(alias|aliases, {languageName})``
------------------------------------------------
``registerAliases(alias|aliases, {languageName})``
--------------------------------------------------

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

Expand Down
13 changes: 6 additions & 7 deletions src/highlight.js
Expand Up @@ -658,7 +658,7 @@ const HLJS = function(hljs) {
lang.rawDefinition = language.bind(null, hljs);

if (lang.aliases) {
lang.aliases.forEach(function(alias) { aliases[alias] = name; });
registerAliases(lang.aliases, { languageName: name });
}
}

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

function registerAlias(alias, {languageName}) {
let list = alias;
if (typeof list === 'string') {
list = [alias]
function registerAliases(aliasList, {languageName}) {
if (typeof aliasList === 'string') {
aliasList = [aliasList]
}
list.forEach(alias => aliases[alias] = languageName);
aliasList.forEach(alias => aliases[alias] = languageName);
}

function autoDetection(name) {
Expand Down Expand Up @@ -724,7 +723,7 @@ const HLJS = function(hljs) {
registerLanguage,
listLanguages,
getLanguage,
registerAlias,
registerAliases,
requireLanguage,
autoDetection,
inherit,
Expand Down
1 change: 1 addition & 0 deletions test/api/index.js
Expand Up @@ -12,4 +12,5 @@ describe('hljs', function() {
require('./highlight');
require('./fixmarkup');
require('./keywords');
require('./registerAlias');
});
6 changes: 3 additions & 3 deletions test/api/registerAlias.js
Expand Up @@ -3,9 +3,9 @@
const hljs = require('../../build');
const should = require('should');

describe('.registerAlias()', () => {
describe('.registerAliases()', () => {
it('should get an existing language by alias', () => {
hljs.registerAlias('jquery', {
hljs.registerAliases('jquery', {
languageName: 'javascript'
});
const result = hljs.getLanguage('jquery');
Expand All @@ -14,7 +14,7 @@ describe('.registerAlias()', () => {
});

it('should get an existing language by aliases', () => {
hljs.registerAlias(['jquery', 'jqueryui'], {
hljs.registerAliases(['jquery', 'jqueryui'], {
languageName: 'javascript'
});
const result = hljs.getLanguage('jquery');
Expand Down

0 comments on commit a3be151

Please sign in to comment.