Re: `String.prototype.symbolAt()` (improved `String.prototype.charAt()`)

2013-10-20 Thread Mathias Bynens
On 19 Oct 2013, at 12:54, Domenic Denicola dome...@domenicdenicola.com wrote:

 My proposed cowpaths:
 
 ```js
 Object.mixin(String.prototype, {
  realCharacterAt(i) {
let index = 0;
for (var c of this) {
  if (index++ === i) {
return c;
  }
}
  }
  get realLength() {
let counter = 0;
for (var c of this) {
  ++counter;
}
return counter;
  }
 });
 ```

Good stuff!

To account for [lookalike symbols due to combining marks] [1], just add a call 
to `String.prototype.normalize`:

Object.mixin(String.prototype, {
  get realLength() {
let counter = 0;
for (var c of this.normalize('NFC')) {
  ++counter;
}
return counter;
  }
});

assert('ma\xF1ana'.realLength == 'man\u0303ana'.realLength);

[1]: http://mathiasbynens.be/notes/javascript-unicode#accounting-for-lookalikes

___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Refinements.

2013-10-20 Thread Jonathan Barronville
Hi everyone.

I have a proposal for ES6. I don't know if it's been discussed before, so if it 
has, I don't mean to waste your time … hopefully, I'll be directed to the right 
place.

One of the most debated JavaScript topics (at least in my experience) is 
whether or not one should modify objects that don't belong to them ... some say 
it's okay if you know what you're doing and others see it as a crime.

I propose we solve this problem by adding refinements to ES6. Ruby v2.0.0 
introduced a nice little feature dubbed refinements. Refinements allow you to 
extend other objects for specific modules of code. I know this isn't a Ruby 
mailing list, but because the inspiration comes from the Ruby implementation, 
an example is in order (feel free to ignore it).

Say you want to modify the `to_str()` method in the built-in String class in 
Ruby, the common way to do this is as follows.

```ruby
class String
  def to_str()
    Blah, blah, blah ... this is an example.
  end
end
```

Very simple ... just re-open the class and create a method with the same name. 
However, there's a problem with this, which happens to be the same problem we 
have in JavaScript, and that's the fact that we've now just modified this 
method permanently for the rest of the execution.

Well, with refinements, the better way of accomplishing the same thing is as 
follows.

```ruby
module MyLibrary
  refine(String) do
    def to_str()
      Blah, blah, blah ... this is an example.
    end
  end
end
```

Now, if you try to send `to_str()` to, say, a string object `hello` 
(`hello.to_str()`), you'll get the original `hello` as a return value. In 
order to use the refinements made to the class, you have to do the following.

```ruby
using MyLibrary

puts(hello.to_str())
```

Running the code above properly outputs the string `Blah, blah, blah ... this 
is an example.`.

Refinements can only be used after a `using` statement and only within that 
file.

Given that we're already getting modules in ES6, I believe something similar to 
this would be a great addition to the language.

I can definitely work on a more in-depth and detailed proposal, with specific 
JavaScript examples, if needed, but I just would like to hear thoughts around 
the idea.

Thanks!

- Jonathan Barronville
@jonathanmarvens
___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss