Skip to content

v4.0

Compare
Choose a tag to compare
@sylvainpolletvillard sylvainpolletvillard released this 17 Jun 13:47
· 183 commits to master since this release

Breaking changes in v4

  • Node 6-7 support dropped. Long term support has officially ended on 2018-04-30.
  • UMD build dropped in favor of ESM builds exclusively. See explainations below.
  • Browser support is the same than v3, but you will have to bring your own transpiler. See explainations below.
  • REWORK defaults assignment: defaults for object models are no longer assigned to the model prototype, but applied only once at model instanciation, and now work for nested properties default values. This change aims to make default values behaviour more intuitive because using the prototype was too confusing for many users (I received many questions about it). To get the original behaviour, manually assign the default values to the model prototype.
  • API cleanup: removed ObjectModel defaults method. With the rework of defaults assignment, the distinction between defaults and defaultTo is no longer needed, so you can replace all defaults() calls with defaultTo() instead.
  • API cleanup: removed Model validate method, in favor of a second optional argument for the test method. Explaination: #96
  • API cleanup: removed the params argument to Model and ObjectModel constructors ; can be replaced with Object.assign
  • MOVED sealed object models to their own external models (see examples/sealed.js). This reduces the size of the library for users that don't use sealed models.
  • REMOVED the numberOfArgs assertion that was bundled with all function models. Explaination: #108

New features

  • Any
  • Any.remaining

Read the docs here

Moving from UMD to ESM

ObjectModel used to be distributed in both ESM (EcmaScript Module) and UMD (Universal Module Definition) format. ESM provide several advantages over UMD:

  • you can import only the features you need, and get a smaller library size penalty
  • modules run in strict mode by default, which covers more potential errors
  • globals are dangerous because they can be overridden by other libs, user scripts or browser extensions
  • AMD and other asynchronous module formats are basically dead now that dynamic import() is standard

In 2019, modern browsers now support natively ESM, and older browsers can still be supported through bundling tools like Webpack, Parcel or Rollup.

Therefore, starting from v4, ObjectModel will be exclusively distributed in ES module format. You should import what you need from the library like this:

import { Model, ObjectModel, ArrayModel } from 'objectmodel'

Some may consider this move to be too early, but there is technically no more argument in favor of UMD today, and legacy codebases can still use bundler tools or manually insert an UMD wrapper if needed. If you just want the ObjectModel API exposed as global variables like before, although I won't personally recommend it, this can be easily done with:

<script type="module">
	import * as globals from "./dist/object-model.js"
	Object.assign(window, globals)
</script>

Please consider switching to ES Modules for all your JavaScript projects if not done already.

Bring your own transpiler

In previous builds of ObjectModel, all the JS code transformations required to support older browsers were included in the distributed files, so that you could directly use the library and get maximum browser support. However, this came at a price: modern browsers had to suffer size and performance penalties due to useless transformations only required for older browsers support.

Therefore, starting from v4, ObjectModel is distributed in the latest version of the ECMAScript standard, with no consideration for code transforms used to improve the browser support. It will be your responsibility to configure a transpiler on your project for this purpose. The most popular option, and my personal recommendation, would be babel and @babel/preset-env.

This decision is motivated by these arguments:

  • it seems unfair to impose the same size and perf penalties on all projects, even those targeting a smaller set of modern browsers.
  • this fits logically with the decision to only distribute ESM builds, since you will have to configure a build step to support older browsers
  • transpilers in 2019 are a very common part of every web dev's toolkit, and included by default in most modern JS frameworks

As always, if you are unhappy with these changes, feel free to stick with an older version of ObjectModel which will continue to receive bugfixes.

Roadmap and public proposals

Since this library is starting to get some traction, I try to make its evolution more participative. New features and breaking changes in v4 have all been introduced as proposals in github issues, open to public discussion and feedback for a few months. I also look at open source projects using ObjectModel to see how it is being used and how could I improve the lib for them. As a reminder, there is a Gitter channel if you want to ask me questions or discuss about ObjectModel in a way that does not justify opening a issue on Github.