nit: `if (type && ending)` should be enough