Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iterate object properties #606

Open
biensurerodezee opened this issue Feb 13, 2025 · 1 comment
Open

iterate object properties #606

biensurerodezee opened this issue Feb 13, 2025 · 1 comment

Comments

@biensurerodezee
Copy link

I tried something that was not documented: "Iterate over an object"
code:

    <script type="text/hyperscript">
      init
        set $o to { name: "john", age: 30 }
        for value in $o index key
          log key
          log value
        end
      end
    </script>

But I could not get it working. Trying multiple methods, most of them resulted into an error.
console:

Uncaught TypeError: i.iterator is undefined
    op https://unpkg.com/hyperscript.org:1
    unifiedEval https://unpkg.com/hyperscript.org:1
    unifiedExec https://unpkg.com/hyperscript.org:1
    execute https://unpkg.com/hyperscript.org:1
    o https://unpkg.com/hyperscript.org:1
    setTimeout handler*install https://unpkg.com/hyperscript.org:1
    apply https://unpkg.com/hyperscript.org:1
    initElement https://unpkg.com/hyperscript.org:1
    processNode https://unpkg.com/hyperscript.org:1
    forEach https://unpkg.com/hyperscript.org:1
    processNode https://unpkg.com/hyperscript.org:1
    w https://unpkg.com/hyperscript.org:1
    n https://unpkg.com/hyperscript.org:1
    w https://unpkg.com/hyperscript.org:1
    promise callback*w https://unpkg.com/hyperscript.org:1
    <anonymous> https://unpkg.com/hyperscript.org:1
    <anonymous> https://unpkg.com/hyperscript.org:1

Since this works in normal javascript I think there must be a way to do this in hyperscript.
Please let me know how to do this.

@biensurerodezee
Copy link
Author

I wrote some code that works with hyperscript:

		  _hyperscript.addLeafExpression("keys", function (parser, runtime, tokens) {
			  if (!tokens.matchToken("keys")) return;
                          return getObjectKeysFunction(parser, runtime, tokens);
		  });
		  
		  _hyperscript.addLeafExpression("properties", function (parser, runtime, tokens) {
			  if (!tokens.matchToken("properties")) return;
                          return getObjectKeysFunction(parser, runtime, tokens);
		  });
		  
		  var getObjectKeysFunction = function(parser, runtime, tokens) {
		          tokens.matchAnyToken("of", "in", "from"); // optional "of", "in", "from"
			  var arg = parser.requireElement("expression", tokens);
			  return {
				  args: [arg],
                                  op: function (context, arg) {
                                             r = [];
                                             for (let key in arg) {
                                                 r.push(key);
                                             } 
                                             return r; 
                                  },
				  evaluate: function (context) {
					  return runtime.unifiedEval(this, context);
				  }
			  };
		  };
  • I use this simply by adding this in a <script stype="text/hyperscript"> tag

to test it:

<script type="text/hyperscript">
    init
        set o to { name: "jerami", age: 28, location: { street: "coastlane", number: 1 } }

        log keys o
        log keys of o
        log keys in o
        log keys from o

        log properties o
        log properties of o
        log properties in o
        log properties from o
        
        set a to ["hi", "you"]
        log keys a
        
        set v to "something"
        log keys v
        
        set n to 5000
        log keys n
    end
</script>
  • this works fine for me but I would like to add this to your language for the community. If you like me doing that, can you help me where to place these lines in the project?

Is adding it to _hyperscript.js enough?

Please let me know

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant