Git Product home page Git Product logo

common's Introduction

common

Build Status Dependencies Status Coverage Status GitHub License

Commitizen Friendly code style: prettier Code Style npm version

Helper utilities for day to day development

Requirements

Installation

npm install --save @lykmapipo/common

Usage

import {
  compact,
  uniq,
  sortedUniq,
  pkg,
  scopesFor,
  abbreviate,
} from '@lykmapipo/common';

const a = [null, 1, '', undefined];
const b = compact(a); // => [ 1 ]
const x = { a: 1, b: '', c: undefined };
const y = compact(x); // => { a: 1 }

const a = [null, 1, 1, '', undefined, 2];
const b = uniq(a); // => [ 1, 2 ]
const x = { a: 1, b: '', c: undefined };
const y = uniq(x); // => { a: 1 }

const a = [null, 1, 2, '', undefined, 1];
const b = sortedUniq(a); // => [ 1, 2 ]
const x = { a: 1, b: '', c: undefined };
const y = sortedUniq(x); // => { a: 1 }

const { name, description } = pkg(); // => { name: ..., version: ...}

const scopes = scopesFor('user', 'payment'); // => ['user:create', 'payment:create'];

const abbreaviation = abbreviate('Ministry of Finance'); // => MOF

Test

  • Clone this repository

  • Install all dependencies

npm install
  • Then run test
npm test

Contribute

It will be nice, if you open an issue first so that we can know what is going on, then, fork this repo and push in your ideas. Do not forget to add a bit of test(s) of what value you adding.

Licence

The MIT License (MIT)

Copyright (c) lykmapipo & Contributors

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

common's People

Contributors

dependabot[bot] avatar lykmapipo avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

jdcumptonjr

common's Issues

Cannot use in ESM project

Trying to use mongoose-gridfs in a project gives me this error

$ node dist/index.js
Debugger attached.
Waiting for the debugger to disconnect...
S:\private-gpt-backend\node_modules\@lykmapipo\common\lib\index.js:7
const mime = require('mime');
             ^

Error [ERR_REQUIRE_ESM]: require() of ES Module S:\private-gpt-backend\node_modules\mime\dist\src\index.js from S:\private-gpt-backend\node_modules\@lykmapipo\common\lib\index.js not supported.
Instead change the require of S:\private-gpt-backend\node_modules\mime\dist\src\index.js in S:\private-gpt-backend\node_modules\@lykmapipo\common\lib\index.js to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (S:\private-gpt-backend\node_modules\@lykmapipo\common\lib\index.js:7:14) {
  code: 'ERR_REQUIRE_ESM'
}

Node.js v20.11.0

Look, I'll be honest: I'm not entirely sure how or why is it giving me that... CommonJS/ESM wizardry is kind of a mystery to me.
All I know is I tried to fix it in so many different ways without touching npm imports but I couldn't.. so I wrote a script to edit this line on my end until you can update this...

Issue with the latest version of parse-json dependency

Hello,

I am facing an issue which is basically the same as this one but with the parse-json dependency.

Four days ago, parse-json has been released on version 6.0.0. The package.json of this library includes the following dependency:
"parse-json": ">=5.2.0",

However, trying to import the parse-json library, the following error is raised:

