Proposed API change for match.

4 views
Skip to first unread message

Jared W

unread,
May 3, 2011, 1:59:42 PM5/3/11
to JSShaper
https://github.com/jaredwy/jsshaper/commit/2edc9b18630088e34ed0f482575adb592fc782f6#src/shaper.js

Here is a small proof of concept for one thing i was looking at. It is
no way final, just threw it together for the purpose of this example.

It allows me now to write code like
if(Shaper.match(IdTemplate,node,function(template,node) {
return node.value != "document";
},true))

Or would allow further traversal of the node to allow sort of
lookahead matching i suppose. I started down the path of using a regex
instead, but found it limited as i could only match on value.



Part two of the proposed change is to change the match character from
"$" as i discovered this makes it impossible to write a plugin that
turns

var myScope = (function($) { })($); into something that does var
myScope = (function($) { })(jQuery);

Thoughts, oppositions?

Jared.

Olov Lassus

unread,
May 3, 2011, 2:35:52 PM5/3/11
to jssh...@googlegroups.com
3 maj 2011 kl. 19.59 skrev Jared W:

> https://github.com/jaredwy/jsshaper/commit/2edc9b18630088e34ed0f482575adb592fc782f6#src/shaper.js
>
> Here is a small proof of concept for one thing i was looking at. It is
> no way final, just threw it together for the purpose of this example.

Welcome to the group and thanks for writing up your proposal.

> It allows me now to write code like
> if(Shaper.match(IdTemplate,node,function(template,node) {
> return node.value != "document";
> },true))

Currently Shaper.match takes two nodes (the template and the source) and returns whether they are the same (they match) or not. Optionally, it supports wildcard-matching using $ and $$ in the template. It's used to match source declaratively instead of manually inspecting node properties (.type, .value) recursively.

To really understand the use-case you're proposing I'd like to see the template and an example of the source. Seems that you're proposing a way of conditionally matching wildcards? I can see how that could be useful.

I'm not sure about the callback API, though. The template could contain many (conditional) wildcards and it isn't certain that you always want to apply the same extra conditional test to all of them.

Instead, Shaper.match could simply return an array with the nodes matching the wildcards (where a $$ identifier would result in a nested array). This result would still be truthy so the simple boolean usage of Shaper.match would remain the same, but more advanced usages would be permitted easily. That API is also more consistent with Shaper.replace.

> Part two of the proposed change is to change the match character from
> "$" as i discovered this makes it impossible to write a plugin that
> turns
>
> var myScope = (function($) { })($); into something that does var
> myScope = (function($) { })(jQuery);

Absolutely. Both Shaper.match and Shaper.replace should be able to take an optional argument specifying the custom identifiers. Default is that $ matches any node (one), $$ matches a list of nodes.


/Olov

Jared W

unread,
May 3, 2011, 2:50:21 PM5/3/11
to jssh...@googlegroups.com
On Tue, May 3, 2011 at 11:35 AM, Olov Lassus <olov....@gmail.com> wrote:
3 maj 2011 kl. 19.59 skrev Jared W:

> https://github.com/jaredwy/jsshaper/commit/2edc9b18630088e34ed0f482575adb592fc782f6#src/shaper.js
>
> Here is a small proof of concept for one thing i was looking at. It is
> no way final, just threw it together for the purpose of this example.

Welcome to the group and thanks for writing up your proposal.

> It allows me now to write code like
> if(Shaper.match(IdTemplate,node,function(template,node) {
>     return node.value != "document";
> },true))

Currently Shaper.match takes two nodes (the template and the source) and returns whether they are the same (they match) or not. Optionally, it supports wildcard-matching using $ and $$ in the template. It's used to match source declaratively instead of manually inspecting node properties (.type, .value) recursively.

To really understand the use-case you're proposing I'd like to see the template and an example of the source. Seems that you're proposing a way of conditionally matching wildcards? I can see how that could be useful.


Basically i am doing some manipulation of some code that is rather dom heavy. I am needing to match "$.getElementById" although i do not want to match if it is querying from the document just on a element. 

So currently $ will match a.getElementById and document.getElementById, ideally i would like an easy way to do this inside jsshaper. Your array idea would work very well as well. Will take a look at implementing that. 

 
I'm not sure about the callback API, though. The template could contain many (conditional) wildcards and it isn't certain that you always want to apply the same extra conditional test to all of them.

Instead, Shaper.match could simply return an array with the nodes matching the wildcards (where a $$ identifier would result in a nested array). This result would still be truthy so the simple boolean usage of Shaper.match would remain the same, but more advanced usages would be permitted easily. That API is also more consistent with Shaper.replace.

> Part two of the proposed change is to change the match character from
> "$" as i discovered this makes it impossible to write a plugin that
> turns
>
> var myScope = (function($) { })($); into something that does var
> myScope = (function($) { })(jQuery);

Absolutely. Both Shaper.match and Shaper.replace should be able to take an optional argument specifying the custom identifiers. Default is that $ matches any node (one), $$ matches a list of nodes.
Yup but if that node is named $ we run into an issue heh. So thinking of either changing the wildcard or having a way to optionally turn of wildcard patterns, although you then run into an issue if you want to do something like $/*wildcard*/.something($/*this is a reference to jquery*/

So some more thought will be required before i start hacking a solution.
Hopefully this makes sense, have had very little sleep :)

Jared.
 


/Olov


Olov Lassus

unread,
May 3, 2011, 3:27:43 PM5/3/11
to jssh...@googlegroups.com
3 maj 2011 kl. 20.50 skrev Jared W:

Basically i am doing some manipulation of some code that is rather dom heavy. I am needing to match "$.getElementById" although i do not want to match if it is querying from the document just on a element. 
So currently $ will match a.getElementById and document.getElementById, ideally i would like an easy way to do this inside jsshaper. Your array idea would work very well as well. Will take a look at implementing that. 

The example helped. Let's shoot for the array idea then and refactor it if needed.

Absolutely. Both Shaper.match and Shaper.replace should be able to take an optional argument specifying the custom identifiers. Default is that $ matches any node (one), $$ matches a list of nodes.
Yup but if that node is named $ we run into an issue heh. So thinking of either changing the wildcard or having a way to optionally turn of wildcard patterns, although you then run into an issue if you want to do something like $/*wildcard*/.something($/*this is a reference to jquery*/

If you want $ in your template to match the identifier (not a wildcard), then providing an argument such as {wildcard: '_$', wildcard-many: '_$$'} would solve that without issues, wouldn't it? But $ is starting to sound like a poor default. I'll give this some thought.

At one time I modified the Narcissus parser so that it supported # terminal tokens and used that for wildcards. I later changed that since I preferred staying forward compatible (# may be on the table for Harmony).

/Olov

Reply all
Reply to author
Forward
0 new messages