1
0
Fork 0

Added @freesewing/utils

This commit is contained in:
Joost De Cock 2019-04-27 12:21:14 +02:00
parent d8cc1f76f3
commit e7b7c5a7dd
18 changed files with 393 additions and 0 deletions

View file

@ -0,0 +1,3 @@
const cloneObject = src => Object.assign({}, src);
export default cloneObject;

View file

@ -0,0 +1,15 @@
const defaultGist = {
settings: {
embed: true,
sa: 0,
complete: true,
paperless: false,
locale: "en",
units: "metric",
margin: 2,
measurements: {},
options: {}
}
};
export default defaultGist;

View file

@ -0,0 +1,6 @@
const defaultSa = {
imperial: 15.875,
metric: 10
};
export default defaultSa;

View file

@ -0,0 +1,17 @@
const formatImperial = (
neg,
inch,
numo = false,
deno = false,
format = "html"
) => {
if (format === "html") {
if (numo) return `<span>${neg}${inch}<sup>${numo}</sup>/<sub>${deno}</sub></span>`;
else return `<span>{neg}{inch}</span>`;
} else {
if (numo) return `${neg}${inch}`;
else return `${neg}${inch} ${numo}/${deno}`;
}
};
export default formatImperial;

View file

@ -0,0 +1,43 @@
import roundMm from "./roundMm";
import formatImperial from "./formatImperial";
const formatMm = (val, units, format = "html") => {
val = roundMm(val);
let H = format === "html" ? true : false;
if (units === "imperial") {
if (val == 0) return formatImperial("", 0, false, false, format);
let negative = "";
let inches = "";
let rest = "";
let fraction = val / 25.4;
if (fraction < 0) {
fraction = fraction * -1;
negative = "-";
}
if (Math.abs(fraction) < 1) rest = fraction;
else {
inches = Math.floor(fraction);
rest = fraction - inches;
}
let fraction128 = Math.round(rest * 128);
if (fraction128 == 0) return formatImperial(negative, inches);
if (fraction128 % 64 == 0)
return formatImperial(negative, inches, fraction128 / 64, 2);
if (fraction128 % 32 == 0)
return formatImperial(negative, inches, fraction128 / 32, 4);
if (fraction128 % 16 == 0)
return formatImperial(negative, inches, fraction128 / 16, 8);
if (fraction128 % 8 == 0)
return formatImperial(negative, inches, fraction128 / 8, 16);
if (fraction128 % 4 == 0)
return formatImperial(negative, inches, fraction128 / 4, 32);
if (fraction128 % 2 == 0)
return formatImperial(negative, inches, fraction128 / 2, 64);
return negative + fraction;
} else {
if (format === "html") return roundMm(val / 10) + "cm";
else return roundMm(val / 10);
}
};
export default formatMm;

View file

@ -0,0 +1,29 @@
import defaultGist from "./defaultGist";
import optionDefault from "./optionDefault";
const gistDefaults = (config, gist = false) => {
if (!gist) gist = defaultGist;
let options = {};
for (let option of Object.keys(config.options)) {
if (
typeof gist.options !== "undefined" &&
typeof gist.options[option] !== undefined
)
options[option] = gist.options[option];
else options[option] = optionDefault(config.options[option]);
}
delete gist.options;
let settings = JSON.parse(JSON.stringify(defaultGist.settings));
delete settings.locale;
delete settings.units;
for (let setting of Object.keys(settings)) {
if (typeof gist.settings[setting] !== "undefined") {
settings[setting] = gist.settings[setting];
}
}
settings.options = options;
return settings;
};
export default gistDefaults;

View file

@ -0,0 +1,14 @@
export { default as smallestImperialStep } from "./smallestImperialStep";
export { default as roundMm } from "./roundMm";
export { default as roundMmDown } from "./roundMmDown";
export { default as roundMmUp } from "./roundMmUp";
export { default as formatImperial } from "./formatImperial";
export { default as formatMm } from "./formatMm";
export { default as defaultSa } from "./defaultSa";
export { default as sliderStep } from "./sliderStep";
export { default as optionType } from "./optionType";
export { default as defaultGist } from "./defaultGist";
export { default as gistDefaults } from "./gistDefaults";
export { default as optionDefault } from "./optionDefault";
export { default as storage } from "./storage";
export { default as cloneObject } from "./cloneObject";

View file

@ -0,0 +1,17 @@
import optionType from "./optionType";
const optionDefault = option => {
let type = optionType(option);
switch (optionType(option)) {
case "constant":
return option;
break;
case "list":
return option.dflt;
break;
default:
return option[type];
}
};
export default optionDefault;

View file

@ -0,0 +1,15 @@
const optionType = option => {
if (typeof option === "object") {
if (typeof option.pct !== "undefined") return "pct";
if (typeof option.mm !== "undefined") return "mm";
if (typeof option.deg !== "undefined") return "deg";
if (typeof option.count !== "undefined") return "count";
if (typeof option.bool !== "undefined") return "bool";
if (typeof option.list !== "undefined") return "list";
return "unknown";
}
return "constant";
};
export default optionType;

View file

@ -0,0 +1,8 @@
const roundMm = (val, units) => {
if (units === "imperial")
return Math.round(val * 1000000) / 1000000;
else
return Math.round(val * 10) / 10;
};
export default roundMm;

View file

@ -0,0 +1,10 @@
import smallestImperialStep from "./smallestImperialStep";
const roundMmDown = (val, units) => {
if (units === "imperial")
return val - (val % smallestImperialStep);
else
return Math.floor(val * 10) / 10;
};
export default roundMmDown;

View file

@ -0,0 +1,10 @@
import smallestImperialStep from "./smallestImperialStep";
const roundMmUp = (val, units) => {
if (units === "imperial")
return val - (val % smallestImperialStep);
else
return Math.ceil(val * 10) / 10;
};
export default roundMmUp;

View file

@ -0,0 +1,6 @@
const sliderStep = {
metric: 0.1,
imperial: 0.396875
};
export default sliderStep;

View file

@ -0,0 +1,3 @@
const smallestImperialStep = 0.396875;
export default smallestImperialStep;

View file

@ -0,0 +1,24 @@
const storage = {
set: (key, value, raw) => {
if (typeof localStorage === "undefined")
throw(new Error("No localStorage support. And we need it. Bailing out."));
const _key = "fs_" + key;
if (typeof value === "undefined" || value === null)
localStorage.removeItem(_key);
else localStorage.setItem(_key, raw ? value : JSON.stringify(value));
return value;
},
get: (key, raw) => {
if (typeof localStorage === "undefined")
throw(new Error("No localStorage support. And we need it. Bailing out."));
const value = localStorage.getItem("fs_"+key);
return raw ? value : JSON.parse(value);
}
}
export default storage;