require() of ES modules is not supported.
require() of /home/node/wks/api/node_modules/parse-json/index.js from /home/node/wks/api/node_modules/@lykmapipo/common/lib/index.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename /home/node/wks/api/node_modules/parse-json/index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /home/node/wks/api/node_modules/parse-json/package.json.

    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1085:13)
    at Module.load (internal/modules/cjs/loader.js:933:32)
    at Function.Module._load (internal/modules/cjs/loader.js:774:14)
    at Module.require (internal/modules/cjs/loader.js:957:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.<anonymous> (/home/node/wks/api/node_modules/@lykmapipo/common/lib/index.js:13:19)
    at Module._compile (internal/modules/cjs/loader.js:1068:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
    at Module.load (internal/modules/cjs/loader.js:933:32)
    at Function.Module._load (internal/modules/cjs/loader.js:774:14)
    at Module.require (internal/modules/cjs/loader.js:957:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.<anonymous> (/home/node/wks/api/node_modules/@lykmapipo/mongoose-common/lib/index.js:5:16)
    at Module._compile (internal/modules/cjs/loader.js:1068:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
    at Module.load (internal/modules/cjs/loader.js:933:32)
    at Function.Module._load (internal/modules/cjs/loader.js:774:14)
    at Module.require (internal/modules/cjs/loader.js:957:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.<anonymous> (/home/node/wks/api/node_modules/mongoose-gridfs/lib/index.js:6:24)
    at Module._compile (internal/modules/cjs/loader.js:1068:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10) {
    code: 'ERR_REQUIRE_ESM'
    }

Node version : v14.17.0

Forcing the dependency to 5.2.0 version should solve the issue.
"parse-json": "5.2.0",

Temporary fix (using yarn) :

Add the following lines at the end of your package.json file :

  "resolutions": {
    "**/@lykmapipo/common/parse-json": "5.2.0"
  }

If you are using the mongoose-gridfs package, you should instead use

  "resolutions": {
    "mongoose-gridfs/@lykmapipo/mongoose-common/@lykmapipo/common/parse-json": "5.2.0"
  }

More information about resolutions : https://newbedev.com/how-do-i-override-nested-dependencies-with-yarn

Latest version of parse-ms 3.0.0 breaks common

We are using the mongoose-gridfs module, that has a dependency on lykmapipo/common. This version includes parse-ms with the following syntax in package.json: "parse-ms": ">=2.1.0"

Today - parse-me introduced version 3.0.0 of their code which breaks in index.js of common because they now require you to use the import command instead of the require command.

To stay compatible for other programs that depend on mongoose-gridfs and the like, if package.json instead used
"parse-ms": "2.1.0" this would not be a problem anymore.

The following code reproduces the problem:

const mongooseGridFs = require('mongoose-gridfs');

package.json has

"mongoose-gridfs": "1.2.48"

Using node 12.6.0 produces the following call stack:
export default function parseMilliseconds(milliseconds) {
^^^^^^

SyntaxError: Unexpected token export
at Module._compile (internal/modules/cjs/loader.js:720:23)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:643:32)
at Function.Module._load (internal/modules/cjs/loader.js:556:12)
at Module.require (internal/modules/cjs/loader.js:683:19)
at require (internal/modules/cjs/helpers.js:16:16)
at Object. (/opt/subtask/node_modules/@lykmapipo/common/lib/index.js:18:27)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:643:32)
at Function.Module._load (internal/modules/cjs/loader.js:556:12)
at Module.require (internal/modules/cjs/loader.js:683:19)
at require (internal/modules/cjs/helpers.js:16:16)
at Object. (/opt/subtask/node_modules/@lykmapipo/mongoose-common/index.js:31:47)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:643:32)
at Function.Module._load (internal/modules/cjs/loader.js:556:12)
at Module.require (internal/modules/cjs/loader.js:683:19)
at require (internal/modules/cjs/helpers.js:16:16)
at Object. (/opt/subtask/node_modules/mongoose-gridfs/lib/schema.js:6:41)
at Module._compile (internal/modules/cjs/loader.js:776:30)

Using node 13.2.0 produces the following call stack:

(node:259) Warning: require() of ES modules is not supported.
require() of /opt/subtask/node_modules/parse-ms/index.js from /opt/subtask/node_modules/@lykmapipo/common/lib/index.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename /opt/subtask/node_modules/parse-ms/index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /opt/subtask/node_modules/parse-ms/package.json.
internal/modules/cjs/loader.js:1156
throw new ERR_REQUIRE_ESM(filename);
^

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /opt/subtask/node_modules/parse-ms/index.js
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1156:13)
at Module.load (internal/modules/cjs/loader.js:976:32)
at Function.Module._load (internal/modules/cjs/loader.js:884:14)
at Module.require (internal/modules/cjs/loader.js:1016:19)
at require (internal/modules/cjs/helpers.js:69:18)
at Object. (/opt/subtask/node_modules/@lykmapipo/common/lib/index.js:18:27)
at Module._compile (internal/modules/cjs/loader.js:1121:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1160:10)
at Module.load (internal/modules/cjs/loader.js:976:32)
at Function.Module._load (internal/modules/cjs/loader.js:884:14)
at Module.require (internal/modules/cjs/loader.js:1016:19)
at require (internal/modules/cjs/helpers.js:69:18)
at Object. (/opt/subtask/node_modules/@lykmapipo/mongoose-common/index.js:31:47)
at Module._compile (internal/modules/cjs/loader.js:1121:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1160:10)
at Module.load (internal/modules/cjs/loader.js:976:32) {
code: 'ERR_REQUIRE_ESM'
}

Thank you for your help.

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.