Hysen Labs
Library / SDK
bramstein/junify avatar
bramstein

junify

JUnify ― JavaScript Unification Library

50 stars2 forksJavaScriptLicense varies
01
DEEP OPEN-SOURCE ANALYSIS

Unification, in JavaScript

junify brings the unification algorithm to JavaScript for matching patterns against objects and arrays, with an API that reads like destructuring.

02
DEEP OPEN-SOURCE ANALYSIS

What unification means here

Unification is an algorithm for finding the substitutions that make two expressions match. If the expressions contain variables, those get bound to values so the match succeeds; if the expressions are not identical and the variables cannot be bound, it fails. The README compares the syntax and use case to destructuring assignment in JavaScript 1.7, and points out that extracting fields is only one use. Pattern matching and expert systems are listed as other things you could build.

03
DEEP OPEN-SOURCE ANALYSIS

The small API

All the code lives in the junify package, and the README notes the package name is left out of examples to keep them short. The important methods are unify, which takes two patterns and returns false or an object of variable bindings, and variable, which creates a named variable with an optional type. A wildcard constant matches any atom, array, or object without creating a binding, and there is a visit pattern method for traversing a pattern with a visitor object.

04
DEEP OPEN-SOURCE ANALYSIS

Matching both ways

Unification is bidirectional here. Both patterns can contain variables, so the example shows two patterns where bindings flow across both sides. A typed variable only matches when the types are identical in both patterns. That symmetry is what makes the library feel like real unification rather than a one directional matcher.

05
DEEP OPEN-SOURCE ANALYSIS

Where the limits are

The scope is explicit: junify can unify objects and arrays, not atoms. The atoms it refuses are Boolean, Number, String, Function, NaN, Infinity, undefined, and null. The wildcard constant can even stand in for an object property name, matching any property, but it must not be renamed because the library identifies wildcards internally by the symbol.

06
DEEP OPEN-SOURCE ANALYSIS

What you could build with it

Beyond extracting fields from JSON, the README points at pattern matching and expert systems, with a book reference for the classic example. The visit pattern method exists so you can rewrite custom pattern syntax before handing it to unify. For a library this small, the reach of those ideas is the interesting part.

07
DEEP OPEN-SOURCE ANALYSIS

Editorial conclusion

junify implements unification for objects and arrays in the browser and in Node.js. The API is small, unify and variable plus a wildcard constant and a pattern visitor, and the limits are clear: no atoms, and both patterns may carry variables.

08
DEEP OPEN-SOURCE ANALYSIS

Official sources

09
Community notes

Community notes