Skip to content

Instantly share code, notes, and snippets.

@anubra266
Last active June 1, 2021 15:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anubra266/146796c466c3021f7d9de7b6f3d013ee to your computer and use it in GitHub Desktop.
Save anubra266/146796c466c3021f7d9de7b6f3d013ee to your computer and use it in GitHub Desktop.
Filter deps

Get deps with

npx npm-remote-ls --flatten @chakra-ui/react -d false -o false

NB: Apostrophe must be double quotes

const deps = [
  "@chakra-ui/react@1.6.3",
  ...
];

const filteredDeps = [...new Set(deps)]

const getO = (str)=>{

var regex = /@/gi, result, indices = [];
while ( (result = regex.exec(str)) ) {
    indices.push(result.index);
}
return indices
}
 


const depObject = filteredDeps.reduce((acc,nxt)=>{
	const atPoses = getO(nxt)
  const atPos = atPoses[atPoses.length-1]
  const vStart = atPos+1
  const version = nxt.slice(vStart)
  const fCharIsLetter = nxt[0]!=='"'
  
  const package = nxt.slice(fCharIsLetter?0: 1,atPos)
  acc[`"${package}"`]= version
  return acc
},{})


console.log(depObject)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment