npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

solve-conversion-path

v1.1.0

Published

Given an array of converters with a 'from' and 'to' property and an array of available formats in strings, find the smallest number of conversions of the available formats required, in order to reach the requested format

Downloads

3

Readme

solve-conversion-path

Install

Via npm:

npm i solve-conversion-path

Usage

Solve-conversion-path was developed for a js color functions library, but it is not restricted to operations on color conversion paths.

This conversion/transform path solver expects to be given three things:

  1. An array of "converters" with "from" and "to" properties. (eg: [{ convert() {}, from: 'hsv', to: 'cmyk' }, ...])
  2. An array of already available "formats". Essentially starting points for the conversion. (eg: ['hex', 'rgb', 'hsl'])
  3. A string representing the desired output "format". (eg: 'cmyk')

If the conversion from the available "formats" to the desired output "format" is possible, the function will return an array with the fewest steps needed to transform one of the already available "formats" into the desired output. (eg: [ { from: 'rgb', to: 'hsv' }, { from: 'hsv', to: 'cmyk' } ])

It is then simple to transform to the desired "format" using Array.prototype.reduce. eg:

const color['cmyk'] = conversionPath.reduce(
	(output, converter) => converter.convert(output),
	color[conversionPath[0].from]
);

Example

import solveConversionPath from 'solve-conversion-path';

const availableFormats = [ 'lab' ];

const converters = [
	{ convert() {}, from: 'rgb', to: 'hex' },
	{ convert() {}, from: 'hex', to: 'rgb' },
	{ convert() {}, from: 'rgb', to: 'hsl' },
	{ convert() {}, from: 'hsl', to: 'rgb' },
	{ convert() {}, from: 'rgb', to: 'lab' },
	{ convert() {}, from: 'lab', to: 'rgb' },
	{ convert() {}, from: 'cmyk', to: 'hex' },
	{ convert() {}, from: 'hex', to: 'cmyk' },
	{ convert() {}, from: 'hsv', to: 'cmyk' },
	{ convert() {}, from: 'cmyk', to: 'hsv' },
];

console.log(
	'solved:',
	JSON.stringify(
		solveConversionPath(converters, availableFormats, 'hsv'),
		null,
		2
	)
);

/* OUTPUTS in console:
solved: [
  {
    "from": "lab",
    "to": "rgb"
  },
  {
    "from": "rgb",
    "to": "hex"
  },
  {
    "from": "hex",
    "to": "cmyk"
  },
  {
    "from": "cmyk",
    "to": "hsv"
  }
]
*/

License

MIT