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

object-score

v1.1.2

Published

A library to score objects based on validators.

Downloads

11

Readme

Object Score

A simple way to score how well an object matches a model.

Usage:

  • Install the library npm install object-score.
  • Optionally install the library validator for a collection of pre-made validators using npm install validator.
const validator = require('validator');
const { ScoringModel } = require('object-score');

const EXAMPLE_DATA = [{
  username: 'jdiggity',
  firstName: 'Joe',
  lastName: 'Diggity',
  email: '[email protected]',
  password: '5f4dcc3b5aa765d61d8327deb882cf99'
}, {
  username: 'franci$',
  firstName: 'Frank',
  email: 'fakeaddress'
}];

const userModel = new ScoringModel('User');

userModel
  .field('username', 0.2)
  .validator(validator.isAlphanumeric, 'Username must be alphanumeric')
  .validator((value) => value.length > 3, 'Username must be at least 3 characters');

userModel
  .field('firstName', 0.15)
  .validator(validator.isAlphanumeric, 'First name must be alphanumeric');

userModel
  .field('lastName', 0.15)
  .validator(validator.isAlphanumeric, 'Last name must be alphanumeric');

userModel
  .field('email', 0.2)
  .validator(validator.isEmail, 'Email must be a valid email address');

userModel
  .field('password', 0.3)
  .validator(validator.isMD5, 'Password MUST be hashed');

scoredData = EXAMPLE_DATA.map((user) => {
  user['score'] = userModel.score(user);
  return user;
})
console.log(JSON.stringify(scoredData, null, 2));

EXAMPLE_DATA.forEach((user) => {
  const result = userModel.score(user, true);
  console.log(JSON.stringify(result, null, 2))
});

Output without messages

[
  {
    "username": "jdiggity",
    "firstName": "Joe",
    "lastName": "Diggity",
    "email": "[email protected]",
    "password": "5f4dcc3b5aa765d61d8327deb882cf99",
    "score": 1
  },
  {
    "username": "franci$",
    "firstName": "Frank",
    "email": "fakeaddress",
    "score": 0
  }
]

Output with messages

{
  "score": 1,
  "fields": {
    "username": {
      "score": 0.2,
      "messages": [],
      "valid": true
    },
    "firstName": {
      "score": 0.15,
      "messages": [],
      "valid": true
    },
    "lastName": {
      "score": 0.15,
      "messages": [],
      "valid": true
    },
    "email": {
      "score": 0.2,
      "messages": [],
      "valid": true
    },
    "password": {
      "score": 0.3,
      "messages": [],
      "valid": true
    }
  },
  "valid": true
}
{
  "score": 0,
  "fields": {
    "username": {
      "score": 0,
      "messages": [
        "Username must be alphanumeric"
      ],
      "valid": false
    },
    "firstName": {
      "score": 0.15,
      "messages": [],
      "valid": true
    },
    "lastName": {
      "score": 0,
      "messages": [
        "Field lastName is not defined."
      ],
      "valid": true
    },
    "email": {
      "score": 0.1,
      "messages": [
        "Email must be a valid email address"
      ],
      "valid": true
    },
    "password": {
      "score": 0,
      "messages": [
        "Field password is not defined."
      ],
      "valid": true
    }
  },
  "valid": false
}

To do

  • Add way to handle nested objects
  • Choose the weighting distribution per validator as well as per field.

License (MIT)

Copyright (c) 2017 Mark Niehe <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.