-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
13.1. utils module - functions working with assets
- Loading branch information
Showing
3 changed files
with
59 additions
and
55 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
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,50 @@ | ||
export const getAudioFileName = (keyContent, shiftKey) => { | ||
const { main, mainName, shifted, shiftedName, code } = keyContent | ||
|
||
let fileName | ||
|
||
if (shiftKey) { | ||
// will be returned 1 of 3 values (if it exist). priority to the first one | ||
fileName = shiftedName || shifted || code | ||
} else { | ||
fileName = mainName || main || code | ||
} | ||
|
||
// to have a guarantee, that everything is written in the same (lower) case | ||
return fileName.toLowerCase() | ||
} | ||
|
||
export const getKeyLabels = keyContent => { | ||
/* | ||
Code line below is called `destructuring`, | ||
because we destructure an object `this.keyContent` | ||
into 3 separate constants. | ||
*/ | ||
const { main = '', shifted = '', label, code } = keyContent | ||
const isUpperCaseLang = main.toUpperCase() === shifted | ||
const mainOutput = isUpperCaseLang ? shifted : main | ||
const shiftedOutput = isUpperCaseLang ? '' : shifted | ||
|
||
return { | ||
/* | ||
|| is a logical `or` operator. | ||
The line below is `or chain`. | ||
If `label` exists, it will be returned. | ||
If `label` doesn't exist, but `main` exists -- will be returned `main`. | ||
If `label` and main don't exist, will be returned `code`. | ||
*/ | ||
main: label || mainOutput || code, | ||
shifted: shiftedOutput | ||
} | ||
} | ||
|
||
export const loadKeyboardData = async lang => { | ||
const { default: keyboardData } = await import(`../keyboardData/${lang}.js`) | ||
return keyboardData | ||
} | ||
|
||
export const playKeyAudio = (lang, keyContent, shiftKey) => { | ||
const fileName = getAudioFileName(keyContent, shiftKey) | ||
const audio = new Audio(`../keyboardData/${lang}/${fileName}.mp3`) | ||
return audio.play() | ||
} |