rikschennink / fitty

✨ Makes text fit perfectly
https://rikschennink.github.io/fitty/
MIT License
3.72k stars 211 forks source link

RFE: support array/element list as fitty() argument #54

Closed sgbeal closed 3 years ago

sgbeal commented 4 years ago

fitty() current accepts a selector or single element as its first argument. "It would be really cool" if it also accepted any container which has a forEach method, e.g. an array or querySelectorAll() result, which fitty could then internally transform into an array for its own use.

Something along the lines of (untested):

  function fitty(target, options = {}) {
    if('string' === typeof target){
        return fittyCreate( toArray( document.querySelectorAll(target) ), options);
    }else if('function' === typeof target.forEach){
        const a = [];
        target.forEach((x)=>a.push(x));
        return fittyCreate(a, options);
        // perhaps simply toArray(target) would do the trick?
    }else{
        return  fittyCreate([target], options)[0];
    }
  }
rikschennink commented 4 years ago

Would be nice indeed, I think this should do it, happy to accept a PR

// fitty creation function
function fitty(target, options = {}) {

  // if target is a string
  return typeof target === 'string' ?

    // treat it as a querySelector
    fittyCreate( toArray( document.querySelectorAll(target) ), options) :

    // create single fitty
    fittyCreate( target.forEach ? toArray( target ) : [target], options)[0];
}
sgbeal commented 4 years ago

Other than cloning the occasional repo, i don't do git - i'm a developer on the Fossil SCM project ;). The overhead of figuring out how to do a PR seems like overkill, considering there's a trivial 4-line implementation above.

rikschennink commented 3 years ago

Closed because inactive