Skip to content

Commit

Permalink
5.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
spencermountain committed Feb 3, 2023
1 parent 5bad89d commit 00f9da5
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 18 deletions.
78 changes: 68 additions & 10 deletions builds/suffix-thumb.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* suffix-thumb 5.0.1 MIT */
/* suffix-thumb 5.0.2 MIT */
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('efrt')) :
typeof define === 'function' && define.amd ? define(['exports', 'efrt'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.suffixThumb = {}, global.efrt));
})(this, (function (exports, efrt) { 'use strict';
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.suffixThumb = {}));
})(this, (function (exports) { 'use strict';

// 01- full-word exceptions
const checkEx = function (str, ex = {}) {
Expand Down Expand Up @@ -332,13 +332,27 @@
};
// console.log(compress('fixture', 'fixturing'))

const pack = function (obj) {
let byVal = {};
Object.keys(obj).forEach(k => {
let val = obj[k];
byVal[val] = byVal[val] || [];
byVal[val].push(k);
});
let out = [];
Object.keys(byVal).forEach(val => {
out.push(`${val}:${byVal[val].join(',')}`);
});
return out.join('¦')
};

const packObj = function (obj = {}) {
let tmp = {};
Object.keys(obj).forEach(k => {
let val = compress$1(k, obj[k]);// compress any shared prefix
tmp[k] = val;
});
return efrt.pack(tmp)
return pack(tmp)
};

const compress = function (model) {
Expand All @@ -351,14 +365,58 @@
return out
};


// let model = {
// fwd: {
// foo: 'food',
// bar: 'bard',
// cool: 'nice'
// }
// }
// console.log(compress(model))

const prefix = /^([0-9]+)/;

const toObject = function (txt) {
let obj = {};
txt.split('¦').forEach(str => {
let [key, vals] = str.split(':');
vals = (vals || '').split(',');
vals.forEach(val => {
obj[val] = key;
});
});
return obj
};

const growObject = function (key = '', val = '') {
val = String(val);
let m = val.match(prefix);
if (m === null) {
return val
}
let num = Number(m[1]) || 0;
let pre = key.substring(0, num);
let full = pre + val.replace(prefix, '');
return full
};

const unpackOne = function (str) {
let obj = toObject(str);
return Object.keys(obj).reduce((h, k) => {
h[k] = growObject(k, obj[k]);
return h
}, {})
};

const uncompress = function (model = {}) {
if (typeof model === 'string') {
model = JSON.parse(model);
}
model.fwd = efrt.unpack(model.fwd);
model.both = efrt.unpack(model.both);
model.bkwd = efrt.unpack(model.bkwd);
model.ex = efrt.unpack(model.ex);
model.fwd = unpackOne(model.fwd || '');
model.both = unpackOne(model.both || '');
model.rev = unpackOne(model.rev || '');
model.ex = unpackOne(model.ex || '');
return model
};

Expand Down
2 changes: 1 addition & 1 deletion builds/suffix-thumb.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 63 additions & 7 deletions builds/suffix-thumb.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/* suffix-thumb 5.0.1 MIT */
import { pack, unpack } from 'efrt';

/* suffix-thumb 5.0.2 MIT */
// 01- full-word exceptions
const checkEx = function (str, ex = {}) {
if (ex.hasOwnProperty(str)) {
Expand Down Expand Up @@ -328,6 +326,20 @@ let compress$1 = function (key, val) {
};
// console.log(compress('fixture', 'fixturing'))

const pack = function (obj) {
let byVal = {};
Object.keys(obj).forEach(k => {
let val = obj[k];
byVal[val] = byVal[val] || [];
byVal[val].push(k);
});
let out = [];
Object.keys(byVal).forEach(val => {
out.push(`${val}:${byVal[val].join(',')}`);
});
return out.join('¦')
};

const packObj = function (obj = {}) {
let tmp = {};
Object.keys(obj).forEach(k => {
Expand All @@ -347,14 +359,58 @@ const compress = function (model) {
return out
};


// let model = {
// fwd: {
// foo: 'food',
// bar: 'bard',
// cool: 'nice'
// }
// }
// console.log(compress(model))

const prefix = /^([0-9]+)/;

const toObject = function (txt) {
let obj = {};
txt.split('¦').forEach(str => {
let [key, vals] = str.split(':');
vals = (vals || '').split(',');
vals.forEach(val => {
obj[val] = key;
});
});
return obj
};

const growObject = function (key = '', val = '') {
val = String(val);
let m = val.match(prefix);
if (m === null) {
return val
}
let num = Number(m[1]) || 0;
let pre = key.substring(0, num);
let full = pre + val.replace(prefix, '');
return full
};

const unpackOne = function (str) {
let obj = toObject(str);
return Object.keys(obj).reduce((h, k) => {
h[k] = growObject(k, obj[k]);
return h
}, {})
};

const uncompress = function (model = {}) {
if (typeof model === 'string') {
model = JSON.parse(model);
}
model.fwd = unpack(model.fwd);
model.both = unpack(model.both);
model.bkwd = unpack(model.bkwd);
model.ex = unpack(model.ex);
model.fwd = unpackOne(model.fwd || '');
model.both = unpackOne(model.both || '');
model.rev = unpackOne(model.rev || '');
model.ex = unpackOne(model.ex || '');
return model
};

Expand Down

0 comments on commit 00f9da5

Please sign in to comment.