Git Product home page Git Product logo

type's Introduction

Sapphire Logo

@sapphire/type

A type detection utility for JavaScript.

GitHub npm bundle size npm


Table of Contents

Description

Sometimes there is a need to know what a value's type of something is, this package exists to satisfy those needs by implementing a parser that can retrieve the type of the value recursively and process a string similar to TypeScript's types.

Features

  • Written in TypeScript
  • Bundled with Rollup so it can be used in NodeJS and browsers
  • Offers CommonJS, ESM and UMD bundles
  • Fully tested

Installation

yarn add @sapphire/type
# npm install @sapphire/type

Usage

Note: While this section uses require, the imports match 1:1 with ESM imports. For example const { Type } = require('@sapphire/type') equals import { Type } from '@sapphire/type'.

Note: When using ESM syntax you can also default-import Type class: import Type from '@sapphire/type'.

Note: The internal functions of getPromiseDetails and getProxyDetails are also exported. If you don't know what these are, then you need not be concerned about this.

Basic Usage

// Import the Type class
const { Type } = require('@sapphire/type');

// Define a Type with an array of two numbers
const type = new Type([1, 2]);

console.log(type.toString());
// Logs: Array<number>

Buy us some doughnuts

Sapphire Community is and always will be open source, even if we don't get donations. That being said, we know there are amazing people who may still want to donate just to show their appreciation. Thank you very much in advance!

We accept donations through Open Collective, Ko-fi, PayPal, Patreon and GitHub Sponsorships. You can use the buttons below to donate through your method of choice.

Donate With Address
Open Collective Click Here
Ko-fi Click Here
Patreon Click Here
PayPal Click Here

Contributors

Please make sure to read the Contributing Guide before making a pull request.

Thank you to all the people who already contributed to Sapphire!

type's People

Contributors

allcontributors[bot] avatar dependabot[bot] avatar depfu[bot] avatar favna avatar github-actions[bot] avatar lioness100 avatar pyrotechniac avatar realshadownova avatar renovate[bot] avatar vladfrangu avatar voltrexkeyva avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

type's Issues

bug: Error when getting output type of "require('discord.js')"

Is there an existing issue for this?

  • I have searched the existing issues

Description of the bug

Only seems to happen when requiring discord.js and evaluating the output of it using the Type class. So I doubt you would want to fix it. Just thought I'd make a bug report just in case.

Example code includes

const { Type } = require('@sapphire/type');
const DJS = require('discord.js');
const output = new Type(DJS);
output.toString();

Will cause the error.

TypeError: Cannot read properties of null (reading 'name')
     at Function.resolve (/opt/stratobotelite/node_modules/@sapphire/type/src/lib/index.ts:152:2)
     at new Type (/opt/stratobotelite/node_modules/@sapphire/type/src/lib/index.ts:60:13)
     at _Type.addValue (/opt/stratobotelite/node_modules/@sapphire/type/src/lib/index.ts:102:25)
     at _Type.addEntry (/opt/stratobotelite/node_modules/@sapphire/type/src/lib/index.ts:112:30)
     at _Type.check (/opt/stratobotelite/node_modules/@sapphire/type/src/lib/index.ts:134:2)
     at _Type.toString (/opt/stratobotelite/node_modules/@sapphire/type/src/lib/index.ts:77:7)
     at Function.codeBlock (/opt/stratobotelite/src/frameworks/klasa/lib/util/util.js:41:42)
     at module.exports.run (/opt/stratobotelite/src/bot/commands/Admin/eval.js:138:23)
     at processTicksAndRejections (node:internal/process/task_queues:95:5)
     at async module.exports.runCommand (/opt/stratobotelite/src/bot/monitors/commandHandler.js:75:23)

Steps To Reproduce

const { Type } = require('@sapphire/type');
const DJS = require('discord.js');
const output = new Type(DJS);
output.toString();

Expected behavior

Wasn't expected to error.

Screenshots

image
image

Additional context

No response

System Info

