convert get of lodash javascript library to optional chaining - lodash

I wanted to change this code which is using get method from lodash library to optional chaining method.Can someone help me to convert this
return get(
this.UIObj,
'excelList',
[]
);

Optional return the excelList property of this.UIObj, and if it's undefined (or null) return an empty array using the Nullish coalescing operator (??):
return this.UIObj?.excelList ?? []

Related

Blade custom directive custom, passing dynamic expression as variable will not evaluate

Is there a way to evaluate a variable in a blade directive? I try to get the value of an associative array in function of dynamically passed arguments over directive
Having the following will fail
Blade::directive('imagepol', function ($expression) {
$options = get_field('fallback_image', 'option');
if(!empty($expression) && $options) {
return "<?= isset($options[$expression]) ? wp_get_attachment_image_srcset($options[$expression]['ID']) : ''?>";
}
});
I call the directive as it follows
#imagepol($item->get('pattern_options'))
In this case will fail with the following
Undefined array key "$item->get('pattern_options')"

In a Postman pre-request-script, how can I read the actual value of a header that uses a variable

I have a variable called token with a specific value myTokenValue
I try to make a call that includes that variable in a header, tokenHeader:{{token}}
I also have a pre-request-script that needs to change the request based on the value of the token header, but if I try to read the value pm.request.headers.get('tokenHeader') I get the literal value {{token}} instead of the interpolated myTokenValue
How do I get this value without having to look at the variable directly?
You can use the following function to replace any Postman variables in a string with their resolved values:
var resolveVariables = s => s.replace(/\{\{([^}]+)\}\}/g,
(match, capture) => pm.variables.get(capture));
In your example:
var token = resolveVariables(pm.request.headers.get('tokenHeader'));
Basically I was missing a function to interpolate a string, injecting variables from the environment
There are some workarounds:
write your own function, as in this comment by pomeh
function interpolate (value) {
return value.replace(/{{([^}]+)}}/g, function (match, $1) {
return pm.variables.get($1);
});
}
use Postman's own replaceSubstitutions, as in this comment by codenirvana
function interpolate (value) {
const {Property} = require('postman-collection');
let resolved = Property.replaceSubstitutions(value, pm.variables.toObject());
}
Either of these can be used as
const tokenHeader = interpolate(pm.request.headers.get('tokenHeader'));
but the second one is also null safe.

Empty object with union type in graphQL

When I' using union type in my graphQL schema I use it typically like this:
const documentTypeDefs = gql`
union TestType = TypeExample1 | TypeExample2
type Document {
exampleKey: TestType
}
`
Then I resolve it like this:
TestType: {
__resolveType(obj) {
if(obj.property1) {
return 'TypeExample1';
}
if(obj.property2) {
return 'TypeExample2';
}
return null;
},
}
But sometimes I'm getting empty object in my resolving function (ie. obj is {}). I thought returning null or undefined will do the job but unfortunately I'm getting error:
"Abstract type ItemsType must resolve to an Object type at runtime for field Document.exampleKey with value {}, received \"{}\". Either the ItemsType type should provide a \"resolveType\" function or each possible type should provide an \"isTypeOf\" function."
How can I resolve empty object then?
Thank you!
If an empty object is being passed to __resolveType, that means your field is resolving to an empty object. That means either the value being returned inside your resolver is an empty object, or else the Promise returned is resolving to one.
If you're working with a field that returns a List, it's also possible for just one of the items being returned to be an empty object. This is particularly likely when working with MongoDB if one of the documents you're getting is actually empty or at least missing the fields you've specified in your mongoose schema.

String with variable inside that can dynamically change

I'm trying to setup an API in golang, for specific needs, I want to be able to have an environment variable that would contain an URL as string (i.e : "https://subdomain.api.com/version/query") and I want to be able to modify the bold parts within an API call.
I have no clue on how I could achieve this.
Thanks for your time,
Paul
There are many ways, one which allows the URL to be configured from the environment, then to have the url configured dynamically at runtime, would be to use a template.
You could expect a template from the ENV:
apiUrlFromEnv := "https://{{.Subdomin}}.api.com/{{.Version}}/query" // get from env
Modified From the docs:
type API struct {
Subdomain string
Version string
}
api := API{"testapi", "1.1"}
tmpl, err := template.New("api").Parse(apiUrlFromEnv)
if err != nil { panic(err) }
err = tmpl.Execute(os.Stdout, api) // write to buffer so you can get a string?
if err != nil { panic(err) }
The simplest way is to use fmt.Sprintf.
fmt.Sprintf(format string, a ...interface{}) string
As you see this function returns a new formatted string and this is built-in library. Furthermore you can use indexing to place arguments in a template:
In Printf, Sprintf, and Fprintf, the default behavior is for each formatting verb to format successive arguments passed in the call. However, the notation [n] immediately before the verb indicates that the nth one-indexed argument is to be formatted instead.
fmt.Sprintf("%[2]d %[1]d %d[2]\n", 11, 22)
But if you want to use named variables you should use text/template package.

JSON result problems in ASP.NET Web API when returning value types?

I'm learning aspnet mvc 4 web api, and find it very easy to implement by simply returning the object in the apicontrollers.
However, when I try to return value types such as bool, int, string - it does not return in JSON format at all. (in Fiddler it showed 'true/false' result in raw and webview but no content in JSON at all.
Anyone can help me on this?
Thanks.
Some sample code for the TestApiController:
public bool IsAuthenticated(string username)
{
return false;
}
Some sample code for the jQuery usage:
function isAuthenticated(string username){
$.getJSON(OEliteAPIs.ApiUrl + "/api/membership/isauthenticated?username="+username,
function (data) {
alert(data);
if (data)
return true;
else
return false;
});
}
NOTE: the jquery above returns nothing because EMPTY content was returned - however if you check it in fiddler you can actually see "false" being returned in the webview.
cheers.
Before your callback function is called, the return data is passed to the jquery parseJSON method, which expects the data to be in the JSON format. jQuery will ignore the response data and return null if the response is not formatted correctly. You have two options, wrap you return boolean in a class or anonymous type so that web api will return a JSON object:
return new { isAuthentication = result }
or don't use getJSON from jQuery since you're not returning a properly formatted JSON response. Maybe just use $.get instead.
Below is a quote for the jQuery documentation:
Important: As of jQuery 1.4, if the JSON file contains a syntax error,
the request will usually fail silently. Avoid frequent hand-editing of
JSON data for this reason. JSON is a data-interchange format with
syntax rules that are stricter than those of JavaScript's object
literal notation. For example, all strings represented in JSON,
whether they are properties or values, must be enclosed in
double-quotes. For details on the JSON format, see http://json.org/.