diff options
author | Samy Pessé <samypesse@gmail.com> | 2016-01-27 22:51:07 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2016-01-27 22:51:07 +0100 |
commit | 36f2f5d52e54c03036f05225df3f58e9833f3e71 (patch) | |
tree | 67fe034938ba51a09fcaf6a4bf134631be09f20b /lib/backbone | |
parent | ddc99104d4394cc21eaa615b01d73ec001ac7a16 (diff) | |
download | gitbook-36f2f5d52e54c03036f05225df3f58e9833f3e71.zip gitbook-36f2f5d52e54c03036f05225df3f58e9833f3e71.tar.gz gitbook-36f2f5d52e54c03036f05225df3f58e9833f3e71.tar.bz2 |
Add method glossary.find
Diffstat (limited to 'lib/backbone')
-rw-r--r-- | lib/backbone/glossary.js | 19 |
1 files changed, 15 insertions, 4 deletions
diff --git a/lib/backbone/glossary.js b/lib/backbone/glossary.js index 0f5e567..22c4d42 100644 --- a/lib/backbone/glossary.js +++ b/lib/backbone/glossary.js @@ -2,6 +2,15 @@ var _ = require('lodash'); var util = require('util'); var BackboneFile = require('./file'); +// Normalize a glossary entry name into a unique id +function nameToId(name) { + return name.toLowerCase() + .replace(/[\/\\\?\%\*\:\;\|\"\'\\<\\>\#\$\(\)\!\.\@]/g, '') + .replace(/ /g, '_') + .trim(); +} + + /* A glossary entry is represented by a name and a short description An unique id for the entry is generated using its name @@ -19,10 +28,7 @@ function GlossaryEntry(name, description) { // Normalizes a glossary entry's name to create an ID GlossaryEntry.prototype.getId = function() { - return this.name.toLowerCase() - .replace(/[\/\\\?\%\*\:\;\|\"\'\\<\\>\#\$\(\)\!\.\@]/g, '') - .replace(/ /g, '_') - .trim(); + return nameToId(this.name); }; @@ -54,6 +60,11 @@ Glossary.prototype.get = function(id) { }); }; +// Find an entry by its name +Glossary.prototype.find = function(name) { + return this.get(nameToId(name)); +}; + // Return false if glossary has entries (and exists) Glossary.prototype.isEmpty = function(id) { return _.size(this.entries) === 0; |