@shopify/app-bridge-core
Version:
**[Join our team and work on libraries like this one.](https://www.shopify.ca/careers)**
123 lines (122 loc) • 4.44 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.NonSnakeCaseGroup = void 0;
exports.actionWrapper = actionWrapper;
exports.getEventNameSpace = getEventNameSpace;
exports.findMatchInEnum = findMatchInEnum;
exports.getMergedProps = getMergedProps;
exports.forEachInEnum = forEachInEnum;
exports.isValidOptionalNumber = isValidOptionalNumber;
exports.isValidOptionalString = isValidOptionalString;
exports.updateActionFromPayload = updateActionFromPayload;
var merge_1 = __importDefault(require("./merge"));
var constants_1 = require("./constants");
var types_1 = require("./types");
function actionWrapper(action) {
return action;
}
exports.NonSnakeCaseGroup = [
types_1.Group.AuthCode,
types_1.Group.Button,
types_1.Group.ButtonGroup,
types_1.Group.Cart,
types_1.Group.Error,
types_1.Group.Features,
types_1.Group.Fullscreen,
types_1.Group.Link,
types_1.Group.Loading,
types_1.Group.Menu,
types_1.Group.Modal,
types_1.Group.Navigation,
types_1.Group.Pos,
types_1.Group.Print,
types_1.Group.ResourcePicker,
types_1.Group.Scanner,
types_1.Group.SessionToken,
types_1.Group.Share,
types_1.Group.TitleBar,
types_1.Group.Toast,
types_1.Group.unstable_Picker,
];
function camelCaseToSnakeCase(value) {
return value.replace(/([A-Z])/g, function (matcher, _val, index) {
return "".concat(index === 0 ? '' : '_').concat(matcher[0].toLowerCase());
});
}
/**
* Maps the group name to its event name
* @internal
* @remarks - This method is necessary for the new pattern of using snake case
* which makes it more readable and easier to reconstruct the group from an event name.
* Example: `ContextualSaveBar` becomes `CONTEXTUAL_SAVE_BAR`
* */
function groupToEventNameSpace(group) {
if (exports.NonSnakeCaseGroup.includes(group)) {
return group.toUpperCase();
}
return camelCaseToSnakeCase(group).toUpperCase();
}
/**
* Returns full event name with prefix, group, subgroups and type formatted with separators
* @internal
* */
function getEventNameSpace(group, eventName, component) {
if (eventName.startsWith("".concat(constants_1.PREFIX).concat(constants_1.SEPARATOR))) {
return eventName;
}
var eventNameSpace = groupToEventNameSpace(group);
if (component) {
var subgroups_1 = component.subgroups, type = component.type;
if (subgroups_1 && subgroups_1.length > 0) {
eventNameSpace += eventNameSpace.length > 0 ? constants_1.SEPARATOR : '';
subgroups_1.forEach(function (subgroup, index) {
eventNameSpace += "".concat(subgroup.toUpperCase()).concat(index < subgroups_1.length - 1 ? constants_1.SEPARATOR : '');
});
}
if (type !== group && type) {
eventNameSpace += "".concat(eventNameSpace.length > 0 ? constants_1.SEPARATOR : '').concat(type.toUpperCase());
}
}
if (eventNameSpace) {
eventNameSpace += "".concat(eventNameSpace.length > 0 ? constants_1.SEPARATOR : '').concat(eventName.toUpperCase());
}
return "".concat(constants_1.PREFIX).concat(constants_1.SEPARATOR).concat(eventNameSpace);
}
function findMatchInEnum(types, lookup) {
var match = Object.keys(types).find(function (key) {
return lookup === types[key];
});
return match ? types[match] : undefined;
}
function getMergedProps(props, newProps) {
var merged = (0, merge_1.default)(props, newProps);
if (!merged) {
// tslint:disable-next-line:prefer-object-spread
var cloned = Object.assign(props, newProps);
return cloned;
}
return merged;
}
function forEachInEnum(types, callback) {
Object.keys(types).forEach(function (key) {
callback(types[key]);
});
}
function isValidOptionalNumber(value) {
return value === null || value === undefined || typeof value === 'number';
}
function isValidOptionalString(value) {
return value === null || value === undefined || typeof value === 'string';
}
function updateActionFromPayload(action, newProps) {
var id = action.id;
if (id === newProps.id) {
// Merge new properties
Object.assign(action, getMergedProps(action, newProps));
return true;
}
return false;
}