This question already has answers here:
'var' parameters are deprecated and will be removed in Swift 3
(8 answers)
Closed 6 years ago.
I am using a var for IPAddress, for which i wanted to remove trailing slash (/). Now i see a warning that 'var' is deprecated. In such case how can i use removeAtIndex method in new style?
if ipAddress.characters.last == "/" {
ipAddress.removeAtIndex(ipAddress.endIndex.predecessor())
}
Remove var from the function parameter declaration and then create a mutable copy:
func myFunc(ipAddress: String) { // remove the var if you write here var ipAddress
var ipAddress = ipAddress
// change ipAddress here
}
var is only deprecated in function arguments.
See here the original change request:
https://github.com/apple/swift-evolution/blob/master/proposals/0003-remove-var-parameters.md
Related
This question already has answers here:
Configure Node.js to log to a file instead of the console
(28 answers)
Closed 6 months ago.
i want to write console logs into logs.json file and saves it like:
{
"1":"consolelog1",
"2":"consolelog2",
"3":"consolelog3"
}
Using node.js, you can use fs:
const fs = require('fs');
function log_to_file(mystuff){
// GET file
const file = JSON.parse(fs.readFileSync('./logs.json'));
// Find number of console.logs already there
let lognumb = -1, cont = true;
while(cont){
if(!(file[lognumb+1]===undefined)){
lognumb++;
continue
};
cont=false
};
file[lognumb+1] = "consolelog"+mystuff;
fs.writeFileSync("./logs.json",JSON.stringify(file));
}
Then, you can use log_to_file("your_console_log_data") to save your data.
Make Sure You Are Using NODE.JS, Not JAVASCRIPT
This question already has answers here:
using process.env in TypeScript
(19 answers)
Closed 1 year ago.
I'm trying to make a simple api using typescript and when I use any env variable I get an error from TS compiler Tells me that this could be undefined
example
// Not Working
const db = process.env.DB_URL // This gives an error that the result could be a string or undefined
to fix this
I have to make a type guard and check with if statement as follows
const db = process.env.DB_URL
if (db){
// ....
}
Is there a better approach to to such a thing instead of explicitly check for every variable ?
You can apply null check and keep it as string instead of defining two types:
const db: string = process.env.DB_URL ?? ''
// empty strings are falsy/falsey
if (db) { // do this}
else { //do this}
This question already has answers here:
How do I get the path to the current script with Node.js?
(15 answers)
Closed 3 years ago.
If you have a module that's being imported in some code, let's say...
var my_cool_module = require('my_directory/my_cool_module');
my_cool_module.print_directory_name();
And I'm in that module's context, say this is the file my_cool_module.js...
function get_dir_name() {
// get the directory name...
return directory_name;
}
exports.module.print_directory_name = function() {
console.log("This module is in directory " + get_dir_name());
};
How can I get the directory that the module is in (i.e. "my_directory")
I found out from the node.js documentation here that you can use __dirname
function get_directory_name() {
return __dirname;
}
This question already has answers here:
How to use JavaScript regex over multiple lines?
(8 answers)
Closed 4 years ago.
Looking to scrape the comments out of a JS file. Was thinking I can create a function to input a .js file, perform a RegExp match, and output an array of strings using fs.readFile() and string.match();
Here's an over-simplified example:
I have two files class.js (to read) and parse.js (to perform the text parsing)
class.js:
/*
by: Mike Freudiger
*/
/**
* one
* #returns 'Hello World'
*/
function one () {
return 'Hello World';
}
alert();
/* end of file */
parse.js:
var fs = require('fs');
var file = fs.readFile('C:\\Users\\mikef\\Desktop\\node_regex_test\\class.js', 'utf8', function(err, doc) {
var comments = doc.match(/(\/\*\*(.|\n)+?\*\/)/g);
console.log(comments);
});
when I run node parse.js the console output is null.
However when I run the regex match on a multiline string, I get the expected output:
var doc = `/*
by: Mike Freudiger
*/
/**
* one
* #returns 'Hello World'
*/
function one () {
return 'Hello World';
}
alert();
/* end of file */`
Any idea why the readFile() string would behave differently than a string literal?
...Also, I realize there may be a better way to get these comments out, with another npm package or something, but now I really just want to know why these two strings are different.
As mentioned by vsemozhetbyt, it seems that newlines used in class.js file are either \r\n or \r.
One of the simplest (and fastest) way to match these newlines would be to use [\s\S] instead of (.|\n) in your regex.
Thus you get:
var fs = require('fs');
var file = fs.readFile('C:\\Users\\mikef\\Desktop\\node_regex_test\\class.js', 'utf8', function(err, doc) {
var comments = doc.match(/(\/\*\*[\s\S]+?\*\/)/g);
console.log(comments);
});
This question already has answers here:
What is the scope of variables in JavaScript?
(27 answers)
Closed 5 years ago.
first();
function first(){
second();
third();
}
function second(){
var b='second';
}
function third(){
console.log(b);
}
Getting error while trying to access variable b in third(), can you please help
console.log(b);
^
ReferenceError: b is not defined
if you want to access b than you have to define it as global
first();
var b;
function first(){
second();
third();
}
function second(){
b='second';
}
function third(){
console.log(b);
}
console.log(b);
look my bin global b