From 66aaf64c8146af44e4a9f057979c864663a70d03 Mon Sep 17 00:00:00 2001 From: satori Date: Sun, 30 Jan 2022 01:14:45 +0800 Subject: [PATCH] beta --- README.md | 33 ++++++++++++++++++++++++++++++ demo.js | 27 ++++++++++++++++++++++++ index.js | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 20 ++++++++++++++++++ 4 files changed, 138 insertions(+) create mode 100644 demo.js create mode 100644 index.js create mode 100644 package.json diff --git a/README.md b/README.md index a1e529e..9bf7ca8 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,35 @@ # interrelated js 数据双向关联 + + +```javascript + +import fmhub from './index.js' + +const fm = new fmhub() +const users = ["0001", '0002', "0003", "0004"] + +// 一组用户全部订阅频道一 +users.forEach((item) => { + fm.set("频道一号", item) +}) + +// 操作所有订阅了频道一的用户 +fm.atob("频道一号", (data) => { + console.log(data) +}) + +fm.btoa("0002", (data) => { + console.log(data) +}) + +// console print: +// +// 0001 +// 0002 +// 0003 +// 0004 +// 频道一号 +// + +``` diff --git a/demo.js b/demo.js new file mode 100644 index 0000000..2058559 --- /dev/null +++ b/demo.js @@ -0,0 +1,27 @@ +import fmhub from './index.js' + +const fm = new fmhub() +const users = ["0001", '0002', "0003", "0004"] + +// 一组用户全部订阅频道一 +users.forEach((item) => { + fm.set("频道一号", item) +}) + +// 操作所有订阅了频道一的用户 +fm.atob("频道一号", (data) => { + console.log(data) +}) + +fm.btoa("0002", (data) => { + console.log(data) +}) + +// console print: +// +// 0001 +// 0002 +// 0003 +// 0004 +// 频道一号 +// \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..b8f3edd --- /dev/null +++ b/index.js @@ -0,0 +1,58 @@ +export default class interrelated { + constructor() { + this.channels = new Map() + this.users = new Map() + } + + // 将两种类型的不同信息相关连 + set(fid, uid) { + let channel = this.channels.get(fid) || new Map() + if (!channel.size) this.channels.set(fid, channel) + channel.set(uid, true) + let user = this.users.get(uid) || new Map() + if (!user.size) this.users.set(uid, user) + user.set(fid, true) + } + + // 取消关联 + delete(fid, uid) { + let channel = this.channels.get(fid) + if (channel && channel.size) channel.delete(uid) + let user = this.users.get(uid) + if (user && user.size) user.delete(fid) + } + + // 读取类型 A 关联的 B + atob(uid, callback) { + let channel = this.channels.get(uid) + if (channel && channel.size) { + channel.forEach((value, key) => { + callback(key) + }) + } + } + + // 读取类型 B 关联的 A + btoa(fid, callback) { + let user = this.users.get(fid) + if (user && user.size) { + user.forEach((value, key) => { + callback(key) + }) + } + } + + // 读取所有 a + aall(uid, callback) { + this.channels.forEach((value, key) => { + callback(key) + }) + } + + // 读取所有 b + ball(fid, callback) { + this.users.forEach((value, key) => { + callback(key) + }) + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..644d77f --- /dev/null +++ b/package.json @@ -0,0 +1,20 @@ +{ + "name": "interrelated", + "version": "1.0.0", + "description": "js 数据双向关联", + "main": "index.js", + "type": "module", + "scripts": { + "test": "demo.js" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/InvisibleFuture/interrelated.git" + }, + "author": "satori.love", + "license": "MIT", + "bugs": { + "url": "https://github.com/InvisibleFuture/interrelated/issues" + }, + "homepage": "https://github.com/InvisibleFuture/interrelated#readme" +} \ No newline at end of file