Coder Social home page Coder Social logo

molefrog / srcset-parse Goto Github PK

View Code? Open in Web Editor NEW
14.0 2.0 0.0 14 KB

๐Ÿ™ Extra tiny `srcset` attribute parser ~150B

Home Page: https://www.npmjs.com/package/srcset-parse

TypeScript 100.00%
responsive-images srcset zero-dependency micro-library images

srcset-parse's Introduction

srcset-parse

An extra small srcset attribute parser compliant with the latest spec. It's is inspired by the srcset package and has following features:

  • Extra tiny, only ~150B;
  • Supports URLs that contain commas;
  • Zero or one descriptors per image definitions are supported (like 2x or 100w, but not both!);

Example usage:

import parse from "srcset-parse";

/**
 * [
 *   { url: "hifi-cat.jpeg",  density: 3 },
 *   { url: "lowfi-cat.jpeg", width: 128 },
 * ]
 */
parse("hifi-cat.jpeg 3x, lowfi-cat.jpeg 128w");

Using with TypeScript

The library is written in TypeScript, so you can import types if needed:

import parse, { ImageCandidate } from "srcset-parse";

const result: ImageCandidate[] = parse("[email protected] 2x, icon.png 1x");

Platform support

This library is written according to the ES2015 standard. Make sure your platform supports it, or your project is configured to transpile external modules.

srcset-parse's People

Contributors

molefrog avatar

Stargazers

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

Watchers

 avatar  avatar

srcset-parse's Issues

srcset with no space after the comma

I'm trying to understand if this is the correct behavior - when there is no space after the comma the parsing fails.
The following will fail with 'Invalid srcset descriptor':

srcset.parse("banner-HD.jpg 2x,banner-phone.jpg 100w");

Due to the regular expression in the code: string.split(/,\s+/)

Changing to string.split(/,\s*/)

Will fix the problem.

Is that on purpose?

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.