'command-line interface in Node.js

The goal is to write a command-line interface in Node.js. This program has to filter a list of elements containing a pattern.

Details:

  • In the following file data.js, there are Countries containing Peoples containing Animals.
  • Only animals containing the pattern passed as argument (e.g. ry) are displayed. The order should be kept intact.
  • Empty array after filtering is NOT returned.

Sample of running the command, and its output:

$ node app.js --filter=ry
[
  {
    name: 'Uzuzozne',
    people: [
      {
        name: 'Lillie Abbott',
        animals: [
          {
            name: 'John Dory'
          }
        ]
      }
    ]
  },
  {
    name: 'Satanwi',
    people: [
      {
        name: 'Anthony Bruno',
        animals: [
          {
            name: 'Oryx'
          }
        ]
      }
    ]
  }
]

Count

The next goal is to print the counts of People and Animals by counting the number of children and appending it in the name, eg. Satanwi [2].

Sample of running the command, and its output:

node app.js --count
[ { name: 'Dillauti [5]',
    people:
     [ { name: 'Winifred Graham [6]',
         animals:
          [ { name: 'Anoa' },
            { name: 'Duck' },
            { name: 'Narwhal' },
            { name: 'Badger' },
            { name: 'Cobra' },
            { name: 'Crow' } ] },
       { name: 'Blanche Viciani [8]',
         animals:
          [ { name: 'Barbet' },
            { name: 'Rhea' },
            { name: 'Snakes' },
            { name: 'Antelope' },
            { name: 'Echidna' },
            { name: 'Crow' },
            { name: 'Guinea Fowl' },
            { name: 'Deer Mouse' } ] },
     
]
## Requirements
- No library/modules should be used, except for the testing library
##DATA 
const data = [
  {
  name: 'Dillauti',
  people:
    [{
      name: 'Winifred Graham',
      animals:
        [{name: 'Anoa'},
          {name: 'Duck'},
          {name: 'Narwhal'},
          {name: 'Badger'},
          {name: 'Cobra'},
          {name: 'Crow'}]
    },
      {
        name: 'Blanche Viciani',
        animals:
          [{name: 'Barbet'},
            {name: 'Rhea'},
            {name: 'Snakes'},
            {name: 'Antelope'},
            {name: 'Echidna'},
            {name: 'Crow'},
            {name: 'Guinea Fowl'},
            {name: 'Deer Mouse'}]
      },
      {
        name: 'Philip Murray',
        animals:
          [{name: 'Sand Dollar'},
            {name: 'Buzzard'},
            {name: 'Elephant'},
            {name: 'Xenops'},
            {name: 'Dormouse'},
            {name: 'Anchovy'},
            {name: 'Dinosaur'}]
      },
      {
        name: 'Bobby Ristori',
        animals:
          [{name: 'Kowari'},
            {name: 'Caecilian'},
            {name: 'Common Genet'},
            {name: 'Chipmunk'},
            {name: 'Aardwolf'},
            {name: 'Przewalski\'s Horse'},
            {name: 'Badger'},
            {name: 'Sand Cat'},
            {name: 'Linne\'s Two-toed Sloth'}]
      },
      {
        name: 'Louise Pinzauti',
        animals:
          [{name: 'Manta Ray'},
            {name: 'Nubian Ibex'},
            {name: 'Warbler'},
            {name: 'Duck'},
            {name: 'Mice'}]
      }]
},
  {
    name: 'Tohabdal',
    people:
      [{
        name: 'Effie Houghton',
        animals:
          [{name: 'Zebra'},
            {name: 'Ring-tailed Lemur'},
            {name: 'Fly'},
            {name: 'Blue Iguana'},
            {name: 'Emu'},
            {name: 'African Wild Ass'},
            {name: 'Numbat'}]
      },
        {
          name: 'Essie Bennett',
          animals:
            [{name: 'Aldabra Tortoise'},
              {name: 'Patagonian Toothfish'},
              {name: 'Giant Panda'},
              {name: 'Goat'},
              {name: 'Quahog'},
              {name: 'Collared Lemur'},
              {name: 'Aldabra Tortoise'}]
        },
        {
          name: 'Owen Bongini',
          animals:
            [{name: 'Zebrashark'},
              {name: 'Dogs'},
              {name: 'Mouse'},
              {name: 'Numbat'},
              {name: 'African Wild Dog'}]
        },
        {
          name: 'Alexander Fleury',
          animals:
            [{name: 'Gelada'},
              {name: 'Rattlesnake'},
              {name: 'Rabbit'},
              {name: 'Aardvark'},
              {name: 'Duck'},
              {name: 'Courser'},
              {name: 'Woodpecker'}]
        },
        {
          name: 'Curtis Fuchs',
          animals:
            [{name: 'Squirrel'},
              {name: 'Falcon'},
              {name: 'Cat'},
              {name: 'Lobe Coral'},
              {name: 'Camel'},
              {name: 'Broadclub Cuttlefish'}]
        },
        {
          name: 'Maud Lorenzo',
          animals:
            [{name: 'Bush Dog'},
              {name: 'Sea Urchin'},
              {name: 'Gayal'},
              {name: 'Tortoise'},
              {name: 'Meerkat'},
              {name: 'Lion'},
              {name: 'Gecko'}]
        },
        {
          name: 'Linnie Lamb',
          animals:
            [{name: 'Burro'},
              {name: 'African Wild Dog'},
              {name: 'Slender Snipe Eel'},
              {name: 'Red Panda'},
              {name: 'Baby Doll Sheep'},
              {name: 'California Sea Lion'},
              {name: 'Rabbits'}]
        },
        {
          name: 'Randall Benoît',
          animals:
            [{name: 'Chameleons'},
              {name: 'Bee-eater'},
              {name: 'King Vulture'},
              {name: 'Giant Isopod'},
              {name: 'Sand Cat'}]
        }]
  },
  {
    name: 'Uzuzozne',
    people:
      [{
        name: 'Harold Patton',
        animals:
          [{name: 'Bearded Dragon'},
            {name: 'Chicken'},
            {name: 'Sand Cat'},
            {name: 'Hedgehog'},
            {name: 'Collared Lemur'},
            {name: 'Frogmouth'},
            {name: 'Raccoon dog'},
            {name: 'Shortfin Mako Shark'}]
      },
        {
          name: 'Millie Lapini',
          animals:
            [{name: 'Bearded Dragon'},
              {name: 'Peafowl'},
              {name: 'Aardvark'},
              {name: 'Cows'},
              {name: 'Crane Fly'},
              {name: 'Rock Hyrax'},
              {name: 'Gerbils'},
              {name: 'Brown Bear'}]
        },
        {
          name: 'Lillian Calamandrei',
          animals:
            [{name: 'Rats'},
              {name: 'Macaw'},
              {name: 'Gazelle'},
              {name: 'Gazelle'},
              {name: 'Alpaca'},
              {name: 'Snakes'},
              {name: 'Yellowjacket'},
              {name: 'Stickleback'}]
        },
        {
          name: 'Lina Allen',
          animals:
            [{name: 'Rabbit'},
              {name: 'Cats'},
              {name: 'Jaguarundi'},
              {name: 'Duck'},
              {name: 'Caribbean Flamingo'},
              {name: 'Oyster'},
              {name: 'Agouti'}]
        },
        {
          name: 'Georgia Hooper',
          animals:
            [{name: 'Grasshopper'},
              {name: 'Polar Bear'},
              {name: 'Rabbit'},
              {name: 'Loggerhead Turtle'},
              {name: 'Rhinoceros'},
              {name: 'African Wild Dog'},
              {name: 'Jackal'},
              {name: 'Zebu'}]
        },
        {
          name: 'Lillie Abbott',
          animals:
            [{name: 'John Dory'},
              {name: 'Gayal'},
              {name: 'Hawk'},
              {name: 'Umbrella Squid'},
              {name: 'Hyrax'},
              {name: 'Henkel\'s Leaf-tailed Gecko'}]
        },
        {
          name: 'Philip Davis',
          animals:
            [{name: 'Mini Donkey'},
              {name: 'Flatback Turtle'},
              {name: 'Rabbit'},
              {name: 'Zebra'},
              {name: 'Rhea'},
              {name: 'Leafy Seadragon'},
              {name: 'Bat'},
              {name: 'Caterpillar'}]
        }]
  },
  {
    name: 'Zuhackog',
    people:
      [{
        name: 'Elva Baroni',
        animals:
          [{name: 'Silkworm'},
            {name: 'Zebu'},
            {name: 'King Vulture'},
            {name: 'Zebrashark'},
            {name: 'Ostrich'},
            {name: 'Waxwing'}]
      },
        {
          name: 'Johnny Graziani',
          animals:
            [{name: 'Dunnart'},
              {name: 'Cotinga'},
              {name: 'Carp'},
              {name: 'Bat'},
              {name: 'Olive Sea Snake'},
              {name: 'Caterpillar'},
              {name: 'Jackal'}]
        },
        {
          name: 'Herman Christensen',
          animals:
            [{name: 'Death Adder'},
              {name: 'Pronghorn'},
              {name: 'Carp'},
              {name: 'Jaguar'},
              {name: 'Anteater'},
              {name: 'Zebu'},
              {name: 'Red Ruffed Lemur'}]
        },
        {
          name: 'Fannie Ancillotti',
          animals:
            [{name: 'Silkworm'},
              {name: 'Horses'},
              {name: 'Anaconda'},
              {name: 'Guinea'},
              {name: 'Bird'},
              {name: 'Aardwolf'},
              {name: 'Crane Fly'},
              {name: 'Caterpillar'}]
        },
        {
          name: 'Lawrence Camiciottoli',
          animals:
            [{name: 'Bustard'},
              {name: 'Numbat'},
              {name: 'Cat'},
              {name: 'Gecko'},
              {name: 'Northern Red Snapper'},
              {name: 'Monkfish'},
              {name: 'Birds'},
              {name: 'Caterpillar'},
              {name: 'Mule'}]
        },
        {
          name: 'Marion Landi',
          animals:
            [{name: 'Tortoise'},
              {name: 'Mule'},
              {name: 'Hedgehog'},
              {name: 'Geckos'},
              {name: 'Sheep'},
              {name: 'Emu'}]
        },
        {
          name: 'Lou de Bruin',
          animals:
            [{name: 'Boa'},
              {name: 'Death Adder'},
              {name: 'Okapi'},
              {name: 'Fly'},
              {name: 'Horses'}]
        }]
  },
  {
    name: 'Satanwi',
    people:
      [{
        name: 'Elmer Kinoshita',
        animals:
          [{name: 'Weasel'},
            {name: 'Birds'},
            {name: 'Snakes'},
            {name: 'Anteater'},
            {name: 'Groundhog'},
            {name: 'Ant'},
            {name: 'Courser'}]
      },
        {
          name: 'Cora Howell',
          animals:
            [{name: 'Rhea'},
              {name: 'Sponge'},
              {name: 'Cat'},
              {name: 'African Wild Dog'},
              {name: 'Snakes'},
              {name: 'Starling'},
              {name: 'Pronghorn'}]
        },
        {
          name: 'Ernest Conte',
          animals:
            [{name: 'Bird'},
              {name: 'Colugo'},
              {name: 'Grison'},
              {name: 'Pot Bellied Pig'},
              {name: 'Asian Elephant'}]
        },
        {
          name: 'Dennis Franci',
          animals:
            [{name: 'Grouse'},
              {name: 'Hapuka'},
              {name: 'Cheetah'},
              {name: 'Donkey'},
              {name: 'Turkey'},
              {name: 'Carp'},
              {name: 'Octopus'},
              {name: 'Silkworm'},
              {name: 'Bearded Dragon'}]
        },
        {
          name: 'Anthony Bruno',
          animals:
            [{name: 'Caracal'},
              {name: 'Anteater'},
              {name: 'Kiwa Hirsuta'},
              {name: 'Zooplankton'},
              {name: 'Tarantula'},
              {name: 'Oryx'}]
        }]
  }
]


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source