first commit

This commit is contained in:
输入äwhat 2023-04-20 11:14:04 +08:00
commit 3ae53625c0
10 changed files with 192 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
node_modules/
.vscode/
yarn.lock
yarn-error.log

1
README.md Normal file
View File

@ -0,0 +1 @@
-- 依赖 lodash.js

8
babel.config.js Normal file
View File

@ -0,0 +1,8 @@
module.exports = {
presets: [
['@babel/preset-env'],
],
plugins: [
"@babel/plugin-proposal-class-properties"
]
}

11
dist/jbase62.js vendored Normal file

File diff suppressed because one or more lines are too long

1
dist/jbase62.js.map vendored Normal file

File diff suppressed because one or more lines are too long

2
jest.config.js Normal file
View File

@ -0,0 +1,2 @@
module.exports = {
};

29
package.json Normal file
View File

@ -0,0 +1,29 @@
{
"name": "jbase62",
"version": "1.0.0",
"main": "dist/jBase62.js",
"module": "src/index.js",
"author": "what-00@qq.com",
"license": "MIT",
"scripts": {
"test": "jest --verbose ./test",
"build": "webpack --config webpack.config.js"
},
"devDependencies": {
"@babel/core": "^7.8.4",
"@babel/plugin-proposal-class-properties": "^7.18.6",
"@babel/preset-env": "^7.8.4",
"@types/jest": "^29.5.0",
"babel-jest": "^29.5.0",
"babel-loader": "^8.0.6",
"jest": "^29.5.0",
"webpack": "4.41.6",
"webpack-cli": "3.3.11"
},
"dependencies": {
"base-x": "^4.0.0",
"jsonpack": "^1.1.5",
"lodash": "^4.17.21",
"merge": "^2.1.1"
}
}

63
src/index.js Normal file
View File

@ -0,0 +1,63 @@
import _ from 'lodash';
import jsonpack from 'jsonpack';
import merge from 'deepmerge';
import basex from 'base-x';
class JBase62 {
static #instance = null
static getInstance = (characters = null) => {
if (characters || !JBase62.#instance) {
JBase62.#instance = new JBase62(characters)
}
return JBase62.#instance
}
#basex = null
constructor(characters = "") {
if (!characters && !JBase62.#instance) {
throw new "characters is required"
}
if (characters) {
this.#basex = basex(characters)
} else if (JBase62.#instance) {
return JBase62.#instance
}
}
encode = (json = {}) => {
const jsonString = JSON.stringify(json, (__, v) => v === undefined ? null : v)
const binaryString = jsonpack.pack(jsonString)
return this.#basex.encode(Buffer.from(binaryString))
}
decode = (base62 = '', defaultValue = {}) => {
if (base62 === undefined || base62 === '') {
return defaultValue
}
try {
const binaryString = this.#basex.decode(base62).reduce((carry, bin) => carry + String.fromCharCode(bin), "")
const jsonString = jsonpack.unpack(binaryString)
return typeof (jsonString) === 'string' ? JSON.parse(jsonString) : jsonString
} catch (e) {
console.warn('JBase62.decode', e)
return defaultValue
}
}
get = (base62, path, defaultValue) => _.get(this.decode(base62), path, defaultValue)
set = (base62, path, value) => {
const obj = this.decode(base62)
_.set(obj, path, value)
return this.encode(obj)
}
assign = (base62, data = {}, isDeepMerge = false) => this.encode(isDeepMerge ? merge(this.decode(base62), data) : Object.assign(this.decode(base62), data))
}
export default JBase62

41
test/index.test.js Normal file
View File

@ -0,0 +1,41 @@
import { describe, expect, test } from '@jest/globals';
import JBase62 from "../src";
test('encode example 1', () => {
const x = JBase62.getInstance("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")
expect(x.encode({ A: 123 })).toEqual("6N4GIxiF9tHa12D")
const xx = JBase62.getInstance("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
expect(xx.encode({ A: 123 })).toEqual("GXEQS7sPJ3RkBCN")
});
test('encode example 2', () => {
const x = JBase62.getInstance("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")
expect(x.encode({ A: 123 })).toEqual("6N4GIxiF9tHa12D")
// 是用最后一次的 characters
const xx = JBase62.getInstance()
// expect(xx.encode({ A: 123 })).toEqual("GXEQS7sPJ3RkBCN")
expect(xx.encode({ A: 123 })).toEqual("6N4GIxiF9tHa12D")
});
test('encode example 3', () => {
const x = JBase62.getInstance("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")
// set
expect(x.set("6N4GIxiF9tHa12D", "a.b", 888)).toEqual("C2pN4cIiUNzT0X9EvQYWqK53E4r368y9hsb")
// decode
expect(x.decode("C2pN4cIiUNzT0X9EvQYWqK53E4r368y9hsb")).toEqual({ A: 123, a: { b: 888 } })
// get
expect(x.get("C2pN4cIiUNzT0X9EvQYWqK53E4r368y9hsb", "a.b")).toEqual(888)
// assign
expect(x.assign("C2pN4cIiUNzT0X9EvQYWqK53E4r368y9hsb", { a: { c: 333 } })).toEqual("C2pN4cL9JKMyAdG8e5JWK3Z3cDQtawtYziX")
expect(x.decode("C2pN4cL9JKMyAdG8e5JWK3Z3cDQtawtYziX")).toEqual({ A: 123, a: { c: 333 } })
// merge
expect(x.assign("C2pN4cIiUNzT0X9EvQYWqK53E4r368y9hsb", { a: { c: 333 } }, true)).toEqual("Hd9OGloH4qT5y8F6q9rSqeLiTMwJLFdQbq7UrjP2I1ZmTSH")
expect(x.decode("Hd9OGloH4qT5y8F6q9rSqeLiTMwJLFdQbq7UrjP2I1ZmTSH")).toEqual({ A: 123, a: { b: 888, c: 333 } })
});

32
webpack.config.js Normal file
View File

@ -0,0 +1,32 @@
const path = require('path');
module.exports = {
mode: 'production',
devtool: 'nosources-source-map',
entry: './src/index.js',
externals: ['lodash'],
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'jbase62.js',
library: "JBase62",
libraryExport: 'default',
libraryTarget: 'umd',
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env'],
}
}
}
]
},
externals: {
'lodash': '_',
}
};