Skip to content
This repository was archived by the owner on Jul 30, 2024. It is now read-only.

Commit 2c3b484

Browse files
author
TATSUNO Yasuhiro
authored
Merge pull request #51 from exoego/util
Overhaul util module
2 parents e083097 + b20c147 commit 2c3b484

File tree

7 files changed

+178
-8
lines changed

7 files changed

+178
-8
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ The following core Node.js modules (v8.7.0+) have been implemented:
2323
| [assert](https://nodejs.org/api/assert.html) | :heavy_check_mark: |
2424
| [buffer](https://nodejs.org/api/buffer.html) | :heavy_check_mark: |
2525
| [child_process](https://nodejs.org/api/child_process.html) | :heavy_check_mark: |
26-
| [cluster](https://nodejs.org/api/cluster.html) | |
26+
| [cluster](https://nodejs.org/api/cluster.html) | :heavy_check_mark: |
2727
| [console](https://nodejs.org/api/console.html) | :heavy_check_mark: |
2828
| [crypto](https://nodejs.org/api/crypto.html) | |
2929
| [dgram](https://nodejs.org/api/dgram.html) | |
@@ -44,7 +44,7 @@ The following core Node.js modules (v8.7.0+) have been implemented:
4444
| [string-decoder](https://nodejs.org/api/string_decoder.html) | |
4545
| [timers](https://nodejs.org/api/timers.html) | |
4646
| [tty](https://nodejs.org/api/tty.html) | |
47-
| [url](https://nodejs.org/api/url.html) | |
47+
| [url](https://nodejs.org/api/url.html) | :heavy_check_mark: |
4848
| [util](https://nodejs.org/api/util.html) | |
4949
| [vm](https://nodejs.org/api/vm.html) | |
5050
| [zlib](https://nodejs.org/api/zlib.html) | |

app/current/src/main/scala/io/scalajs/nodejs/util/InspectOptions.scala

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.scalajs.nodejs.util
22

33
import scala.scalajs.js
4+
import scala.scalajs.js.|
45

56
/**
67
* Inspect Options
@@ -18,6 +19,10 @@ import scala.scalajs.js
1819
* Defaults to 100. Set to null to show all array elements. Set to 0 or negative to show no array elements.
1920
* @param breakLength The length at which an object's keys are split across multiple lines. Set to Infinity to
2021
* format an object as a single line. Defaults to 60 for legacy compatibility.
22+
* @param compact For Node.js v9.9.0+
23+
* @param sorted For Node.js v10.12.0+
24+
* @param getters For Node.js v11.5.0+
25+
*
2126
* @see [[https://nodejs.org/api/util.html#util_util_inspect_object_options]]
2227
*/
2328
class InspectOptions(var showHidden: js.UndefOr[Boolean] = js.undefined,
@@ -26,5 +31,8 @@ class InspectOptions(var showHidden: js.UndefOr[Boolean] = js.undefined,
2631
var customInspect: js.UndefOr[Boolean] = js.undefined,
2732
var showProxy: js.UndefOr[Boolean] = js.undefined,
2833
var maxArrayLength: js.UndefOr[Int] = js.undefined,
29-
var breakLength: js.UndefOr[Int] = js.undefined)
34+
var breakLength: js.UndefOr[Int] = js.undefined,
35+
var compact: js.UndefOr[Boolean | Int] = js.undefined,
36+
var sorted: js.UndefOr[Boolean | js.Function2[String, String, Int]] = js.undefined,
37+
var getters: js.UndefOr[Boolean | String] = js.undefined)
3038
extends js.Object
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package io.scalajs.nodejs.util
2+
3+
import scala.scalajs.js
4+
import scala.scalajs.js.annotation.JSImport
5+
import scala.scalajs.js.typedarray.{ArrayBuffer, ArrayBufferView}
6+
import scala.scalajs.js.|
7+
@js.native
8+
@JSImport("util", "TextDecoder")
9+
class TextDecoder() extends js.Object {
10+
def this(encoding: String) = this()
11+
12+
val encoding: String = js.native
13+
val fatal: Boolean = js.native
14+
val ignoreBOM: Boolean = js.native
15+
16+
def decode(buffer: ArrayBuffer | ArrayBufferView, options: TextDecodeOptions = js.native): String = js.native
17+
}
18+
19+
class TextDecodeOptions(
20+
stream: js.UndefOr[Boolean] = js.undefined
21+
) extends js.Object {}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package io.scalajs.nodejs.util
2+
3+
import scala.scalajs.js
4+
import scala.scalajs.js.annotation.JSImport
5+
import scala.scalajs.js.typedarray.Uint8Array
6+
@js.native
7+
@JSImport("util", "TextEncoder")
8+
class TextEncoder() extends js.Object {
9+
def this(encoding: String) = this()
10+
11+
val encoding: String = js.native
12+
13+
def encode(text: String): Uint8Array = js.native
14+
}

app/current/src/main/scala/io/scalajs/nodejs/util/Util.scala

Lines changed: 81 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package io.scalajs.nodejs.util
22

3+
import com.thoughtworks.{enableIf, enableMembersIf}
34
import io.scalajs.RawOptions
4-
import io.scalajs.nodejs.events.IEventEmitter
55
import io.scalajs.nodejs.stream
66

77
import scala.scalajs.js
@@ -18,7 +18,7 @@ import scala.scalajs.js.|
1818
* @see https://nodejs.org/api/util.html
1919
*/
2020
@js.native
21-
trait Util extends IEventEmitter {
21+
trait Util extends js.Object {
2222

2323
/**
2424
* Deprecated predecessor of console.error.
@@ -37,9 +37,9 @@ trait Util extends IEventEmitter {
3737

3838
/**
3939
* Marks that a method should not be used any more.
40-
* @example util.deprecate(function, string)
40+
* @example util.deprecate(function, message)
4141
*/
42-
def deprecate(function: js.Function, string: String): js.Any = js.native
42+
def deprecate(function: js.Function, message: String, code: String = js.native): js.Any = js.native
4343

4444
/**
4545
* Deprecated predecessor of console.error.
@@ -52,7 +52,13 @@ trait Util extends IEventEmitter {
5252
* Returns a formatted string using the first argument as a printf-like format.
5353
* @example util.format(format[, ...])
5454
*/
55-
def format(format: js.Any*): String = js.native
55+
def format(format: String, args: js.Any*): String = js.native
56+
57+
@enableIf(io.scalajs.nodejs.CompilerSwitches.gteNodeJs10)
58+
def formatWithOptions(inspectOptions: InspectOptions | RawOptions, format: String, args: js.Any*): String = js.native
59+
60+
@enableIf(io.scalajs.nodejs.CompilerSwitches.gteNodeJs10)
61+
def getSystemErrorName(err: Int): String = js.native
5662

5763
/**
5864
* 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 {
7278
*/
7379
def inspect(`object`: js.Any, options: InspectOptions | RawOptions = js.native): String = js.native
7480

81+
val inspect: InspectObject = js.native
82+
7583
/**
7684
* Returns true if the given "object" is an Array. Otherwise, returns false.
7785
* <p/><b>NOTE</b>Internal alias for Array.isArray.
@@ -215,6 +223,16 @@ trait Util extends IEventEmitter {
215223
@deprecated("Use Object.assign() instead.", "6.0.0")
216224
def _extend[A <: js.Any, B <: js.Any, C <: js.Any](target: A, source: B): C = js.native
217225

226+
def callbackify[T](original: js.Function): js.Function2[js.Any, T, Any] = js.native
227+
228+
def promisify(original: js.Function): js.Function = js.native
229+
val promisify: PromisifyObject = js.native
230+
231+
@enableIf(io.scalajs.nodejs.CompilerSwitches.gteNodeJs10)
232+
val types: UtilTypes = js.native
233+
234+
@enableIf(io.scalajs.nodejs.CompilerSwitches.gteNodeJs10)
235+
def isDeepStrictEqual(val1: js.Any, val2: js.Any): Boolean = js.native
218236
}
219237

220238
/**
@@ -223,3 +241,61 @@ trait Util extends IEventEmitter {
223241
@js.native
224242
@JSImport("util", JSImport.Namespace)
225243
object Util extends Util
244+
245+
@js.native
246+
trait InspectObject extends js.Object {
247+
var defaultOptions: InspectOptions = js.native
248+
var styles: js.Dictionary[String] = js.native
249+
250+
@enableIf(io.scalajs.nodejs.CompilerSwitches.gteNodeJs10)
251+
val custom: js.Symbol = js.native
252+
}
253+
254+
@js.native
255+
trait PromisifyObject extends js.Object {
256+
val custom: js.Symbol = js.native
257+
}
258+
259+
@enableMembersIf(io.scalajs.nodejs.CompilerSwitches.gteNodeJs10)
260+
@js.native
261+
trait UtilTypes extends js.Object {
262+
def isAnyArrayBuffer(value: js.Any): Boolean = js.native
263+
def isArgumentsObject(value: js.Any): Boolean = js.native
264+
def isArrayBuffer(value: js.Any): Boolean = js.native
265+
def isAsyncFunction(value: js.Any): Boolean = js.native
266+
def isBigInt64Array(value: js.Any): Boolean = js.native
267+
def isBigUint64Array(value: js.Any): Boolean = js.native
268+
def isBooleanObject(value: js.Any): Boolean = js.native
269+
def isBoxedPrimitive(value: js.Any): Boolean = js.native
270+
def isDataView(value: js.Any): Boolean = js.native
271+
def isDate(value: js.Any): Boolean = js.native
272+
def isExternal(value: js.Any): Boolean = js.native
273+
def isFloat32Array(value: js.Any): Boolean = js.native
274+
def isFloat64Array(value: js.Any): Boolean = js.native
275+
def isGeneratorFunction(value: js.Any): Boolean = js.native
276+
def isGeneratorObject(value: js.Any): Boolean = js.native
277+
def isInt8Array(value: js.Any): Boolean = js.native
278+
def isInt16Array(value: js.Any): Boolean = js.native
279+
def isInt32Array(value: js.Any): Boolean = js.native
280+
def isMap(value: js.Any): Boolean = js.native
281+
def isMapIterator(value: js.Any): Boolean = js.native
282+
def isModuleNamespaceObject(value: js.Any): Boolean = js.native
283+
def isNativeError(value: js.Any): Boolean = js.native
284+
def isNumberObject(value: js.Any): Boolean = js.native
285+
def isPromise(value: js.Any): Boolean = js.native
286+
def isProxy(value: js.Any): Boolean = js.native
287+
def isRegExp(value: js.Any): Boolean = js.native
288+
def isSet(value: js.Any): Boolean = js.native
289+
def isSetIterator(value: js.Any): Boolean = js.native
290+
def isSharedArrayBuffer(value: js.Any): Boolean = js.native
291+
def isStringObject(value: js.Any): Boolean = js.native
292+
def isSymbolObject(value: js.Any): Boolean = js.native
293+
def isTypedArray(value: js.Any): Boolean = js.native
294+
def isUint8Array(value: js.Any): Boolean = js.native
295+
def isUint8ClampedArray(value: js.Any): Boolean = js.native
296+
def isUint16Array(value: js.Any): Boolean = js.native
297+
def isUint32Array(value: js.Any): Boolean = js.native
298+
def isWeakMap(value: js.Any): Boolean = js.native
299+
def isWeakSet(value: js.Any): Boolean = js.native
300+
def isWebAssemblyCompiledModule(value: js.Any): Boolean = js.native
301+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package io.scalajs.nodejs.util
2+
3+
import org.scalatest.FunSpec
4+
5+
import scala.scalajs.js
6+
7+
class UtilTest extends FunSpec {
8+
9+
it("have formatWithOptions added in v10.0.0") {
10+
assert(Util.formatWithOptions(new InspectOptions(compact = true), "See object %O", new js.Object {
11+
val foo: Int = 42
12+
}) === "See object { foo: 42 }")
13+
}
14+
15+
it("have getSystemErrorName added in v9.7.0") {
16+
assert(Util.getSystemErrorName(-1) === "EPERM")
17+
}
18+
19+
it("have types added in v10.0.0") {
20+
assert(Util.types.isDate(new js.Date))
21+
}
22+
23+
it("have inspect.custom added in v10.12.0") {
24+
assert(Util.inspect.custom != null)
25+
}
26+
27+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package io.scalajs.nodejs.util
2+
3+
import org.scalatest.FunSpec
4+
5+
class UtilTest extends FunSpec {
6+
7+
it("have inspect object") {
8+
assert(Util.inspect != null)
9+
assert(Util.inspect.defaultOptions != null)
10+
assert(Util.inspect.styles != null)
11+
}
12+
13+
it("have promisify") {
14+
assert(Util.promisify.custom != null)
15+
}
16+
17+
it("have TextEncoder/TextDecoder") {
18+
val encoder = new TextEncoder()
19+
val decoder = new TextDecoder()
20+
val encoded = encoder.encode("foobar")
21+
val decoded = decoder.decode(encoded)
22+
assert(decoded === "foobar")
23+
}
24+
}

0 commit comments

Comments
 (0)