System:
    OS: Linux 4.15 Debian GNU/Linux 12 (bookworm) 12 (bookworm)
    CPU: (4) x64 Intel Core Processor (Haswell, no TSX)
    Memory: 6.34 GB / 7.60 GB
    Container: Yes
    Shell: 5.2.15 - /bin/bash
  Binaries:
    Node: 21.7.1 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 10.5.0 - /usr/local/bin/npm

request: glibc 2.28 support for AlmaLinux 8.7 users

Is there an existing issue or pull request for this?

  • I have searched the existing issues and pull requests

Feature description

The prebuilt binaries released do not include ones for glibc 2.28, which is used in AlmaLinux 8.7 (CentOS replacement). Therefore, Sapphire cannot run on an AlmaLinux 8.7 system without manually building it (which is very tedious considering this package is often a dependency and used within a package manager, which fails because it could not find a prebuilt for glibc 2.28 to download).

Desired solution

Prebuilt binaries for glibc 2.28 in releases.

Alternatives considered

Building manually; very tedious and not worth the amount of effort necessary, especially for every new release.

Using a different version of glibc; also very tedious when AlmaLinux is basically set to use 2.28 by default. Even AlmaLinux 9 uses 2.34, which is also not included in prebuilt binaries.

Additional context

No response

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Ignored or Blocked

These are blocked by an existing closed PR and will not be recreated unless you click a checkbox below.

Detected dependencies

github-actions
.github/workflows/auto-deprecate.yml
.github/workflows/ci-build-and-test.yml
  • actions/checkout v4
  • actions/setup-node v4
  • actions/checkout v4
  • actions/checkout v4
  • actions/setup-node v4
  • actions/checkout v4
.github/workflows/ci-lint-and-docs.yml
.github/workflows/codeql-analysis.yml
.github/workflows/continuous-delivery.yml
.github/workflows/deprecate-on-merge.yml
.github/workflows/documentation.yml
.github/workflows/labelsync.yml
  • actions/checkout v4
  • crazy-max/ghaction-github-labeler v5
.github/workflows/manual-asset-updater.yml
  • actions/checkout v4
  • actions/setup-node v4
  • actions/checkout v4
  • actions/checkout v4
  • actions/setup-node v4
  • actions/checkout v4
.github/workflows/publish.yml
.github/workflows/release-updater.yml
  • actions/checkout v4
  • actions/setup-node v4
  • actions/checkout v4
  • actions/checkout v4
  • actions/setup-node v4
  • actions/checkout v4
npm
package.json
  • @discordjs/node-pre-gyp ^0.4.5
  • nan ^2.20.0
  • tslib ^2.7.0
  • @actions/core ^1.10.1
  • @actions/github ^6.0.0
  • @actions/glob ^0.5.0
  • @commitlint/cli ^19.5.0
  • @commitlint/config-conventional ^19.5.0
  • @favware/cliff-jumper ^4.1.0
  • @favware/npm-deprecate ^1.0.7
  • @sapphire/eslint-config ^5.0.5
  • @sapphire/prettier-config ^2.0.0
  • @sapphire/ts-config ^5.0.1
  • @types/node ^20.16.5
  • @vitest/coverage-v8 ^2.1.1
  • cz-conventional-changelog ^3.3.0
  • discord.js ^14.16.2
  • eslint ^8.57.1
  • eslint-config-prettier ^9.1.0
  • eslint-plugin-prettier ^5.2.1
  • lint-staged ^15.2.10
  • prettier ^3.3.3
  • tsup ^8.3.0
  • vite ^5.4.7
  • vitest ^2.1.1
  • ansi-regex ^5.0.1
  • minimist ^1.2.8
  • yarn 4.5.0

  • Check this box to trigger a request for Renovate to run again on this repository

request: musl 1.2.4 support for Alpine 3.18 users

Is there an existing issue or pull request for this?

  • I have searched the existing issues and pull requests

Feature description

