Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

datatype-expansion

Package Overview
Dependencies
Maintainers
2
Versions
31
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

datatype-expansion - npm Package Compare versions

Comparing version 0.1.1 to 0.2.0

LICENSE

35

package.json
{
"name":"datatype-expansion",
"version":"0.1.1",
"main":"index",
"name": "datatype-expansion",
"version": "0.2.0",
"description": "Utility tool to expand a given RAML type and create a canonical form",
"main": "src/index.js",
"scripts": {
"lint": "standard",
"test-cov": "istanbul --include-all-sources cover --root src node_modules/mocha/bin/_mocha --report lcovonly -- -R dot --recursive --bail test/",
"test-spec": "_mocha -R spec --recursive --bail test/",
"test": "npm run lint && npm run test-cov",
"browserify": "browserify src/index.js -o datatype-expansion.js -s datexp"
},
"keywords": [
"raml",
"expansion",
"json",
"types"
],
"homepage": "https://github.com/raml-org/raml-parser-toolbelt/tree/master/tools/datatype-expansion",
"author": "Mulesoft",
"license": "Apache-2.0",
"bugs": {
"url": "https://github.com/raml-org/raml-parser-toolbelt/issues"
}
"devDependencies": {
"browserify": "^14.4.0",
"chai": "^4.0.2",
"istanbul": "^0.4.5",
"mocha": "^3.4.2",
"standard": "^10.0.2"
},
"dependencies": {
"lodash": "^4.17.4"
},
"files": [
"src/**",
"LICENSE",
"test/**"
]
}

@@ -12,192 +12,1 @@ # RAML Data Type Expansion

```
### Developer Environment
To run this project and build the different packages for Java or Javascript, you need to install `leiningen`. You can use [homebrew](http://brew.sh/) for that.
```
brew install leiningen
```
## RAML type expanded form
The RAML expanded form for a RAML type, resolves references and fills missing information to compute a fully expanded representation of the type.
The form and the algorithm to compute is [documented here](doc/algorithms.md).
### Usage Clojure
The Clojure interface for the library offers a function to compute the expanded form using the `datatype-expansion.expanded-form/expanded-form` function.
The function accepts an in-memory representation of the RAML type (keywordize) and a map from type names to types with the same representation.
It returns the expanded form.
The following example shows a usage example:
``` clojure
;; A type context with some types definitions
(def types-context {"Songs.Song" {:properties {:title "string"
:length "number"}}
"Songs.Album" {:properties {:title "string"
:songs "Songs.Song[]"}}})
(is (= (datatype-expansion.expanded-form/expanded-form (get types-context "Songs.Album") types-context)
{:type "object"
:properties
{"title" {:type "string" :required true}
"songs" {:type "array"
:items {:type "object"
:properties
{"title" {:type "string" :required true}
"length" {:type "number" :required true}}
:additionalProperties true
:required true}
:required true}}
:additionalProperties true
:required true}))
```
### Usage Node.js
The Node.js interface for the library offers the `expandedForm` function to compute the expanded form.
It accepts an in-memory JSON representation of the type, the types mapping and a callback function.
If the invocation succeeds, it will return the expanded form as an argument to the provided callback function.
``` javascript
var tools = require("datatype-expansion");
var typesContext = {"Songs.Song": {"properties": {"title": "string",
"length": "number"}},
"Songs.Album": {"properties": {"title": "string",
"songs": "Songs.Song[]"}}};
tools.expandedForm(typesContext["Songs.Album"], typesContext, function(err, expanded) {
// expanded contains the computed expanded form
//{
// "properties": {
// "title": {
// "type": "string",
// "required": true
// },
// "songs": {
// "type": "array",
// "items": {
// "properties": {
// "title": {
// "type": "string",
// "required": true
// },
// "length": {
// "type": "number",
// "required": true
// }
// },
// "additionalProperties": true,
// "type": "object",
// "required": true
// },
// "required": true
// }
// },
// "additionalProperties": true,
// "type": "object",
// "required": true
//}
console.log(JSON.stringify(expanded,null,2));
});
```
## RAML type canonical form
The canonical form computes inheritance and pushes unions to the top level of the type structure of an expanded RAML type. It is described in the [documentation section](doc/algorithms.md) of this repository.
### Usage Clojure
The Clojure interface to compute the canonical form is implemented in the `datatype-expansion.canonical-form/canonical-form` function.
The function accepts and expanded RAML type and returns the canonical form for the same type.
``` clojure
;; A type context with some types definitions
(def types-context {"Songs.Song" {:properties {:title "string"
:length "number"}}
"Songs.Album" {:properties {:title "string"
:songs "Songs.Song[]"}}})
(-> types-context
(get "Songs.Song")
(datatype-expansion.expanded-form/expanded-form types-context)
datatype-expansion.canonical-form/canonical-form)
```
### Usage Node.js
The Node.js version of the canonical form function is defined in the `canonicalForm` function of the library module.
It accepts a JSON in-memory representation of an expanded RAML type and a callback function. It returns the canonical form or an exception:
``` javascript
var tools = require("datatype-expansion");
var typesContext = {"Songs.Song": {"properties": {"title": "string",
"length": "number"}},
"Songs.Album": {"properties": {"title": "string",
"songs": "Songs.Song[]"}}};
tools.expandedForm(typesContext["Songs.Album"], typesContext, function(err, expanded) {
tools.canonicalForm(expanded, function(err, canonical) {
// canonical contains the computed canonical form
console.log(canonical);
});
});
```
### Usage browser
Require the browser build of the library, located in the `browser` directory.
``` html
<html>
<head>
<script type="text/javascript" src="./browser/index.js"></script>
</head>
<body>
<script type='text/javascript'>
var compose = function(g,f) {
return (function(x) { return g(f(x)); });
};
console.log("Starting...");
var result = compose(
datatype_expansion.js.canonicalForm,
datatype_expansion.js.expandedForm
)("string");
console.log(result); // you should get {"type": "string"}
</script>
</body>
</html>
```
## Running tests
Tests for the library can be run for the clojure version using leiningen:
``` shell
$ lein test
```
The JavaScript tests can be run with the following leiningen invocation:
``` shell
$ lein doo node test
```
## Building the NPM package
The ClojureScript code of the library can be packaged as a NPM library using the following leiningen comand:
``` shell
$ lein cljsbuild once default
```
This command will compile the ClojureScript code into JavaScript code that can be published as a NPM package using the provided `packages.json` file.
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc