dimButTries
dimButTries

Reputation: 886

Tips on making my functions more reusable

Any guidance or education would be very much appreciated.

I have an array of products name - with a lot of redundant and random phrases.

I thought I'd be smart and create small helper functions to extract the non-essential phrases and at the same time, I am dabbling in the "new" (for me) array functionality.

However, I have got myself stuck, and I do not know how best to proceed, in the most efficient route.

let sku = base_arr.map(function(word) {
    let result = (check_regex(word, regex_sku)) ? extract_regex(word, regex_sku) : word;
    return result;
});
let age = sku.map(function(word) {
    let result = (check_regex(word, regex_age)) ? extract_regex(word, regex_age) : word;
    return result;
});

If I add more helper functions such as remove whitespaces and do them not in sequential order.

My arrays start to cancel out some of the previous work.

I've tried to wrap the code in a function instead of condensing into small function expressions. However, that does not seem to work.

For example:

function extract_sku(arr) {
return arr.map(function(word) {
    let result = (check_regex(word, regex_sku)) ? extract_regex(word, regex_sku) : word;
    return result;
});
};

However, when I want to apply the second helper function, I get an undedefined.

console.log(extract_sku(extract_whitespace(base_arr)))

Depending on what array I am dealing with, I do not know what helper functions to apply.

Have I approached this all wrong? Should I have written a function to encapsulate all of this perhaps this.extract_sku = function(), copy the base array to a temp array, then use splice?

Any mentoring would be most helpful!

Upvotes: 0

Views: 69

Answers (3)

tv1st
tv1st

Reputation: 39

Just create a function, which will create a mapper for you:

function createRegexMapper(regex) {
    return word => check_regex(word, regex)
        ? extract_regex(word, regex)
        : word;
}

and then you can use it in a lot of mappings

arr.map(createRegexMapper(regex1))
   .map(createRegexMapper(regex2))
   .map(createRegexMapper(regex3))

Upvotes: 0

Arngue
Arngue

Reputation: 235

You can just pass an array of regex to replace in order and aquire the desired result

function extract(base_arr,regex_arr){
  let result;
  for(regex in regex_arr){
    base_arr.map(function(word) {
      result = (check_regex(word, regex)) ? extract_regex(word, regex) : word;
    }
  }
  return result;  
}

Then to aquire the desired result in your example case you shoud call

let age = extract(base_arr, [regex_sku, regex_age]);

Also you can just set the regex arrays as global variables

let ageRegex = [regex_sku, regex_age];

And a function getAge();

  function getAge(base_arr) {
    return extract(base_arr,ageRegex);
  }

Upvotes: 2

Nimer Awad
Nimer Awad

Reputation: 4199

You can create separated function, set different part as parameter and use it as call back inside map, something like:

function mapper (word, reg) {
    let result = (check_regex(word, reg)) ? extract_regex(word, reg) : word;
    return result;
}

let age = sku.map(word => mapper(word, regex_sku));

Upvotes: 1

Related Questions