diff --git a/README.md b/README.md index d567eb91a..d37af2df9 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ The following core Node.js modules (v8.7.0+) have been implemented: | [assert](https://nodejs.org/api/assert.html) | :heavy_check_mark: | | [buffer](https://nodejs.org/api/buffer.html) | :heavy_check_mark: | | [child_process](https://nodejs.org/api/child_process.html) | :heavy_check_mark: | -| [cluster](https://nodejs.org/api/cluster.html) | | +| [cluster](https://nodejs.org/api/cluster.html) | :heavy_check_mark: | | [console](https://nodejs.org/api/console.html) | :heavy_check_mark: | | [crypto](https://nodejs.org/api/crypto.html) | | | [dgram](https://nodejs.org/api/dgram.html) | | @@ -44,7 +44,7 @@ The following core Node.js modules (v8.7.0+) have been implemented: | [string-decoder](https://nodejs.org/api/string_decoder.html) | | | [timers](https://nodejs.org/api/timers.html) | | | [tty](https://nodejs.org/api/tty.html) | | -| [url](https://nodejs.org/api/url.html) | | +| [url](https://nodejs.org/api/url.html) | :heavy_check_mark: | | [util](https://nodejs.org/api/util.html) | | | [vm](https://nodejs.org/api/vm.html) | | | [zlib](https://nodejs.org/api/zlib.html) | | diff --git a/app/current/src/main/scala/io/scalajs/nodejs/util/InspectOptions.scala b/app/current/src/main/scala/io/scalajs/nodejs/util/InspectOptions.scala index 58805b9b1..93c1deb9d 100644 --- a/app/current/src/main/scala/io/scalajs/nodejs/util/InspectOptions.scala +++ b/app/current/src/main/scala/io/scalajs/nodejs/util/InspectOptions.scala @@ -1,6 +1,7 @@ package io.scalajs.nodejs.util import scala.scalajs.js +import scala.scalajs.js.| /** * Inspect Options @@ -18,6 +19,10 @@ import scala.scalajs.js * Defaults to 100. Set to null to show all array elements. Set to 0 or negative to show no array elements. * @param breakLength The length at which an object's keys are split across multiple lines. Set to Infinity to * format an object as a single line. Defaults to 60 for legacy compatibility. + * @param compact For Node.js v9.9.0+ + * @param sorted For Node.js v10.12.0+ + * @param getters For Node.js v11.5.0+ + * * @see [[https://nodejs.org/api/util.html#util_util_inspect_object_options]] */ class InspectOptions(var showHidden: js.UndefOr[Boolean] = js.undefined, @@ -26,5 +31,8 @@ class InspectOptions(var showHidden: js.UndefOr[Boolean] = js.undefined, var customInspect: js.UndefOr[Boolean] = js.undefined, var showProxy: js.UndefOr[Boolean] = js.undefined, var maxArrayLength: js.UndefOr[Int] = js.undefined, - var breakLength: js.UndefOr[Int] = js.undefined) + var breakLength: js.UndefOr[Int] = js.undefined, + var compact: js.UndefOr[Boolean | Int] = js.undefined, + var sorted: js.UndefOr[Boolean | js.Function2[String, String, Int]] = js.undefined, + var getters: js.UndefOr[Boolean | String] = js.undefined) extends js.Object diff --git a/app/current/src/main/scala/io/scalajs/nodejs/util/TextDecoder.scala b/app/current/src/main/scala/io/scalajs/nodejs/util/TextDecoder.scala new file mode 100644 index 000000000..970e368b5 --- /dev/null +++ b/app/current/src/main/scala/io/scalajs/nodejs/util/TextDecoder.scala @@ -0,0 +1,21 @@ +package io.scalajs.nodejs.util + +import scala.scalajs.js +import scala.scalajs.js.annotation.JSImport +import scala.scalajs.js.typedarray.{ArrayBuffer, ArrayBufferView} +import scala.scalajs.js.| +@js.native +@JSImport("util", "TextDecoder") +class TextDecoder() extends js.Object { + def this(encoding: String) = this() + + val encoding: String = js.native + val fatal: Boolean = js.native + val ignoreBOM: Boolean = js.native + + def decode(buffer: ArrayBuffer | ArrayBufferView, options: TextDecodeOptions = js.native): String = js.native +} + +class TextDecodeOptions( + stream: js.UndefOr[Boolean] = js.undefined +) extends js.Object {} diff --git a/app/current/src/main/scala/io/scalajs/nodejs/util/TextEncoder.scala b/app/current/src/main/scala/io/scalajs/nodejs/util/TextEncoder.scala new file mode 100644 index 000000000..d9de15ddd --- /dev/null +++ b/app/current/src/main/scala/io/scalajs/nodejs/util/TextEncoder.scala @@ -0,0 +1,14 @@ +package io.scalajs.nodejs.util + +import scala.scalajs.js +import scala.scalajs.js.annotation.JSImport +import scala.scalajs.js.typedarray.Uint8Array +@js.native +@JSImport("util", "TextEncoder") +class TextEncoder() extends js.Object { + def this(encoding: String) = this() + + val encoding: String = js.native + + def encode(text: String): Uint8Array = js.native +} diff --git a/app/current/src/main/scala/io/scalajs/nodejs/util/Util.scala b/app/current/src/main/scala/io/scalajs/nodejs/util/Util.scala index 7c12cac3b..938833bcc 100644 --- a/app/current/src/main/scala/io/scalajs/nodejs/util/Util.scala +++ b/app/current/src/main/scala/io/scalajs/nodejs/util/Util.scala @@ -1,7 +1,7 @@ package io.scalajs.nodejs.util +import com.thoughtworks.{enableIf, enableMembersIf} import io.scalajs.RawOptions -import io.scalajs.nodejs.events.IEventEmitter import io.scalajs.nodejs.stream import scala.scalajs.js @@ -18,7 +18,7 @@ import scala.scalajs.js.| * @see https://nodejs.org/api/util.html */ @js.native -trait Util extends IEventEmitter { +trait Util extends js.Object { /** * Deprecated predecessor of console.error. @@ -37,9 +37,9 @@ trait Util extends IEventEmitter { /** * Marks that a method should not be used any more. - * @example util.deprecate(function, string) + * @example util.deprecate(function, message) */ - def deprecate(function: js.Function, string: String): js.Any = js.native + def deprecate(function: js.Function, message: String, code: String = js.native): js.Any = js.native /** * Deprecated predecessor of console.error. @@ -52,7 +52,13 @@ trait Util extends IEventEmitter { * Returns a formatted string using the first argument as a printf-like format. * @example util.format(format[, ...]) */ - def format(format: js.Any*): String = js.native + def format(format: String, args: js.Any*): String = js.native + + @enableIf(io.scalajs.nodejs.CompilerSwitches.gteNodeJs10) + def formatWithOptions(inspectOptions: InspectOptions | RawOptions, format: String, args: js.Any*): String = js.native + + @enableIf(io.scalajs.nodejs.CompilerSwitches.gteNodeJs10) + def getSystemErrorName(err: Int): String = js.native /** * Inherit the prototype methods from one constructor into another. The prototype of constructor will be set to a @@ -72,6 +78,8 @@ trait Util extends IEventEmitter { */ def inspect(`object`: js.Any, options: InspectOptions | RawOptions = js.native): String = js.native + val inspect: InspectObject = js.native + /** * Returns true if the given "object" is an Array. Otherwise, returns false. *
NOTEInternal alias for Array.isArray. @@ -215,6 +223,16 @@ trait Util extends IEventEmitter { @deprecated("Use Object.assign() instead.", "6.0.0") def _extend[A <: js.Any, B <: js.Any, C <: js.Any](target: A, source: B): C = js.native + def callbackify[T](original: js.Function): js.Function2[js.Any, T, Any] = js.native + + def promisify(original: js.Function): js.Function = js.native + val promisify: PromisifyObject = js.native + + @enableIf(io.scalajs.nodejs.CompilerSwitches.gteNodeJs10) + val types: UtilTypes = js.native + + @enableIf(io.scalajs.nodejs.CompilerSwitches.gteNodeJs10) + def isDeepStrictEqual(val1: js.Any, val2: js.Any): Boolean = js.native } /** @@ -223,3 +241,61 @@ trait Util extends IEventEmitter { @js.native @JSImport("util", JSImport.Namespace) object Util extends Util + +@js.native +trait InspectObject extends js.Object { + var defaultOptions: InspectOptions = js.native + var styles: js.Dictionary[String] = js.native + + @enableIf(io.scalajs.nodejs.CompilerSwitches.gteNodeJs10) + val custom: js.Symbol = js.native +} + +@js.native +trait PromisifyObject extends js.Object { + val custom: js.Symbol = js.native +} + +@enableMembersIf(io.scalajs.nodejs.CompilerSwitches.gteNodeJs10) +@js.native +trait UtilTypes extends js.Object { + def isAnyArrayBuffer(value: js.Any): Boolean = js.native + def isArgumentsObject(value: js.Any): Boolean = js.native + def isArrayBuffer(value: js.Any): Boolean = js.native + def isAsyncFunction(value: js.Any): Boolean = js.native + def isBigInt64Array(value: js.Any): Boolean = js.native + def isBigUint64Array(value: js.Any): Boolean = js.native + def isBooleanObject(value: js.Any): Boolean = js.native + def isBoxedPrimitive(value: js.Any): Boolean = js.native + def isDataView(value: js.Any): Boolean = js.native + def isDate(value: js.Any): Boolean = js.native + def isExternal(value: js.Any): Boolean = js.native + def isFloat32Array(value: js.Any): Boolean = js.native + def isFloat64Array(value: js.Any): Boolean = js.native + def isGeneratorFunction(value: js.Any): Boolean = js.native + def isGeneratorObject(value: js.Any): Boolean = js.native + def isInt8Array(value: js.Any): Boolean = js.native + def isInt16Array(value: js.Any): Boolean = js.native + def isInt32Array(value: js.Any): Boolean = js.native + def isMap(value: js.Any): Boolean = js.native + def isMapIterator(value: js.Any): Boolean = js.native + def isModuleNamespaceObject(value: js.Any): Boolean = js.native + def isNativeError(value: js.Any): Boolean = js.native + def isNumberObject(value: js.Any): Boolean = js.native + def isPromise(value: js.Any): Boolean = js.native + def isProxy(value: js.Any): Boolean = js.native + def isRegExp(value: js.Any): Boolean = js.native + def isSet(value: js.Any): Boolean = js.native + def isSetIterator(value: js.Any): Boolean = js.native + def isSharedArrayBuffer(value: js.Any): Boolean = js.native + def isStringObject(value: js.Any): Boolean = js.native + def isSymbolObject(value: js.Any): Boolean = js.native + def isTypedArray(value: js.Any): Boolean = js.native + def isUint8Array(value: js.Any): Boolean = js.native + def isUint8ClampedArray(value: js.Any): Boolean = js.native + def isUint16Array(value: js.Any): Boolean = js.native + def isUint32Array(value: js.Any): Boolean = js.native + def isWeakMap(value: js.Any): Boolean = js.native + def isWeakSet(value: js.Any): Boolean = js.native + def isWebAssemblyCompiledModule(value: js.Any): Boolean = js.native +} diff --git a/app/nodejs-v10/src/test/scala/nodejs/util/UtilTest.scala b/app/nodejs-v10/src/test/scala/nodejs/util/UtilTest.scala new file mode 100644 index 000000000..5591c7d62 --- /dev/null +++ b/app/nodejs-v10/src/test/scala/nodejs/util/UtilTest.scala @@ -0,0 +1,27 @@ +package io.scalajs.nodejs.util + +import org.scalatest.FunSpec + +import scala.scalajs.js + +class UtilTest extends FunSpec { + + it("have formatWithOptions added in v10.0.0") { + assert(Util.formatWithOptions(new InspectOptions(compact = true), "See object %O", new js.Object { + val foo: Int = 42 + }) === "See object { foo: 42 }") + } + + it("have getSystemErrorName added in v9.7.0") { + assert(Util.getSystemErrorName(-1) === "EPERM") + } + + it("have types added in v10.0.0") { + assert(Util.types.isDate(new js.Date)) + } + + it("have inspect.custom added in v10.12.0") { + assert(Util.inspect.custom != null) + } + +} diff --git a/app/nodejs-v8/src/test/scala/nodejs/util/UtilTest.scala b/app/nodejs-v8/src/test/scala/nodejs/util/UtilTest.scala new file mode 100644 index 000000000..afee3aa72 --- /dev/null +++ b/app/nodejs-v8/src/test/scala/nodejs/util/UtilTest.scala @@ -0,0 +1,24 @@ +package io.scalajs.nodejs.util + +import org.scalatest.FunSpec + +class UtilTest extends FunSpec { + + it("have inspect object") { + assert(Util.inspect != null) + assert(Util.inspect.defaultOptions != null) + assert(Util.inspect.styles != null) + } + + it("have promisify") { + assert(Util.promisify.custom != null) + } + + it("have TextEncoder/TextDecoder") { + val encoder = new TextEncoder() + val decoder = new TextDecoder() + val encoded = encoder.encode("foobar") + val decoded = decoder.decode(encoded) + assert(decoded === "foobar") + } +}