node-relation
This module helps you manage string, numbers, object as a group.
Check the code.
import { Relationship } from 'node-relation'
const A = new Relationship().to('a', 'b', 'c')
console.log(A.nodes)
const B = A.to('b', 'd')
console.log(B.nodes)
const C = B.to('e', 'f')
console.log(C.from('e').nodes)
const D = C.to('e', 'a')
console.log(D.from('e').nodes)
Install
You can download in npm node-relation.
npm install node-relation
How to use
Browser (umd)
<script src="https://cdn.jsdelivr.net/npm/node-relation@latest/dist/umd/index.min.js"></script>
<script>
const A = new NodeRelation.Relationship().to('a', 'b', 'c')
</script>
Browser (esnext)
import { Relationship } from 'https://cdn.jsdelivr.net/npm/node-relation@latest/dist/esm/index.min.js'
Node.js
import { Relationship } from 'node-relation'
const A = new Relationship().to('a', 'b', 'c')
Migration 3.x.x to 4.x.x
Check readme
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
You can pass dataset parameter to init this instance.
The RelationData
is type of 2-dimensional array. Check dataset getter description.
const state = new Relationship([['language', ['English', 'Korean', 'Japanese']]])
const clone = new Relationship(state.dataset)
(getter)
dataset: RelationData[]
Returns as 2-dimensional array of relationships between nodes in the instance. Relationships are returned to saveable data-type(json).
const state = new Relationship().to('a', 'b').to('b', 'c', 'd')
state.dataset
(getter)
nodes: RelationNode[]
Get all nodes from the instance.
const state = new Relation().to('a', 'b').to('b', 'c')
state.nodes
(getter)
nodeset: Set<RelationNode>
Get all nodes as Set object from the instance.
const state = new Relation().to('a', 'b').to('b', 'c')
state.nodeset
(getter)
oneHot: Map<RelationNode, number[]>
Get all nodes as one-hot vectors from the instance. It could be used as dataset for machine learning.
const state = new Relation().to('a', 'b').to('b', 'c')
const vectors = state.oneHot
vectors
Array.from(vectors.values())
(getter)
label: Map<RelationNode, number>
Get all nodes as labeled vector from the instance. It could be used as dataset for machine learning.
const state = new Relation().to('a', 'b').to('b', 'c')
const vector = state.label
vector
Array.from(vector.values())
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.
const A = new Relationship().to('language', 'English', 'Korean', 'Japanese')
language ─> English
language ─> Korean
language ─> Japanese
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.
const A = new Relationship().to('language', 'English', 'Korean', 'Japanese')
const B = A.both('English', 'US', 'France', 'Italy')
language ─> English
language ─> Korean
language ─> Japanese
language ─> English
(English <─> US)
(English <─> France)
(English <─> Italy)
language ─> Korean
language ─> Japanese
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.
const Team = new Relationship().all('john', 'harris', 'richard')
john <─> harris
harris <─> richard
richard <─> john
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 calculate all relationship between nodes in instance. Depth parameter default value is -1.
A.from('language').nodes
B.from('English').nodes
where(filter: (node: RelationNode
, i: number
, array: RelationNode[]
) => boolean
): Relationship
Returns a new relationship instance with only nodes that meet the conditions.
A.where((v) => v.includes('Kor')).nodes
without(...nodes: RelationNode[]
): RelationNode[]
Returns the remaining nodes except those received as parameters from the current relationship instance.
A.from('language').without('language')
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.
B.unlinkTo('English', 'France')
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.
B.unlinkBoth('English', 'France')
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.
B.drop('language').nodes
has(node: RelationNode
): boolean
Returns whether the instance contains that node.
const hasKorean = B.has('korean')
hasAll(...nodes: RelationNode[]
): boolean
Returns whether the instance contains all of its nodes.
const hasAll = B.hasAll('Japanese', 'Korean')
weight(node: RelationNode
, log?: boolean
= false
): number
Returns how many nodes are related to the node received by the parameter.
const weight = B.weight('language')
weights(log?: boolean
= false
, normalize?: boolean
= false
): Map<RelationNode, number>
Returns the weight of all nodes. Check the weight
method.
const weights = B.weights()
clear(): void
Destroy the data in the instance. It is used for garbage collector.
Try it simply
const state = new Relationship()
.to('language', 'English', 'Korean', 'Japanese')
.both('English', 'US', 'France', 'Italy')
console.log(`Languages: ${ state.from('language').without('language') }`)
console.log(`English country: ${ state.from('English').drop('language').without('English') }`)
Applying (Advanced usage, with Typescript)
import { Relationship } from 'node-relation'
type ServerName = 'server-a' | 'server-b'
class User {
...
}
const userA = new User
const userB = new User
const userC = new User
let state: Relationship<ServerName|User> = new Relationship
state = state.to('server-a', userA, userB)
state = state.to('server-b', userC)
console.log( state.from('server-b').without('server-b') )
import { Relationship } from 'node-relation'
class Human {
name: string
constructor(name: string) {
this.name = name
}
sayHello() {
console.log(`${this.name}: Hello, my name is ${this.name}`)
}
}
const john = new Human('john')
const paul = new Human('paul')
const lawrence = new Human('lawrence')
const jacob = new Human('jacob')
const richard = new Human('richard')
const collin = new Human('collin')
const manager = new Human('harris')
let state: Relationship<Human> = new Relationship
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.from(manager, 1).without(manager).forEach((leader: Human) => {
leader.sayHello()
console.log(`${leader.name}: And... these are my teammates.`)
state.from(leader).without(leader).forEach((member: Human) => {
member.sayHello()
})
})