How to import library in DartPad? - dart

I wrote the below code in Dartpad and it return me this error:
Error compiling to JavaScript: unsupported import:
package:collection/collection.dart
import "package:collection/collection.dart";
main(List<String> args) {
var data = [
{"title": 'Avengers', "release_date": '10/01/2019'},
{"title": 'Creed', "release_date": '10/01/2019'},
{"title": 'Jumanji', "release_date": '30/10/2019'},
];
var newMap = groupBy(data, (obj) =>
(obj as dynamic)['release_date']
);
print(newMap);
}
Is there any way to resolve it?

It appears that importing packages is not currently supported. There's an open issue request this here:
https://github.com/dart-lang/dart-pad/issues/901

After waiting... It seems that the issue has solved by the new update of dartpad.dev website.

Related

Google audio to text API but return null

I have send below JSON request but I am getting below error.
JSON Request:-
{
"config": {
"enableAutomaticPunctuation": "true",
"encoding": "MULAW",
"languageCode": "en-US",
"model": "default",
"sampleRateHertz": 8000
},
"audio": {
"content": "QzpcU3BlZWNoVG9UZXh0XGVuZ2xpc2hcUENNXDIud2F2"
}
}
Output: null
Method for Encoding of wav file as given below
byte[] encodedAudio = Base64.encodeBase64(pcmFilePath.getBytes());
String s = new String(encodedAudio);
If pcmFilePath is a String, then pcmFilePath.getBytes() will return the path to the file. Therefore, encodedAudio will contain the path to the file, not the encoded audio.
One way to get the contents of the file would be to use java.nio.file.Files.readAllBytes() introduced in JDK 7.
import java.nio.file.Files;
import java.nio.file.Paths;
byte[] pcmBytes = Files.readAllBytes(Paths.get(pcmFilePath));
byte[] encodedAudio = Base64.encodeBase64(pcmBytes);

Lua script fails but the JS works in console

I have this very basic lua script that returns an error, but running the querySelector directly in the console works just fine.
Any hints on what is wrong with my lua?
function main(splash, args)
assert(splash:go(args.url))
assert(splash:wait(1))
assert(splash:runjs('document.querySelector("button.btn.btn-primary.btn-show-rates").click()'))
splash:set_viewport_full()
return {
html = splash:html(),
}
end
BTW: website is here
Error log:
{
"type": "ScriptError",
"error": 400,
"info": {
"type": "LUA_ERROR",
"line_number": 4,
"error": "JS error: 'TypeError: null is not an object (evaluating \\'document.querySelector(\"button.btn.btn-primary.btn-show-rates\").click\\')'",
"message": "Lua error: [string \"function main(splash, args)\r...\"]:4: JS error: 'TypeError: null is not an object (evaluating \\'document.querySelector(\"button.btn.btn-primary.btn-show-rates\").click\\')'",
"source": "[string \"function main(splash, args)\r...\"]"
},
"description": "Error happened while executing Lua script"
}
Turns out it was kinda simple...
A slightly longer "wait" and changing to document.querySelectorAll made it work. Final code here:
function main(splash, args)
assert(splash:go(args.url))
assert(splash:wait(5.0))
assert(splash:runjs('document.querySelectorAll("button.btn.btn-primary.btn-show-rates")[0].click()'))
assert(splash:runjs('document.querySelectorAll("button.btn.btn-primary.btn-show-rates")[1].click()'))
assert(splash:runjs('document.querySelectorAll("button.btn.btn-primary.btn-show-rates")[2].click()'))
splash:set_viewport_full()
return {
html = splash:html(),
}
end

Is it possible to configure a format provider for the File sink using JSON configuration?

In the documentation for the Serilog.Sinks.File a JSON formatter is used:
// Install-Package Serilog.Formatting.Compact
.WriteTo.File(new CompactJsonFormatter(), "log.txt")
Is it possible to do the same using JSON configuration?
Yes, in the Args object, specify the full type name of the formatter:
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "log.txt",
"formatter": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact"
}
}
]

How to use CustomElement v1 polyfill in a webpack/babel build?

