Code Monkey home page Code Monkey logo

array-mixer's Introduction


Logo ArrayMixer

This repository contains the ArrayMixer source code. ArrayMixer is a tiny javascript lib with less than 1kb made to help ordering groups of arrays in a very personalized manner. Powerful and easy to use.

Table of contents

Common usage

Let's think we have two arrays: photos and ads.

photos.length === 12; // true
ads.length === 6; // true

Use ArrayMixer to create a new array containing 2 photos followed by 1 ad until the end of both arrays.

let mixedArray = ArrayMixer({P:photos, A:ads}, ["2P", "1A"]);

So mixedArray will contain:

Installation

ArrayMixer can be used in node projects and web projects.

Node projects

Requires node version 5.7 or later.

npm install array-mixer --save

Import it to your code using:

const ArrayMixer = require("array-mixer");

Web projects

Download latest ES5 transpiled version from unpkg.com.

Import ES5 transpiled version to your code.

<script src="https://unpkg.com/[email protected]/release/array-mixer.js"></script>

Parameters

ArrayMixer has only two mandatory parameters.

let aliases = {M:myArray, O:otherArray};
let sequence = ["3M", "5O"];

let mixed = ArrayMixer(aliases, sequence);

Aliases

This parameter should be an object with keys used as alias for sequence and key values pointing to avaliable arrays.

Sequence

This parameters uses the aliases defined on aliases parameter to create a sequence order to display the arrays.

Examples

ArrayMixer can be used combining different arrays, aliases and sequences. The following examples shows some ways to use it.

Example 1) For every 7 photos display an ad:

ArrayMixer({F: Photos, A: Ads}, ["7P", "1A"]);

or (as number 1 on sequence can be ommited):

ArrayMixer({F: Photos, A: Ads}, ["7P", "A"]);

Example 2) For every 4 paragraphs of text include 2 images:

ArrayMixer({P: paragraphs, I: images}, ["4P", "2I"]);

Example 3) In a group of 8 related links reserve positions 5 and 6 for sponsored links:

ArrayMixer({R: related, S: sponsored}, ["4R", "2S", "2R"]);

Example 4) Display a list of songs including the most successful songs for every 10 songs:

ArrayMixer({M: musics, H: hits}, ["10M", "2H"]);

Example 5) You can also use larger aliases and the ES6 object shorthand:

ArrayMixer({days, weekend}, ["5days", "2weekend"]);

You can manipulate more than two vectors at a time, as in the following example:

Example 6) View photos of puppies, kittens and penguins in sequence:

let mixed = ArrayMixer({puppies, kittens, penguins}, ["puppies", "kittens", "penguins"));
puppies kittens penguins mixed
[๐Ÿถ, ๐Ÿถ, ๐Ÿถ] [๐Ÿฑ, ๐Ÿฑ, ๐Ÿฑ] [๐Ÿง, ๐Ÿง, ๐Ÿง] [๐Ÿถ, ๐Ÿฑ, ๐Ÿง, ๐Ÿถ, ๐Ÿฑ, ๐Ÿง, ๐Ÿถ, ๐Ÿฑ, ๐Ÿง]

Example 7) Include 1 large photo for every 2 medium size photos followed by 3 small photos:

Tip: ArrayMixer lets you mix three or more arrays at once.

ArrayMixer({L:large, M:medium, S:small}, ["2M", "3S", "L"]);

Disclaimer: All arrays mentioned in this section must exist for the examples to work.

More examples

For more example please check the specification file.

Contributing

You may contribute in several ways like creating new features, fixing bugs, improving documentation and examples or translating any document here to your language. Find more information in CONTRIBUTING.md.

License

MIT - Jota Teles - 2017

Special thanks

array-mixer's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

array-mixer's Issues

Add scripts to generate release files

ArrayMixer is written in ES6 syntax.
Would be nice to provide a ES5 version and a ES5 minified of it so users could use this package easily and in many ways.

Add unit testing to project

It is necessary to have a spec for the ArrayMixer function.
This spec should show all the commons uses for the function and guarantee the correct output.

Refactor code to eliminate unnecessary variables

The code doesn't need some of the variables that it is using.
These variables are just created to keep states just like counters, but many of them are unnecessary.

The unnecessary variables are:

let index currentAliasName, currentAliasArray, currentAliasIndex, currentAliasCounterValue;

docs: fix image related issues on README

There are some issue regarding images on README file, such as:

Blocks not aligned with texts:
Screenshot 2024-05-29 at 06 46 31

Blocks stuck to the blocks below
Screenshot 2024-05-29 at 06 46 37

Hard to read text in "Sequence"
Screenshot 2024-05-29 at 06 46 44

Missing logo
Screenshot 2024-05-29 at 06 46 49

Includes eslint to the project

Including eslint to the project will help mantain code quality.
The rules of eslint can be the easier rules to set and somewhere in future these rules can be overriden.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.