I have a NestJs project where I am trying to generate js files into the CDN blob storage.
For eg I have this object:
{
"prop1":"${resolvedprop1}",
"prop2":"John's pet"
}
content of the generated js file should look like:
var metadata=()=>JSON.parse('{ "prop1":"${resolvedprop1}", "prop2":"John's pet" }')
Code for generation of the file:
function writeToCDN(metadata){
const content = `var metadata = ()=>JSON.parse('${JSON.stringify(metadata)}')`;
//write content string to a file in CDN BLOB.
}
When I load this file into the browser and execute metadata() I get an error when parsing because the single quote is not escaped.
Important note:
I can not use backtick instead of single quote inside JSON.parse since I am expecting ${} in the prop1 value.
Thank you for your help in advance!
Related
I am not sure how can I find the encoding of the file. I am trying to upload the CSV file only with utf-8 and find if there are any non utf-8 characters I want to show an error message.
I am using Papa persor for parsing.
How can I read the encoding of the file either in java or in js.
var fileElement = element.find('.csv-upload');
var file = fileElement[0].files[0]
var parseConfig = {
skipEmptyLines: true,
header: true,
encoding:'UTF-8',
trimHeaders: true,
complete: function (content) {
scope.onFileRead(content, fileElement[0]);
}
};
if (scope.rowsToParse) {
parseConfig.preview = scope.rowsToParse;
}
if (file) {
Papa.parse(file, parseConfig);
}
A CSV file will not have any encoding information in it. It is just a sequence of bytes.
You have to know beforehand whether the file contains non-UTF8 characters, and what is the right file encoding to use when reading it.
If you are using Java (not sure if you are because you have the Spring tag), you could use one of the libraries suggested here to try and infer the file type.
Is there a java library equivalent to file command in unix
Maybe there is something similar for Javascript.
I'd like to use JSON2HTML to parse the HTML data from JSON and render it in an UIWebView (using Swift 3.0). Please let me know how to achieve it. Thanks in advance!
Here's what I've tried:
let jsfile1 = try!String(contentsOfFile: Bundle.main.path(forResource: "json2html", ofType: "js")!)
func loadJS()
{
var getData={}
var context = JSContext()
var valSwiftyJson:JSON = [:]
var test = context?.evaluateScript(jsfile1)
let testFunction = test?.objectForKeyedSubscript("json2html")
let urlString = //Have removed the URL string due to restrictions
Alamofire.request(urlString,encoding:JSONEncoding.default).responseJSON
{ response in
if let alamoJson = response.result.value
{
let swiftyJson = JSON(data:response.data!)
valSwiftyJson = swiftyJson["FormInfo"]["Form"]
print(valSwiftyJson)
}
}
let result = testFunction?.call(withArguments: [getData,valSwiftyJson])
webView.loadHTMLString((result?.toString())!, baseURL: nil)
}
Finally, I managed to solve the issue by creating an index.html file (locally stored) and I referred the JSON2HTML library inside it. I then added the JSON(HTML inside) content dynamically to it each time whenever I needed to convert JSON to HTML. At last I load the final index.html in the UIWebView (it worked like charm).
Are you talking about this library as JSON2HTML ? If so, I don't think there is a library for translating the JSON elements to HTML in Swift.
Do you plan to download the JSON elements from a back-end ? Then, as there is a node.js wrapper to JSON2HTML, I would recommend to do the translating from JSON to HTML on the same server. Thus you would just download the HTML compiled data and rendering it in the UIWebView would be as easy as this line of code (in Swift 3) :
// html is the HTML data downloaded from your back-end
webView.mainFrame.loadHTMLString(html, baseURL: nil)
How to create PDF Documents in Node.JS.? Is there any better solution to manage templates for different types of PDF creation.
I am using PDFKit to create PDF Documents and this will be server side using Javascript. I can not use HTML to create PDF. It will blob of paragraphs and sections with replacing tags with in.
Does anyone know Node.js has any npm package that can deal templates with paragraphs sections headers.
Something like
getTemplateByID() returns a template that contains sections , headers, paragraphs and then i use to replace appropriate tags within the template.
In my case, I have to get my HTML template from my database (PostgreSQL) stocked as stream. I request the db to get my template and I create a tmp file.
Inside my template, I have AngularJS tags so I compile this template with datas thanks to the 'ng-node-compile' module:
var ngCompile = require('ng-node-compile');
var ngEnvironment = new ngCompile();
var templateHTML = getTemplateById(id);
templateHTML = ngEnvironment.$compile(templateHTML)(datas);
Now I have my compiled template (where you can set your paragraph etc.) and I convert them into PDF thanks to a PhantomJS module 'phantom-html-to-pdf'
var phantomHTML2PDF = require('phantom-html-to-pdf')(options);
phantomHTML2PDF(convertOptions, function (error, pdf) {
if(error) console.log(error);
// Here you have 'pdf.stream.path' which is your tmp PDF file
callback(pdf);
});
Now you have your compiled and converted template (pdf), you can do whatever you want ! :)
Useful links:
https://github.com/MoLow/ng-node-compile
https://github.com/pofider/phantom-html-to-pdf
I hope this help !
I have an Javascript (AngularJS) App, and I try to load an language-file, which is converted in "UCS-2 Little Endian".
I have to use this, and are not allowed to convert the file to UTF-8.
Now the problem is, that I can´t read this file in JavaScript.
// console.log(row);
��[�l�a�n�g�_�d�e�_�n�a�m�e�]�,�G�e�r�m�a�n�
Instead this row should be:
[lang_de_name],German
Is there any way to read the file without changing the file? I already tried encodeURIComponent() or escape(), which does not work.
Thank you very much.
Link: http://terenceyim.wordpress.com/tag/ucs2/
What I do in code now:
var csv = UTF8.decode(UTF8.encode(row))
Works.
Hai I am trying to read properties file from jar to my javascript code in mirth environment. I wrote js code in transformer.
I kept jar file in lib folder.
It is showing exception like:
The choice of Java method java.io.FileInputStream matching JavaScript
argument types (null) is ambiguous; candidate methods are:
FileInputStream(java.lang.String) FileInputStream(java.io.File)
FileInputStream(java.io.FileDescriptor)
(com.mirth.connect.server.MirthJavascriptTransformerException)
Below is the code:
importPackage(Packages.logproperties.*);
var prop=new Packages.java.util.Properties();
var fis=new Packages.java.io.FileInputStream(Packages.java.lang.ClassLoader.getSystemResourceAsStream("Logs.properties"));
prop.load(fis);
Based on the error message is looks like you are calling FileInputStream with null.
That means that Packages.java.lang.ClassLoader.getSystemResourceAsStream("Logs.properties") is returning null.
Try using the absolute path to the file that you want to read, e.g.
var fis=new Packages.java.io.FileInputStream("/full/path/to/Logs.properties");