So trick to reference exported enum?

So trick to reference exported enum? How do you reference an exported enum?

const { LGTV } = require('lgtv-ip-control');
const TV = new LGTV('x.x.x.x', 'xx:xx:xx:xx:xx:xx', XXXXXXXX');

Object.keys(Keys).forEach(key => console.log("Key:", key))

The module I am using works... loads, I can execute various functions in the TV, i.e. LGTV class instance (i.e. object). But if I try to reference an exported enum? I think my limited Javascript knowledge is letting me down here?

Object.keys(Keys).forEach(key => console.log("Key:", key))
            ^
ReferenceError: Keys is not defined
    at Object.<anonymous> (/root/LGTV-Keys.js:4:13)
    at Module._compile (node:internal/modules/cjs/loader:1356:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1414:10)
    at Module.load (node:internal/modules/cjs/loader:1197:32)
    at Module._load (node:internal/modules/cjs/loader:1013:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:128:12)
    at node:internal/main/run_main_module:28:49

Here is the github link to the node module I am using... some day would love to create NR palette wrapper for this https://github.com/WesSouza/lgtv-ip-control/tree/main

What have you done to try and create Keys, I can't see it in the code you've shared. So not really possible to help without that.

Did you mean to do Object.keys(TV).forEach(key => console.log("Key:", key))?

Because Keys are not imported:

const { LGTV, Keys } = require('lgtv-ip-control');
const TV = new LGTV('x.x.x.x', 'xx:xx:xx:xx:xx:xx', 'XXXXXXXX');

Object.keys(Keys).forEach(key => console.log("Key:", key));

I don't really know if you should use Object.keys or Object.values for an enum :thinking:

Fun Fact:
JavaScript doesn't have enums, I mean you can "pretend" and disguise them as enums using
typescript - but during runtime - they are just an object with key:value pairs.

Anyway, as your were

Yes, I HATE TypeScript. :slight_smile:

You beat me to the draw Marcus :grinning:

So it should indeed be Object.keys to get all the keys of the Keys and Object.values to get all of the values of the Keys - and that isn't confusing at all is it! :rofl:

1 Like

If you look at the code reference information in github the following is defined...

export enum Keys {
  arrowDown = 'arrowdown',
  arrowLeft = 'arrowleft',
  arrowRight = 'arrowright',
  arrowUp = 'arrowup',
  aspectRatio = 'aspectratio',
  audioMode = 'audiomode',
  back = 'returnback',
  blueButton = 'bluebutton',
  captionSubtitle = 'captionsubtitle',
  channelDown = 'channeldown',
  channelList = 'channellist',
  channelUp = 'channelup',
  deviceInput = 'deviceinput',
  energySaving = 'screenbright',
  fastForward = 'fastforward',
  greenButton = 'greenbutton',
  home = 'myapp',
  info = 'programminfo',
  liveTV = 'livetv',
  menu = 'settingmenu',
  number0 = 'number0',
  number1 = 'number1',
  number2 = 'number2',
  number3 = 'number3',
  number4 = 'number4',
  number5 = 'number5',
  number6 = 'number6',
  number7 = 'number7',
  number8 = 'number8',
  number9 = 'number9',
  ok = 'ok',
  play = 'play',
  previousChannel = 'previouschannel',
  programGuide = 'programguide',
  record = 'record',
  redButton = 'redbutton',
  rewind = 'rewind',
  sleepTimer = 'sleepreserve',
  userGuide = 'userguide',
  videoMode = 'videomode',
  volumeDown = 'volumedown',
  volumeMute = 'volumemute',
  volumeUp = 'volumeup',
  yellowButton = 'yellowbutton',
}

So that is not an exported enum 'Keys'?

No. This is a TypeScript construct - which is translated into Javascript during the build process.
It is as @marcus-j-davies explained:

When you look into your ./dist/constants directory, you'll find in TV.js the result of that build process as

var Keys;
(function(Keys2) {
  Keys2["arrowDown"] = "arrowdown";
  Keys2["arrowLeft"] = "arrowleft";
  Keys2["arrowRight"] = "arrowright";
  Keys2["arrowUp"] = "arrowup";
  Keys2["aspectRatio"] = "aspectratio";
  Keys2["audioMode"] = "audiomode";
  Keys2["back"] = "returnback";
  Keys2["blueButton"] = "bluebutton";
  Keys2["captionSubtitle"] = "captionsubtitle";
  Keys2["channelDown"] = "channeldown";
  Keys2["channelList"] = "channellist";
  Keys2["channelUp"] = "channelup";
  Keys2["deviceInput"] = "deviceinput";
  Keys2["energySaving"] = "screenbright";
  Keys2["fastForward"] = "fastforward";
  Keys2["greenButton"] = "greenbutton";
  Keys2["home"] = "myapp";
  Keys2["info"] = "programminfo";
  Keys2["liveTV"] = "livetv";
  Keys2["menu"] = "settingmenu";
  Keys2["number0"] = "number0";
  Keys2["number1"] = "number1";
  Keys2["number2"] = "number2";
  Keys2["number3"] = "number3";
  Keys2["number4"] = "number4";
  Keys2["number5"] = "number5";
  Keys2["number6"] = "number6";
  Keys2["number7"] = "number7";
  Keys2["number8"] = "number8";
  Keys2["number9"] = "number9";
  Keys2["ok"] = "ok";
  Keys2["play"] = "play";
  Keys2["previousChannel"] = "previouschannel";
  Keys2["programGuide"] = "programguide";
  Keys2["record"] = "record";
  Keys2["redButton"] = "redbutton";
  Keys2["rewind"] = "rewind";
  Keys2["sleepTimer"] = "sleepreserve";
  Keys2["userGuide"] = "userguide";
  Keys2["videoMode"] = "videomode";
  Keys2["volumeDown"] = "volumedown";
  Keys2["volumeMute"] = "volumemute";
  Keys2["volumeUp"] = "volumeup";
  Keys2["yellowButton"] = "yellowbutton";
})(Keys || (Keys = {}));

which in essence is the definition sequence of a Javascript object.

2 Likes

So @Nodi.Rubrum, is it good?

The reason we don't know if you should use Object.keys or Object.values is that it depends on what you want to do:

  • key: is the label, a friendlier name
  • value: is the value the code expects

As Marcus and Ralph said an enum is a TypeScript construct that is translated into an object in javascript. But it's used the same way:

const cmd = Keys.back;
// Expected: returnback

TV.sendKey(cmd);
...

Interesting, clearly the code suggests 'exported' enum. And I am or was thinking of enums comparable C++ or C#, where defined types are reference-able when library/module is loaded. And, yes, I wanted to reference value by key name. Could not understand by Key.back did not result in 'returnback' value when executing code via node (CLI). Javascript, or typescript, not as familiar with it as other scripting engines, say python, php or even powershell.

I am a c# developer by trade - hence I also had to get my head around this enum / not enum thing when I started my venture into typescript

And not forgetting that Typescript and C# are Microsoft inventions. Typescript was invented to make JavaScript look like C# to try to lock more developers into the Microsoft world. (my cynical view) Hence why it has things that look like C# :grinning:

I don't like it because it is yet another abstraction from JavaScript and will always end up with edge-cases and issues. Might as well invest the time in getting properly to grips with JavaScript in my view.

1 Like

Partially agree.

If a project is not going to involve other contributions, and you’re the only developer - I would indeed not consider typescript.

But when a project is subject to various contributions I think typescript is very strong here, as during design time, errors are highlighted, types are checked for the contributor who may not know the code base etc etc.

I think coming from c# where types are strict, I do see (and use) typescript and its benefits.

But as said, if I was the only developer in a js project, I would not use typescript

1 Like

Did I mention how much I hate C/C++/C# - I may be a little biased. :rofl:

2 Likes

I did quite a bit of C development a long long time ago... there was an explicit elegance to the power of C. But you had to be disciplined and explicit in design and code implementation of course. No variant of C is forgiving.

Javascript is giving me fits... but I am learning. I some times miss the simplistic early VB and HyperCard (Yes HyperCard, if you know what that was?).

I have yet to significantly tackled async python methods or async javascript methods, but I see that in the distance!

Reminds me of something a developer manager at a fortune 10 firm said, "Why does everyone [in my team] have a hard-on for Java!" That was said in 1996 if I recall right... Wow, if only he had known where Java was going to go, lead, etc.

1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.