-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e0c156d
commit 2b04f4d
Showing
3 changed files
with
87 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
const logger = require('./logger'); | ||
|
||
// TODO: Better handle braces | ||
const aria = label => { | ||
const opening = label.indexOf('{'); | ||
if (opening === -1) { | ||
return `[aria-label^="${label}"]`; | ||
} else { | ||
if (opening !== 0) { | ||
return `[aria-label^="${label.slice(0, opening - 1)}"]`; | ||
} | ||
const closing = label.indexOf('}'); | ||
|
||
return `[aria-label$="${label.slice(closing, label.length)}"]`; | ||
} | ||
}; | ||
|
||
const compose = (label, params) => { | ||
let composition = label; | ||
params.forEach((param, index) => { | ||
composition = composition.replace(`{${index}}`, param); | ||
}); | ||
|
||
return composition; | ||
}; | ||
|
||
const getLabel = (element) => { | ||
if (element && element.label) return element.label; | ||
|
||
return element; | ||
}; | ||
|
||
const getParams = (element) => { | ||
if (element && element.params) return element.params; | ||
|
||
return []; | ||
}; | ||
|
||
const localize = (locale, element) => { | ||
const label = getLabel(element); | ||
const params = getParams(element); | ||
|
||
let localization; | ||
if (locale && locale[label]) { | ||
localization = compose(locale[label], params); | ||
} else { | ||
logger.error(`Missing label ${locale[label]}`); | ||
} | ||
|
||
return localization; | ||
}; | ||
|
||
module.exports = { | ||
get: (locale, element) => { | ||
const localization = localize(locale, element); | ||
const selector = aria(localization); | ||
|
||
return selector; | ||
}, | ||
localize, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters