Subject: [PATCH 02/44] guix: build-system: node: Add node-is-type-object helper.

From: Nicolas Graves <[email-masked]>
Date: Tue, 6 Feb 2024 00:18:50 +0100

* guix/build-system/node.scm (node-is-type-object): Add function.

Change-Id: I7027aff3b314cd999798afd8c11dc90ef7c6e43c
---
 guix/build-system/node.scm | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/guix/build-system/node.scm b/guix/build-system/node.scm
index 8a903b4fe5..b52b8f4f3e 100644
--- a/guix/build-system/node.scm
+++ b/guix/build-system/node.scm
@@ -35,8 +35,22 @@ (define-module (guix build-system node)
             node-build
             node-build-system
 
+            node-is-type-object
             node-trivial-package))
 
+(define* (node-is-type-object type #:optional (type-exists? #t))
+  "Helper for node packages replacement.  This is actual code factorized from
+the packages it replaces, use with caution.  It doesn't apply to all
+javascript types."
+  (string-append
+   "function(value) {"
+   (if type-exists?
+       (string-append
+        "if (typeof value === '" (string-downcase type) "') {return true;} ")
+       "")
+   "if (value === null || typeof value !== 'object') {return false;}
+  return Object.prototype.toString.call(value) === '[object " type "]';}"))
+
 (define* (node-trivial-package node-name version replacement description
                                #:key (propagated-inputs '())
                                (mozilla-doclink #f)  ;maybe-string
-- 
2.41.0