@bianic-ui/utils
Version:
Common utilties and types for Bianic UI
67 lines (57 loc) • 1.43 kB
JavaScript
import { isArray, isObject } from "./assertion";
import { objectKeys } from "./object";
import { getLastItem } from "./array";
export var breakpoints = Object.freeze(["base", "sm", "md", "lg", "xl"]);
export function mapResponsive(prop, mapper) {
if (isArray(prop)) {
return prop.map(item => {
if (item === null) {
return null;
}
return mapper(item);
});
}
if (isObject(prop)) {
return objectKeys(prop).reduce((result, key) => {
result[key] = mapper(prop[key]);
return result;
}, {});
}
if (prop != null) {
return mapper(prop);
}
return null;
}
export function objectToArrayNotation(obj, bps) {
if (bps === void 0) {
bps = breakpoints;
}
var result = bps.map(br => {
var _obj$br;
return (_obj$br = obj[br]) != null ? _obj$br : null;
});
while (getLastItem(result) === null) {
result.pop();
}
return result;
}
export function arrayToObjectNotation(values, bps) {
if (bps === void 0) {
bps = breakpoints;
}
var result = {};
values.forEach((value, index) => {
var key = bps[index];
if (value == null) return;
result[key] = value;
});
return result;
}
export function isResponsiveObjectLike(obj, bps) {
if (bps === void 0) {
bps = breakpoints;
}
var keys = Object.keys(obj);
return keys.length > 0 && keys.every(key => bps.includes(key));
}
//# sourceMappingURL=responsive.js.map