Git Product home page Git Product logo

Comments (3)

mbostock avatar mbostock commented on July 28, 2024

Consistency with the standard is good, but there are complicating factors here, namely that d3.map is not a shim for ES6 Map because it’s not really possible to shim ES6 Map. You can get a lot closer than d3.map, but I taking it as far as you can would be counterproductive. Some differences:

  • D3’s map coerces keys to strings, whereas ES6 Maps can use any object.
  • D3 calls it map.remove, whereas ES6 says map.delete.
  • D3 lacks a map.clear method.
  • D3’s map is not iterable. (There’s not really a way to shim this feature.)
  • Likewise, D3’s map.keys returns an array, not an iterator.
  • Likewise, D3’s map.values returns an array, not an iterator.
  • D3’s map.entries method returns an array of {key, value} objects, not an iterator of [key, value].
  • D3’s map.set returns the new value, not the map instance
  • D3’s map.size is a method, not a read-only accessor property.
  • D3’s map.forEach doesn’t accept a thisArg.
  • D3 provides a map.empty method.

Until recently, my plan for 4.0 was to remove d3.map and d3.set, adopt ES6 Map and Set, and provide a barebones shim for runtimes that lack this native feature. But I decided the polyfill approach was flawed because the shim behaves very differently from the native feature, and this would leak out in D3’s public API (such as d3.namespaces and nest.map, which previously used ES6 Map).

I’d love to drop the shim and use ES6 Map and Set directly, but I don’t think it’s appropriate yet to require that of all users. And I don’t believe it’s possible to create a true shim of ES6 collections because that depends on language features like iterators and symbols.

Now, my intention is for d3.map and d3.set to exist on their own independent of ES6: similar, yes, but different. It’s confusing that the forEach method uses the opposite order of arguments from ES6, but d3.map has been around since February 2012, and changing the behavior now would be confusing, too.

So… how about renaming it to map.each and set.each? That’s similar to selection.each, and would make the difference from ES6 more apparent. And, it’s shorter and all lowercase, which I like. 😁

from d3-array.

mbostock avatar mbostock commented on July 28, 2024

See #14.

from d3-array.

mbostock avatar mbostock commented on July 28, 2024

Fixed in 0.2.0.

from d3-array.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.