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

obj-append-strings

v1.1.2

Published

A simple js function for appending strings to the end of values in an object

Downloads

6

Readme

obj-append-strings

npm install obj-append-strings --save

This is a tiny function for bulk appending strings to other strings through the use of objects.

So basically instead of doing this:

function functionName(settings){

  //This is ugly
  settings = settings || {};
  settings.setting_1 = settings.setting_1 + ' c';
  settings.setting_2 = settings.setting_2 || {};
  settings.setting_2.alpha = settings.setting_2.alpha  + ' d';

  return settings;
}

var variable = functionName({
  setting_1: 'a',
  setting_2: {
    alpha: 'b'
  },
});

//variable = { setting_1 : 'a c', setting_2 : { alpha: 'b d' } }

You can avoid the ugly repetition by doing this instead:

var appendStrings = require('obj-append-strings');

function functionName(settings){

  //This is pretty
  settings = appendStrings(settings, {
    setting_1: ' c',
    setting_2: {
      alpha: ' d'
    },
  });

  return settings;
}

var variable = functionName({
  setting_1: 'a',
  setting_2: {
    alpha: 'b'
  },
});

//variable = { setting_1 : 'a c', setting_2 : { alpha: 'b d' } }

Or if you want to prep-end the strings instead:

var appendStrings = require('obj-append-strings');

function functionName(settings){

  //This adds the strings before the original value
  settings = appendStrings(settings, {
    setting_1: 'c ',
    setting_2: {
      alpha: 'd ',
    },
  }, 'before');

  return settings;
}

var variable = functionName({
  setting_1: 'a',
  setting_2: {
    alpha: 'b'
  },
});


//variable = { setting_1 : 'c a', setting_2 : alpha: { 'd b' } }

If you can 100% guarantee that the object having the strings appended to it is an already defined object (not "undefined") then you can leave off the settings = bit:

  //leave off the "settings =" bit if you can 100% guarantee that "settings" is already defined
  appendStrings(settings, {
    setting_1: 'c ',
    setting_2: {
      alpha: 'd ',
    },
  });

If there are values that are undefined, it will just use the values that are available

var appendStrings = require('obj-append-strings');

function functionName(settings){

  //This adds the strings before the original value
  settings = appendStrings(settings, {
    setting_1: 'c ',
    setting_2: {
      alpha: 'd ',
    },
  });

  return settings;
}

var variable_1 = functionName({
  setting_2: {
    alpha: 'b'
  },
});


var variable_2 = functionName({
  setting_1: 'a',
});


//variable_1 = { setting_1 : 'c ', setting_2 : alpha: { 'd b' } }
//variable_2 = { setting_1 : 'a c', setting_2 : alpha: { 'd ' } }

It also works the other way around (yeah the code in this example is a bit silly)

var appendStrings = require('obj-append-strings');

function functionName(settings){

  //This adds the strings before the original value
  settings = appendStrings(settings, {
  });

  return settings;
}

var variable = functionName({
  setting_1: 'a',
  setting_2: {
    alpha: 'b'
  },
});

//variable = { setting_1 : 'a', setting_2 : alpha: { 'b' } }

Pug usage

I primarily built this function for use in Pug templates as a way of assigning permanent classes to sub modules.

To use the function in pug you will need to parse the require function from node.js into the Pug locals object.

If you're using Gulp, then this is a simplified version of the setup you would use to compile Pug templates that have support for the require node.js function:

gulp.src('**/*.pug')
  .pipe(plugins.pug({
    locals: {
      //this bit gives access to the "require" function from inside pug templates
      require: require,
    }
  }))

Once you have the require function available inside your pug templates, you can use the function in pug mixins like this to assign permanent classes to sub modules. The below example uses default-to to make the syntax simpler.

include path/to/subModule1
include path/to/subModule2

- var defaultTo = require('default-to').default;
- var appendStrings = require('obj-append-strings');

mixin example(spec)
  -
    //Any classes in here will be lost if the user defines their own classes
    defaultTo(spec, {
      classes : '',
      subModule1 : {
        classes : 'overridable-classes'
      },
      subModule2 : {
        classes : 'overridable-classes'
      }
    });

    //classes that are placed here are impossible for the user to override remotely
    spec = appendStrings(spec, {
      subModule1: {
        classes: ' permanent-classes'
      },
      subModule2: {
        classes: ' permanent-classes'
      }
    })

  .example(class=spec.classes)&attributes(attributes)
    +subModule1(spec.subModule1)
    +subModule2(spec.subModule2)

This is how that would look when calling the Pug mixin

include path/to/example

+example({
  classes: 'module-classes',
  subModule1: {
    classes: 'subModule1-overide-classes',
  },
  subModule2: {
    classes: 'subModule2-overide-classes',
  }
})