httpsnippet-lite
Version:
HTTP Request snippet generator for *most* languages
60 lines (59 loc) • 2.46 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.literalRepresentation = exports.nsDeclaration = void 0;
/**
* Create a string corresponding to a valid declaration and initialization of an Objective-C object literal.
*
* @param nsClass Class of the litteral
* @param name Desired name of the instance
* @param parameters Key-value object of parameters to translate to an Objective-C object litearal
* @param indent If true, will declare the litteral by indenting each new key/value pair.
* @return A valid Objective-C declaration and initialization of an Objective-C object litteral.
*
* @example
* nsDeclaration('NSDictionary', 'params', {a: 'b', c: 'd'}, true)
* // returns:
* NSDictionary *params = @{ @"a": @"b",
* @"c": @"d" };
*
* nsDeclaration('NSDictionary', 'params', {a: 'b', c: 'd'})
* // returns:
* NSDictionary *params = @{ @"a": @"b", @"c": @"d" };
*/
const nsDeclaration = (nsClass, name, parameters, indent) => {
const opening = `${nsClass} *${name} = `;
const literal = (0, exports.literalRepresentation)(parameters, indent ? opening.length : undefined);
return `${opening}${literal};`;
};
exports.nsDeclaration = nsDeclaration;
/**
* Create a valid Objective-C string of a literal value according to its type.
*
* @param value Any JavaScript literal
*/
const literalRepresentation = (value, indentation) => {
const join = indentation === undefined ? ', ' : `,\n ${' '.repeat(indentation)}`;
switch (Object.prototype.toString.call(value)) {
case '[object Number]':
return `@${value}`;
case '[object Array]': {
const valuesRepresentation = value.map((value) => (0, exports.literalRepresentation)(value));
return `@[ ${valuesRepresentation.join(join)} ]`;
}
case '[object Object]': {
const keyValuePairs = [];
for (const key in value) {
keyValuePairs.push(`@"${key}": ${(0, exports.literalRepresentation)(value[key])}`);
}
return `@{ ${keyValuePairs.join(join)} }`;
}
case '[object Boolean]':
return value ? '@YES' : '@NO';
default:
if (value === null || value === undefined) {
return '';
}
return `@"${value.toString().replace(/"/g, '\\"')}"`;
}
};
exports.literalRepresentation = literalRepresentation;