Skip to content

Commit

Permalink
Use Map instead of lookup error in Song._parseInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanBurunkov committed Nov 16, 2024
1 parent db6882e commit 614df49
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ tm-node-mpd
This is fork of [node-mpd](https://github.com/Prior99/node-mpd) project by Frederick Gnodtke.

tm-node-mpd is a library for simple communicating with a [music player daemon](http://www.musicpd.org/).
It uses a IPC and TCP-socket to communicate with the daemon and provides a list of highlevel promise based methods.
It can use an IPC or TCP-socket to communicate with the daemon and provides a list of highlevel async methods.

Make sure to take a look at the [examples](https://github.com/RomanBurunkov/tm-node-mpd/tree/master/examples).

Note that 0.2 branch was experimental. Do not use is it at all.
Version 1.0.0 has been updated with latest development dependencies and migrated to ECMAScript Modules.

Available options
------
Expand Down
28 changes: 14 additions & 14 deletions src/song.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ import { parseKvp } from './protocol';
const RES_OK = 'OK';
const ERR_MSG_UNKNOWN = 'Unknown response while parsing song.';

const FIELD_MAP = [
{ key: 'file', val: 'file' },
{ key: 'time', val: 'Time' },
{ key: 'date', val: 'Date' },
{ key: 'genre', val: 'Genre' },
{ key: 'title', val: 'Title' },
{ key: 'album', val: 'Album' },
{ key: 'track', val: 'Track' },
{ key: 'artist', val: 'Artist' },
{ key: 'lastModified', val: 'Last-Modified' }
];
const FIELDS_MAP = new Map([
['file', 'file'],
['Time', 'time'],
['Date', 'date'],
['Genre', 'genre'],
['Title', 'title'],
['Album', 'album'],
['Track', 'track'],
['Artist', 'artist'],
['Last-Modified', 'lastModified'],
]);

export default class Song {
constructor(data) {
Expand All @@ -40,9 +40,9 @@ export default class Song {
.forEach((itm) => {
const kvp = parseKvp(itm);
if (!kvp) throw new Error(ERR_MSG_UNKNOWN);
const fieldInfo = FIELD_MAP.find(fldKvp => fldKvp.val === kvp.key);
if (!fieldInfo) return;
info[fieldInfo.key] = kvp.val;
const infoKey = FIELDS_MAP.get(kvp.key);
if (!infoKey) return;
info[infoKey] = kvp.val;
});
return info;
}
Expand Down

0 comments on commit 614df49

Please sign in to comment.