google-ads-api
Version:
Google Ads API Client Library for Node.js
122 lines (121 loc) • 4.33 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
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 long_1 = __importDefault(require("long"));
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,
// eslint-disable-next-line @typescript-eslint/ban-types
ancestors = new Set()) {
if (ancestors.has(data)) {
throw new Error("Cannot build a field mask from an object containing a circular reference");
}
ancestors.add(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) &&
!ArrayBuffer.isView(value) &&
!long_1.default.isLong(value) &&
value !== null) {
if (Object.keys(value).length === 0) {
paths.push(fieldKey);
}
else {
const children = recursiveFieldMaskSearch(value, ancestors);
for (const child of children) {
paths.push(`${fieldKey}.${child}`);
}
}
continue;
}
paths.push(fieldKey);
}
ancestors.delete(data);
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,
});
}