|
| 1 | +'use strict' |
| 2 | + |
| 3 | +var util = require('util') |
| 4 | + |
| 5 | +/** |
| 6 | + * Create an string of given length filled with blank spaces |
| 7 | + * |
| 8 | + * @param {number} length Length of the array to return |
| 9 | + * @return {string} |
| 10 | + */ |
| 11 | +function buildString (length, str) { |
| 12 | + return Array.apply(null, new Array(length)).map(String.prototype.valueOf, str).join('') |
| 13 | +} |
| 14 | + |
| 15 | +/** |
| 16 | + * Create a string corresponding to a Dictionary or Array literal representation with pretty option |
| 17 | + * and indentation. |
| 18 | + */ |
| 19 | +function concatArray (arr, pretty, indentation, indentLevel) { |
| 20 | + var currentIndent = buildString(indentLevel, indentation) |
| 21 | + var closingBraceIndent = buildString(indentLevel - 1, indentation) |
| 22 | + var join = pretty ? ',\n' + currentIndent : ', ' |
| 23 | + |
| 24 | + if (pretty) { |
| 25 | + return '[\n' + currentIndent + arr.join(join) + '\n' + closingBraceIndent + ']' |
| 26 | + } else { |
| 27 | + return '[' + arr.join(join) + ']' |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +module.exports = { |
| 32 | + /** |
| 33 | + * Create a string corresponding to a valid declaration and initialization of a Swift array or dictionary literal |
| 34 | + * |
| 35 | + * @param {string} name Desired name of the instance |
| 36 | + * @param {Object} parameters Key-value object of parameters to translate to a Swift object litearal |
| 37 | + * @param {Object} opts Target options |
| 38 | + * @return {string} |
| 39 | + */ |
| 40 | + literalDeclaration: function (name, parameters, opts) { |
| 41 | + return util.format('let %s = %s', name, this.literalRepresentation(parameters, opts)) |
| 42 | + }, |
| 43 | + |
| 44 | + /** |
| 45 | + * Create a valid Swift string of a literal value according to its type. |
| 46 | + * |
| 47 | + * @param {*} value Any JavaScript literal |
| 48 | + * @param {Object} opts Target options |
| 49 | + * @return {string} |
| 50 | + */ |
| 51 | + literalRepresentation: function (value, opts, indentLevel) { |
| 52 | + indentLevel = indentLevel === undefined ? 1 : indentLevel + 1 |
| 53 | + |
| 54 | + switch (Object.prototype.toString.call(value)) { |
| 55 | + case '[object Number]': |
| 56 | + return value |
| 57 | + case '[object Array]': |
| 58 | + // Don't prettify arrays nto not take too much space |
| 59 | + var pretty = false |
| 60 | + var valuesRepresentation = value.map(function (v) { |
| 61 | + // Switch to prettify if the value is a dictionary with multiple keys |
| 62 | + if (Object.prototype.toString.call(v) === '[object Object]') { |
| 63 | + pretty = Object.keys(v).length > 1 |
| 64 | + } |
| 65 | + return this.literalRepresentation(v, opts, indentLevel) |
| 66 | + }.bind(this)) |
| 67 | + return concatArray(valuesRepresentation, pretty, opts.indent, indentLevel) |
| 68 | + case '[object Object]': |
| 69 | + var keyValuePairs = [] |
| 70 | + for (var k in value) { |
| 71 | + keyValuePairs.push(util.format('"%s": %s', k, this.literalRepresentation(value[k], opts, indentLevel))) |
| 72 | + } |
| 73 | + return concatArray(keyValuePairs, opts.pretty && keyValuePairs.length > 1, opts.indent, indentLevel) |
| 74 | + default: |
| 75 | + return '"' + value.replace(/"/g, '\\"') + '"' |
| 76 | + } |
| 77 | + }, |
| 78 | + |
| 79 | + /** |
| 80 | + * By appending multipart parameters one by one in the resulting snippet, |
| 81 | + * we make it easier for the user to edit it according to his or her needs after pasting. |
| 82 | + * The user can just edit the parameters NSDictionary or put this part of a snippet in a multipart builder method. |
| 83 | + */ |
| 84 | + multipartBody: function (source, opts) { |
| 85 | + var code = [] |
| 86 | + var indent = opts.indent |
| 87 | + |
| 88 | + code.push(this.literalDeclaration('parameters', source.postData.params, opts)) |
| 89 | + code.push(null) |
| 90 | + code.push(util.format('let boundary = "%s"', source.postData.boundary)) |
| 91 | + code.push(null) |
| 92 | + code.push('var body = ""') |
| 93 | + code.push('var error: NSError? = nil') |
| 94 | + code.push('for param in parameters {') |
| 95 | + code.push(indent + 'let paramName = param["name"]!') |
| 96 | + code.push(indent + 'body += "--\\(boundary)\\r\\n"') |
| 97 | + code.push(indent + 'body += "Content-Disposition:form-data; name=\\"\\(paramName)\\""') |
| 98 | + code.push(indent + 'if let filename = param["fileName"] {') |
| 99 | + code.push(indent + indent + 'let contentType = param["content-type"]!') |
| 100 | + code.push(indent + indent + 'let fileContent = String(contentsOfFile: filename, encoding: NSUTF8StringEncoding, error: &error)') |
| 101 | + code.push(indent + indent + 'if (error != nil) {') |
| 102 | + code.push(indent + indent + indent + 'println(error)') |
| 103 | + code.push(indent + indent + '}') |
| 104 | + code.push(indent + indent + 'body += "; filename=\\"\\(filename)\\"\\r\\n"') |
| 105 | + code.push(indent + indent + 'body += "Content-Type: \\(contentType)\\r\\n\\r\\n"') |
| 106 | + code.push(indent + indent + 'body += fileContent!') |
| 107 | + code.push(indent + '} else if let paramValue = param["value"] {') |
| 108 | + code.push(indent + indent + 'body += "\\r\\n\\r\\n\\(paramValue)"') |
| 109 | + code.push(indent + '}') |
| 110 | + code.push('}') |
| 111 | + |
| 112 | + return code.join('\n') |
| 113 | + } |
| 114 | +} |
0 commit comments