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

jotun

v0.0.4

Published

convert objects to number arrays

Downloads

11

Readme

jotun

convert javascript object to numerical 1-D array (vector) representation

jotun

Jotun is the (anglicized) term used to describe the race of Giants living in Jotunheimr, one of the nine worlds in Norse mythology. In this case it also means Javascript Objects To Numbers (however jotn sounded stupid and was only one u away from keeping in line with all the other norse-themed repositories I have).

but why????

Why on Midgard would you want to turn objects into vectors?

  1. because a vector can be used in machine learning.

Pretty much that's it, but you can also use it for serialization purposes, or even create ArrayBuffers to be used in TypedArrays so you can use DataViews?

usage

Simple enough:

let jotun = require('jotun'),
  convert = jotun.converter();

let vector = convert({ foo: 'bar', quux: [2, 3, 4]});
// print [3, 2, 3, 4]

default conversion

jotun will turn your objects into vectors with default converters following this behaviour:

  1. numbers will be left untouched (except for NaN and Infinity)
  2. strings will be converted to their length
  3. booleans will be converted to 1 (true) and 0 (false)
  4. Dates will be converted to their getTime() value
  5. Objects will be converted to an array in which primitives are converted according to the above
  6. undefined will turn to 0

The final resulting array will be flattened.

custom conversions

You may want to cater for special cases, like NaN or Infinity or undefined. You can specify a map which defines the conversion behaviour. All you need to do is associate a conversion function for the object property. For instance, example/example.es6.js includes a conversion of text into bag-of-words vector representation:

let jotun = require('../index'),
  mimir = require('mimir');

let list = [{
  name: 'joe',
  age: 40,
  dob: new Date(1975, 1, 28),
  description: 'a javascript programmer that loves heavy metal',
  contractor: true,
  code: {
    languages: ['javascript', 'java', 'c++'],
    foo: 'bar'
  }
}, {
  name: 'jack',
  age: 25,
  dob: new Date(1990, 2, 20),
  description: 'an ios programmer that pretends to like jazz',
  contractor: false,
  code: {
    languages: ['objective-c', 'some other hipsteria language', 'ruby'],
    foo: 'bar'
  }
}];

let dict = mimir.dict(list.map(o => o.description));

let convert = jotun.converter({
  description: description => mimir.bow(description, dict),
  code: jotun.converter({})
});