Socket
Socket
Sign inDemoInstall

node-relation

Package Overview
Dependencies
0
Maintainers
1
Versions
46
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 3.3.1 to 4.0.0-alpha.0

dist/umd/index.js

14

package.json
{
"name": "node-relation",
"version": "3.3.1",
"version": "4.0.0-alpha.0",
"description": "Manage strings, number, object that are related to each other.",
"main": "dist/index.js",
"main": "dist/umd/index.js",
"module": "dist/module/index.js",
"types": "types/index.d.ts",
"scripts": {

@@ -10,2 +12,6 @@ "test": "echo \"Error: no test specified\" && exit 1",

},
"files": [
"dist/*.js",
"types/*.d.ts"
],
"author": "izure <admin@izure.org>",

@@ -16,4 +22,4 @@ "homepage": "https://github.com/izure1/node-relation",

"devDependencies": {
"webpack": "4.44.1",
"webpack-cli": "3.3.12",
"webpack": "^5.68.0",
"webpack-cli": "^4.9.2",
"@types/node": "12.12.2",

@@ -20,0 +26,0 @@ "typescript": "4.0.3",

@@ -5,149 +5,244 @@ # node-relation

Check the code.
```
```javascript
import { Relationship } from 'node-relation'
const A = new Relationship().setReferTo('a', 'b', 'c')
const A = new Relationship().to('a', 'b', 'c')
console.log(A.nodes) // ['a', 'b', 'c']
const B = A.setReferTo('b', 'd')
const B = A.to('b', 'd')
console.log(B.nodes) // ['a', 'b', 'c', 'd']
const C = B.setReferTo('e', 'f')
console.log(C.getRelation('e').nodes) // ['e', 'f']
const C = B.to('e', 'f')
console.log(C.from('e').nodes) // ['e', 'f']
const D = C.setReferTo('e', 'a')
console.log(D.getRelation('e').nodes) // ['a', 'b', 'c', 'd', 'e', 'f']
const D = C.to('e', 'a')
console.log(D.from('e').nodes) // ['a', 'b', 'c', 'd', 'e', 'f']
```
# Install
## Install
You can download in npm [node-relation](https://www.npmjs.com/package/node-relation).
```
```bash
npm install node-relation
```
# How to use
## Web browser
```
<script src="js/nodeRelation.js"></script>
## How to use
### Browser (umd)
```html
<script src="node-relation/dist/umd/index.js"></script>
<script>
const A = new NodeRelation.Relationship().setReferTo('a', 'b', 'c')
const A = new NodeRelation.Relationship().to('a', 'b', 'c')
</script>
```
## Front-end
### Browser (esnext)
```javascript
import { Relationship } from 'node-relation/dist/module/index.js'
```
### Node.js
```javascript
import { Relationship } from 'node-relation'
const A = new Relationship().setReferTo('a', 'b', 'c')
const A = new Relationship().to('a', 'b', 'c')
```
## Back-end
```
const { Relationship } = require('node-relation')
const A = new Relationship().setReferTo('a', 'b', 'c')
```
# Methods
### Migration 3.x.x to 4.x.x
Check [readme](./Migration_3_to_4.md)
### Methods
The data inside the instance is immutable.
The method does not modify the data inside, it returns the result of the calculation as a new instance.
### constructor(dataset?: `RelationData[]`): `Relationship`
#### constructor(dataset?: `RelationData[]`): `Relationship`
You can pass dataset parameter to init this instance.
The `RelationData` is type of 2-dimentional array. Check dataset getter description.
```
The `RelationData` is type of 2-dimensional array. Check dataset getter description.
```javascript
const state = new Relationship([['language', ['English', 'Korean', 'Japanese']]])
const copy = new Relationship(state.dataset)
const clone = new Relationship(state.dataset)
```
### `(getter)` dataset: `RelationData[]`
Returns as 2-dimentional array of relationships between nodes in the instance. Relationships are returned to saveable data-type(json).
```
const state = new Relationship().setReferTo('a', 'b').setReferTo('b', 'c', 'd')
#### `(getter)` dataset: `RelationData[]`
Returns as 2-dimensional array of relationships between nodes in the instance. Relationships are returned to saveable data-type(json).
```javascript
const state = new Relationship().to('a', 'b').to('b', 'c', 'd')
state.dataset // [ [a,['b']], [b,['c', 'd']] ]
```
### `(getter)` nodes: `RelationNode[]`
#### `(getter)` nodes: `RelationNode[]`
Get all nodes from the instance.
```
const state = new Relation().setReferTo('a', 'b').setReferTo('b', 'c')
```javascript
const state = new Relation().to('a', 'b').to('b', 'c')
state.nodes // a, b, c
```
### `(getter)` nodeset: `Set<RelationNode>`
#### `(getter)` nodeset: `Set<RelationNode>`
Get all nodes as Set object from the instance.
```
const state = new Relation().setReferTo('a', 'b').setReferTo('b', 'c')
```javascript
const state = new Relation().to('a', 'b').to('b', 'c')
state.nodeset // Set<['a', 'b', 'c']>
```
### setReferTo(source: `RelationNode`, ...dists: `RelationNode[]`): `Relationship`
#### to(source: `RelationNode`, ...targets: `RelationNode[]`): `Relationship`
Creates a new refer between nodes, and returns it as a Relationship instance.
This is one-sided relationship between both nodes.
```javascript
const A = new Relationship().to('language', 'English', 'Korean', 'Japanese')
```
const A = new Relationship().setReferTo('language', 'English', 'Korean', 'Japanese')
```bash
# A
language ─> English
language ─> Korean
language ─> Japanese
```
### setReferBoth(a: `RelationNode`, ...b: `RelationNode[]`): `Relationship`
#### both(a: `RelationNode`, ...b: `RelationNode[]`): `Relationship`
Creates a new relationship between nodes, and returns it as a new Relationship instance.
Both nodes will know each other.
```javascript
const A = new Relationship().to('language', 'English', 'Korean', 'Japanese')
const B = A.both('English', 'US', 'France', 'Italy')
```
const A = new Relationship().setReferTo('language', 'English', 'Korean', 'Japanese')
const B = A.setReferBoth('English', 'US', 'France', 'Italy')
```bash
# A
language ─> English
language ─> Korean
language ─> Japanese
# B
language ─> English
(English <─> US)
(English <─> France)
(English <─> Italy)
language ─> Korean
language ─> Japanese
```
### setReferAll(...nodes: `RelationNode[]`): `Relationship`
#### all(...nodes: `RelationNode[]`): `Relationship`
Creates a new relationship between all each other nodes, and returns it as a new Relationship instance.
All nodes will know each others.
```javascript
const Team = new Relationship().all('john', 'harris', 'richard')
```
const Team = new Relationship().setReferAll('john', 'harris', 'richard')
```bash
# Team
john <─> harris
harris <─> richard
richard <─> john
```
### getRelation(source: `RelationNode[]`, depth?: `number` = -1): `Relationship`
#### from(source: `RelationNode[]`, depth?: `number` = -1): `Relationship`
Only the nodes that are related to the node received by the parameter are filtered and returned in a new Relationship instance.
You can control calculation depth relationship with depth parameter. If depth parameter are negative, it's will be calculte all relationship between nodes in instance. Depth parameter default value is -1.
You can control calculation depth relationship with depth parameter. If depth parameter are negative, it's will be calculate all relationship between nodes in instance. Depth parameter default value is -1.
```javascript
A.from('language').nodes // language, English, Korean, Japanese
B.from('English').nodes // language, English, US, France, Italy
```
A.getRelation('language').nodes // language, English, Korean, Japanese
B.getRelation('English').nodes // language, English, US, France, Italy
#### where(filter: (node: `RelationNode`, i: `number`, array: `RelationNode[]`) => `boolean`): `Relationship`
Returns a new relationship instance with only nodes that meet the conditions.
```javascript
A.where((v) => v.includes('Kor')).nodes // Korean
```
### getNodes(node: `RelationNode`): `RelationNode[]`
Same as `(getter)nodes`, but removes the node passed as a parameter.
#### without(...nodes: `RelationNode[]`): `RelationNode[]`
Returns the remaining nodes except those received as parameters from the current relationship instance.
```javascript
A.from('language').without('language') // English, Korean, Japanese
```
B.getRelation('language').getNodes('language') // English, Korean, Japanese, US, France, Italy
```
### getNodeset(node: `RelationNode`): `Set<RelationNode>`
Same as `(getter)nodeset`, but removes the node passed as a parameter.
```
B.getRelation('language').getNodeset('language') // Set<['English', 'Korean', 'Japanese', 'US', 'France', 'Italy']>
```
### getAmbientNodes(node: `RelationNode`): `RelationNode[]`
Alias to `getNodes`
### getAmbientNodeset(node: `RelationNode`): `Set<RelationNode>`
Alias to `getNodeset`
### unlinkTo(source: `RelationNode`, ...dists: `RelationNode[]`): `Relationship`
#### unlinkTo(source: `RelationNode`, ...targets: `RelationNode[]`): `Relationship`
Deletes the relationship between nodes and returns it as a new Relationship instance.
This is one-sided cut off between both nodes.
```
```javascript
B.unlinkTo('English', 'France')
```
### unlinkBoth(a: `RelationNode`, ...b: `RelationNode[]`): `Relationship`
#### unlinkBoth(a: `RelationNode`, ...b: `RelationNode[]`): `Relationship`
Deletes the relationship between nodes and returns it as a new Relationship instance.
Both nodes will cut off each other.
```
```javascript
B.unlinkBoth('English', 'France')
```
### dropNode(...nodes: `RelationNode[]`): `Relationship`
#### drop(...nodes: `RelationNode[]`): `Relationship`
Delete the node. If the node associated with the deleted node is isolated, it is deleted together. Returns the result with a new Relationship instance.
```javascript
B.drop('language').nodes // English, US, France, Italy
```
B.dropNode('language').nodes // English, US, France, Italy
```
### hasNode(node: `RelationNode`): `Boolean`
#### has(node: `RelationNode`): `Boolean`
Returns whether the instance contains that node.
```javascript
const hasKorean = B.has('korean') // true
```
const hasKorean = B.hasNode('korean') // true
#### hasAll(...nodes: `RelationNode[]`): `Boolean`
Returns whether the instance contains all of its nodes.
```javascript
const hasAll = B.hasAll('Japanese', 'Korean') // true
```
### clear(): `void`
#### clear(): `void`
Destroy the data in the instance. It is used for garbage collector.
# Try it simply
```
## Try it simply
```javascript
const state = new Relationship()
.setReferTo('language', 'English', 'Korean', 'Japanese')
.setReferBoth('English', 'US', 'France', 'Italy')
.to('language', 'English', 'Korean', 'Japanese')
.both('English', 'US', 'France', 'Italy')
console.log(`Languages: ${ state.getRelation('language').getAmbientNodes('language') }`)
console.log(`Languages: ${ state.from('language').without('language') }`)
// Languages: English, Korean, Japanese, US, France, Italy
console.log(`English country: ${ state.getRelation('English').dropNode('language').getAmbientNodes('English') }`)
console.log(`English country: ${ state.from('English').drop('language').without('English') }`)
// English country: US, France, Italy
```
# Applying (Advanced usage, with Typescript)
```
## Applying (Advanced usage, with Typescript)
```javascript
// The data structure what you want

@@ -162,3 +257,5 @@ // {

type ServerName = 'server-a' | 'server-b'
class User {}
class User {
...
}

@@ -171,8 +268,9 @@ const userA = new User

state = state.setReferTo('server-a', userA, userB)
state = state.setReferTo('server-b', userC)
state = state.to('server-a', userA, userB)
state = state.to('server-b', userC)
console.log( state.getRelation('server-b').getAmbientNodes('server-b') ) // userC
console.log( state.from('server-b').without('server-b') ) // userC
```
```
```javascript
import { Relationship } from 'node-relation'

@@ -206,14 +304,14 @@

state = state.setReferTo(manager, john, jacob)
.setReferAll(john, paul, lawrence)
.setReferAll(jacob, richard, collin)
state = state.to(manager, john, jacob)
.all(john, paul, lawrence)
.all(jacob, richard, collin)
console.log(`${manager.name}: Here are the leaders of my team.`)
state.getRelation(manager, 1).getAmbientNodes(manager).forEach((leader: Human) => {
state.from(manager, 1).without(manager).forEach((leader: Human) => {
leader.sayHello()
console.log(`${leader.name}: And... these are my teammates.`)
state.getRelation(leader).getAmbientNodes(leader).forEach((member: Human) => {
state.from(leader).without(leader).forEach((member: Human) => {
member.sayHello()
})
})
```
```
SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc