Convert an Array of File Require Statements into File Names

When building scripts, it’s a common requirement to remove and update multiple items in a collection of strings. In this guide we’re going to mimic the process needed to take a list of require statements and generate a list of file names.

Summary

Implement a method that generates a list of file names based on a collection of require statements.

Exercise File

Code File

Exercise Description

Build a method that takes in an array of require statements and outputs an array of the file names with their JavaScript extension added.

Example

javascript_files = [
  '//= require custom/scripts',
  '//= require base/defaults',
  '//= require html.sortable',
  '//= require moment',
  '//= require test-styles'
]

append_js_file_extension javascript_files

# Output
[
  'custom/scripts.js',
  'base/defaults.js',
  'html.sortable.js',
  'moment.js',
  'test-styles.js'
]

Real World Usage

Imagine that you were asked to build a feature that took in the contents of a file and you had to both remove and add content for each array element. This is a common requirement for tasks such as: building file parsers, developing view helper methods, and many more features.

Solution

Can be found on the solutions branch on github.

Jordan Hudgens

view all post
Leave a comment

Please be polite. We appreciate that.

By Daniele Zedda • 18 February

← PREV POST

By Daniele Zedda • 18 February

NEXT POST → 34
Share on