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 🙏

© 2026 – Pkg Stats / Ryan Hefner

namedavatar

v1.2.0

Published

Avatar generated by name text based on svg dataURI.

Readme

named avatar

Avatar generated by name text based on svg. Fill <svg> as data uri into the <img> src, no added element.

如果用户没有个性头像,就填充用户名生成的<svg>头像。 程序会在 <img src> 上填充data URI,没有额外添加元素。

demo

Installtion

npm install namedavatar --save

Usage

namedavatar is a UMD module, support Browser, Requirejs, Vue2 directive, miniprogram(小程序).

Browser

online example

NOTE that the <img> width and border-radius: 100% requires you additional settings, the program is not set.

<img id="avatar1" src="./invalid.jpg" alt="Tom Hanks" width="40" style="border-radius: 100%">

<script src="/dist/namedavatar.min.js"></script>
<!-- also support requirejs
<script>
requirejs.config({
  paths: {
    namedavatar: '/dist/namedavatar'
  }
})
requirejs('namedavatar', function(namedavatar){
  // ...
})
</script>
-->
<script>
namedavatar.config({
  nameType: 'initials'
})

// fill single <img>
var img = document.getElementById('avatar1')
namedavatar.setImg(img, img.alt)

// fill multi <img>
var imgs = document.querySelectorAll('img[data-name]')
namedavatar.setImgs(imgs, 'data-name')
</script>

if img.src invalid, img#avatar1 will be:

<img id="avatar1" src="data:image/svg+xml,&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;32&quot; height=&quot;32&quot;&gt;&lt;rect fill=&quot;#9C27B0&quot; x=&quot;0&quot; y=&quot;0&quot; width=&quot;100%&quot; height=&quot;100%&quot;&gt;&lt;/rect&gt;&lt;text fill=&quot;#FFF&quot; x=&quot;50%&quot; y=&quot;50%&quot; text-anchor=&quot;middle&quot; alignment-baseline=&quot;central&quot; font-size=&quot;16&quot; font-family=&quot;Verdana, Geneva, sans-serif&quot;&gt;Hanks&lt;/text&gt;&lt;/svg&gt;">

without DOM

support like miniprogram(小程序)

import namedavatar from 'namedavatar'

// create svg html string without DOM
const svg = namedavatar.getSVGString('李连杰')
const uri = namedavatar.getDataURI(svg)
console.log(uri)
// data:image/svg+xml,%3Csvg%20...

Vue2

online example main.js

import { directive } from 'namedavatar/vue'
// register v-avatar="Tom Hanks", show firstName (default)
Vue.directive('avatar', directive);

// or set options and same as above
import namedavatarVue from 'namedavatar/vue'
// register v-avatar="Tom Hanks", show lastName
Vue.use(namedavatarVue, {
    nameType: 'lastName'
})

in vue template

<template>
  <img v-avatar="'Tom Hanks'" width="36"/>
</template>

API

.config(Object options)

| filed | type | default | description | | -------- | ------ | ------- | ---------------- | | nameType | string | 'firstName' | pick from: firstName, lastName, initials | | fontFamily | string | 'Verdana, Geneva, sans-serif' | font family | | backgroundColors | Array | Material Design colors *-500 | random background color list | | textColor | string | '#FFF' | name text color | | minFontSize | number | 8 | min font size limit | | maxFontSize | number | 16 | max font size limit |

.getSVG(string name, Object options)

  • name the full name value
  • options extended options

return <svg> node, get string by svg.outerHTML

.getSVGString(string name, Object options)

added at 1.1.0 version, special for without DOM like miniprogram(小程序)

  • name the full name value
  • options extended options

return <svg> html string

.setImg(HTMLImageElement img, string name)

  • img <img> item
  • name the full name value

create svg by name, and fill to <img src="data:image/svg+xml,<svg>...">

.setImgs(HTMLImageElement[] imgs, string attr)

  • imgs <img> list
  • attr pick full name value from special attr, eg 'alt', 'data-name'

create svg by attr value, batch processing setImg()

.getDataURI(string html)

  • html <svg> node html string

get data uri (rfc2397) of svg html

Contributing

  • IE > 8 (based on svg)
  • Continuous improvement, welcome review and suggest

development

npm install -g watchify
npm run dev

build assert

npm install -g browserify uglifyjs

# build UMD bundle and minify
npm run build && npm run minify

# build for vue
npm run build:vue

unit test:

npm install -g mocha
npm run test