The prebuilt binaries do not include musl 1.2.4, which is used by Alpine 3.18. I believe this was just recently released (start of June if I'm correct) so that's why there is no prebuilt for musl (last release of this package was on April).

I checked #429 and the solution seems to be to use Yarn, but it didn't mention that it required Python to be installed in the Docker image. I had to add the following line to my Dockerfile as suggested here:

RUN apk add --no-cache libc6-compat python3 build-base gcc

Desired solution

Prebuilt binaries for musl 1.2.4 in releases would prevent users from having to install Python into the Docker build image.

I know I can use multi-stage builds to remove Python from the final image -- I'm using that for my image. But having the prebuilt for musl could reduce build times.

Alternatives considered

Building manually works with Yarn and Python installed in the Docker build image.

Additional context

Here's the log generated from the failed build by node-pre-gyp when I didn't have Python installed, just in case it is needed:

build.log
# This file contains the result of Yarn building a package (@sapphire/type@npm:2.4.0)
# Script name: install

node-pre-gyp info it worked if it ends with ok
node-pre-gyp info using [email protected]
node-pre-gyp info using [email protected] | linux | x64
node-pre-gyp info check checked for "/app/node_modules/@sapphire/type/prebuild/type-node-v108-linux-x64-musl-1.2.4/type.node" (not found)
node-pre-gyp http GET https://github.com/sapphiredev/type/releases/download/v2.4.0/type-v2.4.0-node-v108-linux-x64-musl-1.2.4.tar.gz
node-pre-gyp ERR! install response status 404 Not Found on https://github.com/sapphiredev/type/releases/download/v2.4.0/type-v2.4.0-node-v108-linux-x64-musl-1.2.4.tar.gz
node-pre-gyp WARN Pre-built binaries not installable for @sapphire/[email protected] and [email protected] (node-v108 ABI, musl) (falling back to source compile with node-gyp)
node-pre-gyp WARN Hit error response status 404 Not Found on https://github.com/sapphiredev/type/releases/download/v2.4.0/type-v2.4.0-node-v108-linux-x64-musl-1.2.4.tar.gz
gyp info it worked if it ends with ok
gyp info using [email protected]
gyp info using [email protected] | linux | x64
gyp info ok
gyp info it worked if it ends with ok
gyp info using [email protected]
gyp info using [email protected] | linux | x64
gyp ERR! find Python
gyp ERR! find Python Python is not set from command line or npm configuration
gyp ERR! find Python Python is not set from environment variable PYTHON
gyp ERR! find Python checking if "python3" can be used
gyp ERR! find Python - "python3" is not in PATH or produced an error
gyp ERR! find Python checking if "python" can be used
gyp ERR! find Python - "python" is not in PATH or produced an error
gyp ERR! find Python
gyp ERR! find Python **********************************************************
gyp ERR! find Python You need to install the latest version of Python.
gyp ERR! find Python Node-gyp should be able to find and use Python. If not,
gyp ERR! find Python you can try one of the following options:
gyp ERR! find Python - Use the switch --python="/path/to/pythonexecutable"
gyp ERR! find Python   (accepted by both node-gyp and npm)
gyp ERR! find Python - Set the environment variable PYTHON
gyp ERR! find Python - Set the npm configuration variable python:
gyp ERR! find Python   npm config set python "/path/to/pythonexecutable"
gyp ERR! find Python For more information consult the documentation at:
gyp ERR! find Python https://github.com/nodejs/node-gyp#installation
gyp ERR! find Python **********************************************************
gyp ERR! find Python
gyp ERR! configure error
gyp ERR! stack Error: Could not find any Python installation to use
gyp ERR! stack     at PythonFinder.fail (/app/node_modules/node-gyp/lib/find-python.js:330:47)
gyp ERR! stack     at PythonFinder.runChecks (/app/node_modules/node-gyp/lib/find-python.js:159:21)
gyp ERR! stack     at PythonFinder.<anonymous> (/app/node_modules/node-gyp/lib/find-python.js:202:16)
gyp ERR! stack     at PythonFinder.execFileCallback (/app/node_modules/node-gyp/lib/find-python.js:294:16)
gyp ERR! stack     at exithandler (node:child_process:427:5)
gyp ERR! stack     at ChildProcess.errorhandler (node:child_process:439:5)
gyp ERR! stack     at ChildProcess.emit (node:events:513:28)
gyp ERR! stack     at ChildProcess._handle.onexit (node:internal/child_process:289:12)
gyp ERR! stack     at onErrorNT (node:internal/child_process:476:16)
gyp ERR! stack     at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
gyp ERR! System Linux 5.15.90.1-microsoft-standard-WSL2
gyp ERR! command "/usr/local/bin/node" "/app/node_modules/node-gyp/bin/node-gyp.js" "configure" "--fallback-to-build" "--module=/app/node_modules/@sapphire/type/prebuild/type-node-v108-linux-x64-musl-1.2.4/type.node" "--module_name=type" "--module_path=/app/node_modules/@sapphire/type/prebuild/type-node-v108-linux-x64-musl-1.2.4" "--napi_version=8" "--node_abi_napi=napi" "--napi_build_version=0" "--node_napi_label=node-v108"
gyp ERR! cwd /app/node_modules/@sapphire/type
gyp ERR! node -v v18.16.1
gyp ERR! node-gyp -v v9.3.0
gyp ERR! not ok
node-pre-gyp ERR! build error
node-pre-gyp ERR! stack Error: Failed to execute '/usr/local/bin/node /app/node_modules/node-gyp/bin/node-gyp.js configure --fallback-to-build --module=/app/node_modules/@sapphire/type/prebuild/type-node-v108-linux-x64-musl-1.2.4/type.node --module_name=type --module_path=/app/node_modules/@sapphire/type/prebuild/type-node-v108-linux-x64-musl-1.2.4 --napi_version=8 --node_abi_napi=napi --napi_build_version=0 --node_napi_label=node-v108' (1)
node-pre-gyp ERR! stack     at ChildProcess.<anonymous> (/app/node_modules/@discordjs/node-pre-gyp/lib/util/compile.js:85:20)
node-pre-gyp ERR! stack     at ChildProcess.emit (node:events:513:28)
node-pre-gyp ERR! stack     at maybeClose (node:internal/child_process:1091:16)
node-pre-gyp ERR! stack     at ChildProcess._handle.onexit (node:internal/child_process:302:5)
node-pre-gyp ERR! System Linux 5.15.90.1-microsoft-standard-WSL2
node-pre-gyp ERR! command "/usr/local/bin/node" "/app/node_modules/@discordjs/node-pre-gyp/bin/node-pre-gyp" "install" "--fallback-to-build"
node-pre-gyp ERR! cwd /app/node_modules/@sapphire/type
node-pre-gyp ERR! node -v v18.16.1
node-pre-gyp ERR! node-pre-gyp -v v0.4.5
node-pre-gyp ERR! not ok
Failed to execute '/usr/local/bin/node /app/node_modules/node-gyp/bin/node-gyp.js configure --fallback-to-build --module=/app/node_modules/@sapphire/type/prebuild/type-node-v108-linux-x64-musl-1.2.4/type.node --module_name=type --module_path=/app/node_modules/@sapphire/type/prebuild/type-node-v108-linux-x64-musl-1.2.4 --napi_version=8 --node_abi_napi=napi --napi_build_version=0 --node_napi_label=node-v108' (1)

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Error type: Cannot find preset's package (github>whitesource/merge-confidence:beta)

[SFW-84] request: scan C files in with Sonar

Is there an existing issue or pull request for this?

  • I have searched the existing issues and pull requests

Feature description

In PR #580 we introduced sonar scanning to this project. However, for now we have disabled scanning of the C files because it requires additional setup as described on https://docs.sonarsource.com/sonarcloud/advanced-setup/languages/c-family/overview/. This request is an open ticket to the community for anyone with more advanced C knowledge to set up our pipelines so Sonar can scan the C code.

Desired solution

Updates to our GitHub action workflows for having Sonar scan C code. After building the C code unit tests should also be run with yarn test so a coverage report can be analyzed.

Alternatives considered

Rewriting the C code to Rust and using a tool such as https://gitlab.com/woshilapin/cargo-sonar to convert the output of Rust tooling to Sonar compatible tooling.

Additional context

No response

SFW-84

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.