I'm having some trouble getting this WebComponents polyfill + native-shim to work right across all devices, though webpack.
Some background on my setup:
* Webpack2 + babel-6
* app is written in ES6, transpiling to ES5
* imports a node_module package written in ES6, which defines/registers a CustomElement used in the app
So the relevant webpack dev config looks something like this:
const config = webpackMerge(baseConfig, {
entry: [
'webpack/hot/only-dev-server',
'#webcomponents/custom-elements/src/native-shim',
'#webcomponents/custom-elements',
'<module that uses CustomElements>/dist/src/main',
'./src/client',
],
output: {
path: path.resolve(__dirname, './../dist/assets/'),
filename: 'app.js',
},
module: {
rules: [
{
test: /\.js$/,
loader: 'babel-loader',
options: {
cacheDirectory: true,
},
include: [
path.join(NODE_MODULES_DIR, '<module that uses CustomElements>'),
path.join(__dirname, '../src'),
],
},
],
},
...
key take aways:
* I need CustomElement poly loaded before <module that uses CustomElements>
* I need <module that uses CustomElements> loaded before my app soure
* <module that uses CustomElements> is ES6 so we're transpiling it ( thus the include in the babel-loader).
The above works as-expected in modern ES6 browsers ( IE desktop Chrome ), HOWEVER
it does not work in older browsers. I get the following error in older browsers, for example iOS 8:
SyntaxError: Unexpected token ')'
pointing to the opening anonymous function in the native-shim pollyfill:
(() => {
'use strict';
// Do nothing if `customElements` does not exist.
if (!window.customElements) return;
const NativeHTMLElement = window.HTMLElement;
const nativeDefine = window.customElements.define;
const nativeGet = window.customElements.get;
So it seems to me like the native-shim would need to be transpiled to ES5:
include: [
+ path.join(NODE_MODULES_DIR, '#webcomponents/custom-elements/src/native-shim'),
path.join(NODE_MODULES_DIR, '<module that uses CustomElements>'),
path.join(__dirname, '../src'),
],
...but doing so now breaks both Chrome and iOS 8 with the following error:
app.js:1 Uncaught TypeError: Failed to construct 'HTMLElement': Please use the 'new' operator, this DOM object constructor cannot be called as a function.
at new StandInElement (native-shim.js:122)
at HTMLDocument.createElement (<anonymous>:1:1545)
at ReactDOMComponent.mountComponent (ReactDOMComponent.js:504)
at Object.mountComponent (ReactReconciler.js:46)
at ReactCompositeComponentWrapper.performInitialMount (ReactCompositeComponent.js:371)
at ReactCompositeComponentWrapper.mountComponent (ReactCompositeComponent.js:258)
at Object.mountComponent (ReactReconciler.js:46)
at Object.updateChildren (ReactChildReconciler.js:121)
at ReactDOMComponent._reconcilerUpdateChildren (ReactMultiChild.js:208)
at ReactDOMComponent._updateChildren (ReactMultiChild.js:312)
.. which takes me to this constructor() line in the native-shim:
window.customElements.define = (tagname, elementClass) => {
const elementProto = elementClass.prototype;
const StandInElement = class extends NativeHTMLElement {
constructor() {
Phew. So it's very unclear to me how we actually include this in a webpack based build, where the dependency using CustomElements is ES6 ( and needs transpiling).
Transpiling the native-shim to es5 doesn't work
using the native-shim as-is at the top of the bundle entry point doesn't work for iOS 8, but does for Chrome
not including the native-shim breaks both Chrome and iOS
I'm really quite frustrated with web components at this point. I just want to use this one dependency that happens to be built with web components. How can I get it to work properly in a webpack build, and work across all devices? Am I missing something obvious here?
My .babelrc config for posterity sake (dev config most relevant):
{
"presets": [
["es2015", { "modules": false }],
"react"
],
"plugins": [
"transform-custom-element-classes",
"transform-object-rest-spread",
"transform-object-assign",
"transform-exponentiation-operator"
],
"env": {
"test": {
"plugins": [
[ "babel-plugin-webpack-alias", { "config": "./cfg/test.js" } ]
]
},
"dev": {
"plugins": [
"react-hot-loader/babel",
[ "babel-plugin-webpack-alias", { "config": "./cfg/dev.js" } ]
]
},
"dist": {
"plugins": [
[ "babel-plugin-webpack-alias", { "config": "./cfg/dist.js" } ],
"transform-react-constant-elements",
"transform-react-remove-prop-types",
"minify-dead-code-elimination",
"minify-constant-folding"
]
},
"production": {
"plugins": [
[ "babel-plugin-webpack-alias", { "config": "./cfg/server.js" } ],
"transform-react-constant-elements",
"transform-react-remove-prop-types",
"minify-dead-code-elimination",
"minify-constant-folding"
]
}
}
}
I was able to achieve something similar with the .babelrc plugin pipeline below. It looks like the only differences are https://babeljs.io/docs/plugins/transform-es2015-classes/ and https://babeljs.io/docs/plugins/transform-es2015-classes/, but I honestly can't remember what problems those were solving specifically:
{
"plugins": [
"transform-runtime",
["babel-plugin-transform-builtin-extend", {
"globals": ["Error", "Array"]
}],
"syntax-async-functions",
"transform-async-to-generator",
"transform-custom-element-classes",
"transform-es2015-classes"
]
}

Extracting value of a node in Java using contains with JsonPath in RestAssured

I have to extract value of book title using JsonPath in RestAssured in Java from following json response
{
"spec": {
"groups": [
{
"name": "book",
"title": "classic-books:1.0.2"
},.......
]
}
}
I am looking to use contains to get the book with a specific title.Please help.
Assume you have response with JSON in it:
response.body().jsonPath().get("spec.groups[i].title");

Resources