google-ads-api
Version:
Google Ads API Client Library for Node.js
102 lines (101 loc) • 3.6 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.fromMicros = fromMicros;
exports.toMicros = toMicros;
exports.normaliseQuery = normaliseQuery;
exports.capitaliseFirstLetter = capitaliseFirstLetter;
exports.toCamelCase = toCamelCase;
exports.toSnakeCase = toSnakeCase;
exports.recursiveFieldMaskSearch = recursiveFieldMaskSearch;
exports.getFieldMask = getFieldMask;
const protos_1 = require("./protos");
/**
* @param micros Money value in micros format
* @description Converts a micro amount to a normalised value
* @example
* const cost = fromMicros(campaign.metrics.costMicros)
*/
function fromMicros(micros) {
return micros / 1000000;
}
/**
* @param value Money value in decimal format
* @description Converts a number to micro format
* @example
* const costMicros = toMicros(12.5) // 12,500,000
*/
function toMicros(value) {
return Math.round(value * 1000000);
}
/**
* @param query String gaql query
* @description Normalises a query by replacing multiple whitespace with single whitespace
* @example
* const gaqlQuery =
* `SELECT campaign.name
* FROM campaign
* LIMIT 10`
* const normalisedQuery = normaliseQuery(gaqlQuery) // "SELECT campaign.name FROM campaign LIMIT 10"
*/
function normaliseQuery(query) {
return `${query.replace(/\s{2,}/g, " ")}`;
}
/**
* @param str
* @description Capitalises the first letter of a string
* @example
* const capitalisedString = capitaliseFirstLetter("adGroupCriterionSimulation") // "AdGroupCriterionSimulation"
*/
function capitaliseFirstLetter(str) {
return (str.charAt(0).toUpperCase() + str.slice(1));
}
/**
* @param str
* @description Converts a string to a camel case string. Works on space case, snake case and title case strings.
* @example
* const camelCaseString = toCamelCase("ad_group_criterion_simulation") // "adGroupCriterionSimulation"
*/
function toCamelCase(str) {
return str
.replace(/\s+/g, "_") // convert spaces to underscores
.replace(/(_)([A-Za-z])/g, (pattern) => pattern[1].toUpperCase()) // replace "_x" patterns with "X"
.replace(/^([A-Z])/g, (pattern) => pattern.toLowerCase()); // capitalises the first letter of the string
}
/**
* @param str
* @description Converts a string to a snake case string. Works on space case, camel case and title case strings.
* @example
* const snakeCaseString = toSnakeCase("adGroupCriterionSimulation") // "ad_group_criterion_simulation"
*/
function toSnakeCase(str) {
return str
.replace(/\s+/g, "_") // convert spaces to underscores
.replace(/(?<!(^|_))([A-Z])/g, (pattern) => "_" + pattern) // places an underscore before any capital chars, unless there is already an underscore or it is the first char of the string
.toLowerCase();
}
function recursiveFieldMaskSearch(data) {
const paths = [];
for (const key of Object.keys(data)) {
if (key === "resourceName") {
continue;
}
const fieldKey = toSnakeCase(key);
const value = data[key];
if (typeof value === "object" && !Array.isArray(value) && value !== null) {
const children = recursiveFieldMaskSearch(value);
for (const child of children) {
paths.push(`${fieldKey}.${child}`);
}
continue;
}
paths.push(fieldKey);
}
return paths;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function getFieldMask(data) {
const paths = recursiveFieldMaskSearch(data);
return new protos_1.protobuf.FieldMask({
paths,
});
}